1.String
key value
缓存对象
set user:1 xxx(JSON序列化)
建议使用protobuffer做序列号
对个别属性经常改动,可以使用
mset user:2:name yinzhen2 user:2:age 19
mget user:2:name user:2:age
计数器记录访问次数
incr article:程序员的自我修养
分布式锁
setnx 加上 lua脚本设置过期时间
使用ThreadLocal存放key对应的value uuid,只能删除自己的锁
使用定时任务不停的保活
有线程框架
https://github.com/redisson/redisson/ 14.9k
分布式id,一次获取1000个id,到500的时候再次获取
incrby generate:order:id 1000
2.Hash
key field value
field value
缓存对象
hmset userInfo user:1:id xxx user:1:name xxx
Hash和String的选择
底层都是Hash存储
String 是整个redis存储是个hash,和其他key放着一起
Hash key 加上value Hash
使用hgetall获取所有的filed比较方便
Hash对单个属性的操作比较方便
Hash 使用容易参数bigkey、负载不均衡问题,一般使用2的n次方个hash
和2的n次方减1做与,散列存储
1.存放apiCode的访问次数
32个hashkey,apiCode和31 与 运算得出key
hincrby apiCode:visitCount:1 10000 1
hincrby apiCode:visitCount:2 10001 1
hget apiCode:visitCount:1 10000
2.实现购物车
hincrby cat:001 1001 1
hincrby cat:001 1002 1
hgetall cat:001
3.List
lpush lpop rpush rpop brpop blpop
lpush lpop可以实现栈
lpush rpop 可以实现队列
lpush brpop blpop 可以实现 阻塞队列
BLPOP key [key ...] timeout
summary: Remove and get the first element in a list, or block until one is available
since: 2.0.0
BRPOP key [key ...] timeout
summary: Remove and get the last element in a list, or block until one is available
since: 2.0.0
使用栈来实现公众号发布订阅
公众号A发布了一个消息
循环所有的用户,向用户的消息列表中添加整个消息
lpush user:1:message:list xxx
用户获取最新消息的时候使用
lpop获取最近的10条消息
lrange user:1:message:list 0,10
4.set集合
sadd userSet 111 222 333
smembers userSet
srem userSet 222
抽奖
SRANDMEMBER key [count]
summary: Get one or multiple random members from a set
since: 1.0.0
SPOP key [count]
summary: Remove and return one or multiple random members from a set
since: 1.0.0
点赞
sadd article:like:{文章id} {userId}
sadd article:like:0001 0001
sadd article:like:0001 0002
sadd article:like:0001 0003
srem article:like:0001 0002
SCARD key
summary: Get the number of members in a set
since: 1.0.0
SMEMBERS key
summary: Get all the members in a set
since: 1.0.0
集合运算
help @set
SINTER key [key ...]
summary: Intersect multiple sets
since: 1.0.0
SDIFF key [key ...]
summary: Subtract multiple sets
since: 1.0.0
第一个集合 减去后面集合的并集
SUNION key [key ...]
summary: Add multiple sets
since: 1.0.0
关注模型
张三的关注 a b c
sadd follow:zhangsan a b c
李四的关注 b c d
sadd follow:lisi b c d
张三和李四共同的关注
sinter follow:zhangsan follow:lisi
我关注的人也关注他的人
循环我关注的人,查看是否在他的集合中
可能认识的人
循环我关注的人,用我关注的人的集合减去我的集合
sdiff follow:lisi follow:zhangsan
商品搜索
按照品牌、内存给产品分类
求并集
sass brand:huawei p40
sadd mem:8g p40 p30
找华为 8g的手机
sinter brand:huawei mem:8g
5.zset
help @sorted_set
ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
summary: Add one or more members to a sorted set, or update its score if it already exists
since: 1.2.0
ZCARD key
summary: Get the number of members in a sorted set
since: 1.2.0
排行榜
一日排行榜
ZINCRBY top1020201119 1 xxx1
ZINCRBY key increment member
summary: Increment the score of a member in a sorted set
since: 1.2.0
ZREVRANGE key start stop [WITHSCORES]
summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
since: 1.2.0
七日排行榜
ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
summary: Add multiple sorted sets and store the resulting sorted set in a new key
since: 2.0.0