sorted_set 类型
新的存储需求:数据排序有利于数据的有效展示,需要提供一种可以根据自身特征进行排序的方式
需要的存储结构:新的存储模型,可以保存可排序的数据
sorted_set类型:在set的存储结构基础上添加可排序字段
sorted_set 类型数据的基本操作
添加数据
zadd key score1 member1 [score2 member2]
获取全部数据
zrange key start stop [WITHSCORES]
zrevrange key start stop [WITHSCORES]
删除数据
zrem key member [member ...]
127.0.0.1:6379> zadd scores 94 zs
(integer) 1
127.0.0.1:6379> zadd scores 100 1s
(integer) 1
127.0.0.1:6379> zadd scores 60 ww
(integer) 1
127.0.0.1:6379> zadd scores 47 z1
(integer) 1
127.0.0.1:6379> zrange scores 0 -1
1) "z1"
2) "ww"
3) "zs"
4) "1s"
127.0.0.1:6379> zrange scores 0 -1
1) "z1"
2) "ww"
3) "zs"
4) "1s"
127.0.0.1:6379> zrange scores 0 -1 withscores
1) "z1"
2) "47"
3) "ww"
4) "60"
5) "zs"
6) "94"
7) "1s"
8) "100"
127.0.0.1:6379> zrevrange scores 0 -1 withscores
1) "1s"
2) "100"
3) "zs"
4) "94"
5) "ww"
6) "60"
7) "z1"
8) "47"
127.0.0.1:6379> zrem scores ww
(integer) 1
127.0.0.1:6379> zrevrange scores 0 -1 withscores
1) "1s"
2) "100"
3) "zs"
4) "94"
5) "z1"
6) "47"
sorted_set 类型数据的基本操作
按条件获取数据
zrangebyscore key min max [WITHSCORES] [LIMIT]
zrevrangebyscore key max min [WITHSCORES]
127.0.0.1:6379> zrangebyscore scores 50 99 limit 0 3 withscores
1) "zs"
2) "94"
//50 到 90 前三条
条件删除数据
zremrangebyrank key start stop
zremrangebyscore key min max
127.0.0.1:6379> zremrangebyrank scores 0 1
(integer) 2
//按索引删除
获取集合数据总量
zcard key
zcount key min max
集合交、并操作
zinterstore destination numkeys key [key ...]
zunionstore destination numkeys key [key ...]
127.0.0.1:6379> zadd s1 50 aa 60 bb 70 cc
(integer) 3
127.0.0.1:6379> zadd s2 60 aa 40 bb 90 dd
(integer) 3
127.0.0.1:6379> zadd s3 70 aa 20 bb 100 dd
(integer) 3
127.0.0.1:6379> zinterstore ss 3 s1 s2 s3
(integer) 2
127.0.0.1:6379> zrange ss 0 -1 withscores
1) "bb"
2) "120"
3) "aa"
4) "180"
127.0.0.1:6379> help zinterstore
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
summary: Intersect multiple sorted sets and store the resulting sorted set in a new key
since: 2.0.0
group: sorted_set
127.0.0.1:6379> zinterstore sss 3 s1 s2 s3 aggregate max
(integer) 2
127.0.0.1:6379> zrange sss 0 -1 withscores
1) "bb"
2) "60"
3) "aa"
4) "70"
127.0.0.1:6379> help zunionstore
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
group: sorted_set
sorted_set 类型数据的扩展操作
为所有参与排名的资源建立排序依据
获取数据对应的索引(排名)
zrank key member
zrevrank key member
score值获取与修改
zscore key member
zincrby key increment member
//电影资源
127.0.0.1:6379> zadd movies 143 aa 97 bb 201 cc
(integer) 3
//查询bb电影的热度排名
127.0.0.1:6379> zrank movies bb
(integer) 0
127.0.0.1:6379> zrevrank movies bb
(integer) 2
//查aa有多少热度
127.0.0.1:6379> zscore movies aa
"143"
//投票加一
127.0.0.1:6379> zincrby movies 1 aa
"144"
redis 应用于计数器组合排序功能对应的排名
sorted_set 类型数据操作的注意事项
score保存的数据存储空间是64位,如果是整数范是-9007199254740992~9007199254740992
score保存的数据也可以是一个双精度的double值,基于双精度浮点数的特征,可能会丢失精度,使用时候要慎重
sorted_set 底层存储还是基于set结构的,因此数据不能重复,如果重复添加相同的数据,score值将被反复覆盖,保留最后一次修改的结果