Redis简介

Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。
它支持字符串、哈希表、列表、集合、有序集合,位图,hyperloglogs等数据类型。
内置复制、Lua脚本、LRU收回、事务以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动分区。

Redis的特点

Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。
Redis不仅仅支持简单的k-v类型的数据,同时还提供list,set,zset,hash等数据结构的存储。
Redis支持数据的备份,即master-slave主从模式的数据备份。

Redis的优势

性能极高——Redis读的速度为11w/s,写的速度为8.1w/s。
丰富的数据类型——Redis支持二进制案例的Strings,Lists,Hashes,Sets即Ordered Sets数据类型操作。
原子性——Redis的所有操作都是原子性的,同时Redis还支持对几个操作合并后的原子性执行。
丰富的特性——Redis还支持public/subscribe,通知,key过期等特性。

Redis应用

应用在高并发和实时请求的场景,eg新浪微博
hash:关注列表,粉丝列表
string:微博数,粉丝数
(避免使用select count(*) from...)
sorted set:
TopN,热门微博,还有github,stackoverflow

部署

mkdir -p /application/tools
cd /application/tools
wget -q http://download.redis.io/releases/redis-5.0.7.tar.gz
tar xf redis-5.0.7.tar.gz -C /application/
make /application/redis-5.0.7 && make install 

复制新文件实例

mkdir -p /application/redis6380
cp /application/redis-5.0.7/redis.conf /application/redis6380/
cp /application/redis-5.0.7/src/redis-server /application/redis6380/

创建实例日志存放路径以启动名称到环境启动加载位置并授权

mkdir -p /application/redis6380/logs/
cp /application/redis-5.0.7/src/redis-cli /usr/bin/
cp /application/redis-5.0.7/src/redis-server /usr/bin/
chmod +x /usr/bin/redis-server
chmod +x /usr/bin/redis-cli

修改配置文件

egrep -v "^#|^$" /application/redis6380/redis.conf

bind 127.0.0.1
protected-mode yes
requirepass root
port 6380
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /application/redis6380/redis-6380.pid
loglevel notice
logfile "/application/redis6380/logs/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

启动脚本

cat /etc/init.d/redis

#!/bin/bash
# chkconfig: 2345 80 90
# description: Start and Stop redis
REDISPORT=6380
EXEC=/application/redis6380/redis-server
REDIS_CLI=/usr/bin/redis-cli
PIDFILE=/application/redis6380/redis-6380.pid
CONF="/application/redis6380/redis.conf"
AUTH="root" #如果启用了密码,此处需要添加该项
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${0} stop
${0} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
exit 1
esac

chmod +x /etc/init.d/redis

chkconfig --add redis

service redis satrt

配置成systemctl启动方式

vim /usr/lib/systemd/system/redis.service

[Unit]
Description=Redis persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target [Service]
ExecStart=/data/redis6380/redis-server /data/redis6380/redis.conf --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755 [Install]
WantedBy=multi-user.target

提示:如果多个实例可以复制出多个文件,然后改配置路径,复制出来的文件名不能一样,可以以端口号来命名

systemctl daemon-reload
systemctl enable redis.service
systemctl start redis.service

最新文章

  1. iOS 键盘类型
  2. iOS开发中的错误整理,再一次整理通过通知中心来处理键盘,一定记得最后关闭通知中心
  3. Func<T>与Action<T>委托泛型介绍
  4. 哈哈,好像swift 以后有可能用来开发安卓喔
  5. 【jqGrid for ASP.NET MVC Documentation】.学习笔记.3.本地化语言包
  6. JavaScript之放大镜效果
  7. JavaWeb核心编程之(三.6)HttpServlet
  8. MyEclipse 8.5整合Git,并在Github上发布项目(转)
  9. 读取和存储文本文件,UTF-8和GB2312通用的函数
  10. 把ResultSet对象转变成List对象
  11. IDEA的优质使用博客资源
  12. linux下安装python3.6.4
  13. Java 读书笔记 (二) 对象和类
  14. 原生JS实现下拉刷新
  15. swift3 单例写法
  16. noip第2课作业
  17. linux 按照端口一句命令杀死进程,按照进程名称一句命令杀死进程
  18. python_web应用雏型
  19. request.getParameter()和request.getAttribute()的区别
  20. 内核模块module传参

热门文章

  1. Pets(匈牙利算法)
  2. [01] C#网络编程的最佳实践
  3. centOS7 设置mysql数据库外网可以访问
  4. JVM中的对象
  5. Fastbin attack——Double Free
  6. jmeter的用途
  7. 痞子衡嵌入式:IAR在线调试时设不同复位类型可能会导致i.MXRT下调试现象不一致(J-Link / CMSIS-DAP)
  8. 大写的服,看完这篇你还不懂RocketMQ算我输
  9. 【二叉树-最长路径系列(任意路径)】直径、最长同值路径、 最大路径和(DFS、树形DP)
  10. hystrix讲解:熔断降级隔离以及合并请求