Redis理解和使用
使用场景
- 缓存
- 排行榜系统
- 计数器应用
- 社交网络
- 消息队列系统
理解使用
全局指令
- 查看所有键
keys *
- 键总数
dbsize
- 键是否存在
exists key
- 删除键
del key
- 键过期
expire key 10
- 键类型
type key
内部编码
- 查看内部编码
object encoding key
object idletime key
- 数据结构和内部编码
数据结构 | 内部编码 |
---|
string | raw、int、embstr |
hash | hashtable、ziplist |
list | linkedlist、ziplist |
set | hashtable、intset |
zset | skiplist、ziplist |
字符串
常用命令
- 赋值
set key "value"
setex key 10 "value"
setnx key "value"
mset key1 "value1" key2 "value2"
- 取值
get key
mget key1 key2
- 计数
incr key
decr key
- 追加
append key "s"
- 长度
strlen key
- 替换
setrange key 0 "V"
- 截取 (闭区间)
getrange key 0 0
内部编码
内部编码 | 出现条件 |
---|
int | 8字节整数 |
embstr | 字符串长度不超过39 |
raw | 字符串长度超过39 |
应用场景
- 缓存
比较经典使用场景,使用Mysql做存储层,使用Redis做缓存层。 - 计数
比如社交网络的视频播放数 - 共享
分布式web服务session共享 - 限速
某段时间内获取验证码次数,某ip某段时间访问次数
哈希
常用命令
- 赋值
hset user:1 id 1 name "ren"
hsetnx user:1 age 25
hmset user:1 sex man phone "158****4591"
- 取值
hget user:1 id
hmget user:1 id sex phone
hkeys user:1
hvals user:1
hgetall user:1
- 删除
hdel user:1 age
- 长度
hlen user:1
hstrlen user:1 name
- 存在
hexists user:1 age
- 计数
hincrby user:1 version 1
hincrbyfloat user:1 version 1.0
内部编码
内部编码 | 出现条件 |
---|
ziplist | 字段数小于hash-max-ziplist-entries(默认512)且字段值长度小于hash-max-ziplist-value(默认值64) |
hashtable | 哈希类型无法满足ziplist条件 |
使用场景
缓存对比字符串
类型 | 优点 | 缺点 |
---|
字符串 | 简单,合理使用可以提高内存利用率 | 序列化开销和跟新字段需全量跟新 |
哈希 | 直观,合理使用可以减少内存使用 | 注意hashtable内存消耗 |
列表
常用命令
- 添加
rpush users u1 u2 u3 u4
linsert users before u3 u
rpush users u5
- 修改
ltrim users 0 0
lset users 0 u4
- 删除
lpop users 2
rpop users
lrem users 1 u0
blpop users 0
brpop users 10
- 查询
lrange users 0 0
lindex users 0
lindex users -1
llen users
内部编码
内部编码 | 出现条件 |
---|
quicklist | redis3.2提供quicklist内部编码,结合ziplist和linkedlist优势 |
ziplist | 字段数小于hash-max-ziplist-entries(默认512)且字段值长度小于hash-max-ziplist-value(默认值64) |
linkedlist | 类型无法满足ziplist条件 |
使用场景
- 消息队列
利用 lpush + brpop命令组合可实现阻塞队列 - 文章列表
集合
常用命令
- 添加
sadd persons p1 p2 p3 p4
- 长度
scard persons
- 删除
srem persons p4
spop persons 2
- 查询
sismember persons p1
srandmember persons 2
smembers persons
- 交集
sinter ps1 ps2
sinterstore psi ps1 ps2
- 并集
sunion ps1 ps2
sunionstore psu ps1 ps2
- 差集
sdiff ps1 ps2
sdiffstore pss ps1 ps2
内部编码
内部编码 | 出现条件 |
---|
intest | 字段数小于set-max-intest-entries(默认512)且字段值长度小于set-max-intest-value(默认值64) |
hashtable | 无法满足intest条件 |
使用场景
- 标签
有序集合
常用命令
- 添加
zadd course 100 u1 95 u2
- 查询
zcard course
zscore course u1
zrank course u1
zrevrank course u1
zrange course 0 3 withscores
zrevrange course 0 3
zrangebyscore course 80 80
zrevrangebyscore course 80 80
zcount course 80 100
- 删除
zrem course u1
zremrangebyrank course 0 0
zremrangebyscore course 80 80
- 修改
zincrby course 5 u2
- 交集
zinter 2 cs1 cs2
zinterstore csi 2 cs1 cs2
- 并集
zunion 2 cs1 cs2
zunionstore csu 2 cs1 cs2
- 差集
zdiff 2 cs1 cs2
zdiffstore css 2 cs1 cs2
内部编码
内部编码 | 出现条件 |
---|
ziplist | 字段数小于zset-max-intest-entries(默认128)且字段值长度小于zset-max-intest-value(默认值64) |
skplist | 无法满足ziplist条件 |
使用场景
- 排行榜系统
键管理
- 重命名
rename old:name new:name
- 随机键
randomkey
- 键过期
expire psi 10
ttl psi
persist ps
- 键迁移
dump users
restore users 0 "xxxxx"
migrate 172.17.0.3 6379 "" 0 3000 REPLACE keys users
5 键遍历
scan 0 MATCH "*" count 10
scan 无法保证完整遍历出来所有的键
6 库管理
select 0
flushdb
flushall
慢日志
config set slowlog-log-slower-than 1000
config set slowlog-max-len 1000
slowlog get
slowlog len
slowlog reset
redis shell
- redis-cli
redis-cli -h 127.0.0.1 -r 5 -i 1 -a 1250 ping
redis-cli --slave -a 1250
redis-cli -a 1250 --scan 0
redis-cli --bigkeys
redis-cli --stat
- redis-benchmark
redis-benchmark -c 100 -n 1000 -r 10000 -a 1250
redis message
subscribe channel:sports
psubscribe channel:*
publish channel:sports "xiusen won the championship"
unsubscribe channel:sports
punsubscribe channel:*
redis client
info clients
client list
client pause 60000
monitor
集群
持久化
RDB
rdb 持久化是将当前进程数据生成快照保存到硬盘的过程
AOF
AOF(append only file)持久化是以独立日志的方式记录每次写命令,重启的时候再重新执行AOF文件中的命令到达数据恢复的目的,AOF工作流程包括 append、sync、rewrite、load
复制
docker run -p 6010:6379 --name redis-master \
-v /var/redis/master/data:/data \
-v /var/redis/master/conf/:/usr/local/etc/redis/ \
-d redis:7.0.4 redis-server /usr/local/etc/redis/redis.conf
docker run -p 6012:6379 --name redis-slave01 \
-v /var/redis/slave01/data:/data \
-v /var/redis/slave01/conf/:/usr/local/etc/redis/ \
-d redis:7.0.4 redis-server /usr/local/etc/redis/redis.conf --slaveof 172.17.0.2 6379
docker run -p 6014:6379 --name redis-slave02 \
-v /var/redis/slave01/data:/data \
-v /var/redis/slave01/conf/:/usr/local/etc/redis/ \
-d redis:7.0.4 redis-server /usr/local/etc/redis/redis.conf --slaveof 172.17.0.2 6379
哨兵
docker run -p 6020:26379 --name redis-sentinel-1 \
-v /var/redis/master/conf/:/usr/local/etc/redis/ \
-d redis:7.0.4 redis-sentinel /usr/local/etc/redis/sentinel.conf
docker run -p 6022:26379 --name redis-sentinel-2 \
-v /var/redis/slave01/conf/:/usr/local/etc/redis/ \
-d redis:7.0.4 redis-sentinel /usr/local/etc/redis/sentinel.conf
docker run -p 6024:26379 --name redis-sentinel-3 \
-v /var/redis/slave01/conf/:/usr/local/etc/redis/ \
-d redis:7.0.4 redis-sentinel /usr/local/etc/redis/sentinel.conf
sentinel master redis-sentinel
sentinel slaves redis-sentinel
sentinel sentinels redis-sentinel
sentinel get-master-addr-by-name redis-sentinel
sentinel reset redis-sentinel
sentinel failover redis-sentinel
sentinel ckquorum redis-sentinel
sentinel flushconfig
sentinel remove redis-sentinel
sentinel monitor redis-sentinel 172.17.0.2 6379 2