Redis的sorted set类型

Redis 有序集合命令

下表列出了 redis 有序集合的基本命令:

序号 命令及描述
1 ZADD key score1 member1 [score2 member2] 
向有序集合添加一个或多个成员,或者更新已存在成员的分数
2 ZCARD key 
获取有序集合的成员数
3 ZCOUNT key min max 
计算在有序集合中指定区间分数的成员数
4 ZINCRBY key increment member 
有序集合中对指定成员的分数加上增量 increment
5 ZINTERSTORE destination numkeys key [key ...] 
计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中
6 ZLEXCOUNT key min max 
在有序集合中计算指定字典区间内成员数量
7 ZRANGE key start stop [WITHSCORES] 
通过索引区间返回有序集合指定区间内的成员
8 ZRANGEBYLEX key min max [LIMIT offset count] 
通过字典区间返回有序集合的成员
9 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] 
通过分数返回有序集合指定区间内的成员
10 ZRANK key member 
返回有序集合中指定成员的索引
11 ZREM key member [member ...] 
移除有序集合中的一个或多个成员
12 ZREMRANGEBYLEX key min max 
移除有序集合中给定的字典区间的所有成员
13 ZREMRANGEBYRANK key start stop 
移除有序集合中给定的排名区间的所有成员
14 ZREMRANGEBYSCORE key min max 
移除有序集合中给定的分数区间的所有成员
15 ZREVRANGE key start stop [WITHSCORES] 
返回有序集中指定区间内的成员,通过索引,分数从高到低
16 ZREVRANGEBYSCORE key max min [WITHSCORES] 
返回有序集中指定分数区间内的成员,分数从高到低排序
17 ZREVRANK key member 
返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序
18 ZSCORE key member 
返回有序集中,成员的分数值
19 ZUNIONSTORE destination numkeys key [key ...] 
计算给定的一个或多个有序集的并集,并存储在新的 key 中
20

ZSCAN key cursor [MATCH pattern] [COUNT count] 
迭代有序集合中的元素(包括元素成员和元素分值)

127.0.0.1:6379> zadd scores 94 zs 100 ls
(integer) 2
127.0.0.1:6379> zdd scores 60 ww 47 zl
(error) ERR unknown command `zdd`, with args beginning with: `scores`, `60`, `ww`, `47`, `zl`,
127.0.0.1:6379> zadd scores 60 ww 47 zl
(integer) 2
127.0.0.1:6379> ZRANGE scores 0 -1
1) "zl"
2) "ww"
3) "zs"
4) "ls"
127.0.0.1:6379> ZREVRANGE scores 0 -1
1) "ls"
2) "zs"
3) "ww"
4) "zl"
127.0.0.1:6379> ZRANGE scores 0 -1 withscores
1) "zl"
2) "47"
3) "ww"
4) "60"
5) "zs"
6) "94"
7) "ls"
8) "100"
127.0.0.1:6379> ZREM scores ww
(integer) 1
127.0.0.1:6379> ZREVRANGE scores 0 -1
1) "ls"
2) "zs"
3) "zl"
127.0.0.1:6379> ZRANGEBYSCORE scores 50 80 withscores
(empty list or set)
127.0.0.1:6379> ZRANGEBYSCORE scores 40 80 withscores
1) "zl"
2) "47"
127.0.0.1:6379> ZRANGEBYSCORE scores 40 100 withscores limit 1
(error) ERR syntax error
127.0.0.1:6379> ZRANGEBYSCORE scores 40 100 withscores limit 0 1
1) "zl"
2) "47"
127.0.0.1:6379> ZRANGEBYSCORE scores 40 100 withscores limit 0 2
1) "zl"
2) "47"
3) "zs"
4) "94"
127.0.0.1:6379> ZREMRANGEBYRANK scores 0 0
(integer) 1
127.0.0.1:6379> ZRANGE scores 0 -1 withscores
1) "zs"
2) "94"
3) "ls"
4) "100"
127.0.0.1:6379> ZCARD scores
(integer) 2
127.0.0.1:6379> ZCOUNT scores 90 99
(integer) 1
127.0.0.1:6379> zadd s1 50 aa 60 bb 70 cc
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> zadd s 50 aa 60 bb 70 cc
(integer) 3
127.0.0.1:6379> zadd s1 50 aa 70 bb
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> zadd ss 50 aa 70 bb
(integer) 2
127.0.0.1:6379> ZINTERSTORE sss 2 s ss
(integer) 2
127.0.0.1:6379> ZRANGE sss 0 -1 withscores
1) "aa"
2) "100"
3) "bb"
4) "130"
127.0.0.1:6379> ZINTERSTORE de

业务场景

票选广东十大杰出青年,各类综艺选秀海选投票各类资源网站TOP10(电影,歌曲,文档,电商,游戏等)
聊天室活跃度统计
游戏好友亲密度

 

127.0.0.1:6379> ZADD movies 143 aa 97 bb 200 cc
(integer) 3
127.0.0.1:6379> ZRAnk movies bb
(integer) 0
127.0.0.1:6379> ZRAnk movies cc
(integer) 2
127.0.0.1:6379> ZREVRANk movies bb
(integer) 2
127.0.0.1:6379> ZSCORE movies aa
"143"
127.0.0.1:6379> ZINCRBY movies 1 aa
"144"
127.0.0.1:6379> ZSCORE movies aa
"144"
127.0.0.1:6379>

业务场景
基础服务+增值服务类网站会设定各位会员的试用,让用户充分体验会员优势。例如观影试用VIP、游戏
VIP体验、云盘下载体验VIP、数据查看体验VIP。当VIP体验到期后,如果有效管理此类信息。即便对于正式
VIP用户也存在对应的管理方式。
网站会定期开启投票、讨论,限时进行,逾期作废。如何有效管理此类过期信息。

127.0.0.1:6379> zadd tx 19021090 uid:001
(integer) 1
127.0.0.1:6379> zadd tx 19234290 uid:007
(integer) 1
127.0.0.1:6379> zadd tx 1923429083 uid:008
(integer) 1
127.0.0.1:6379> zrange tx 0 -1 withscores
1) "uid:001"
2) "19021090"
3) "uid:007"
4) "19234290"
5) "uid:008"
6) "1923429083"
127.0.0.1:6379> time
1) "1584619821"
2) "452034"
127.0.0.1:6379>

业务场景
任务/消息权重设定应用
当任务或者消息待处理,形成了任务队列或消息队列时,对于高优先级的任务要保障对其优先处理,如
何实现任务权重管理。

127.0.0.1:6379> ZADD tasks 4 order:id:001
(integer) 1
127.0.0.1:6379> ZADD tasks 1 order:id:002
(integer) 1
127.0.0.1:6379> ZADD tasks 9 order:id:003
(integer) 1
127.0.0.1:6379> ZREVRANGE task 0 -1 withscores
(empty list or set)
127.0.0.1:6379>
127.0.0.1:6379> ZREVRANGE tasks 0 -1 withscores
1) "order:id:003"
2) "9"
3) "order:id:001"
4) "4"
5) "order:id:002"
6) "1"
127.0.0.1:6379> ZREVRANGE tasks 0 0
1) "order:id:003"
127.0.0.1:6379> ZREM tasks order:id:003
(integer) 1
127.0.0.1:6379> ZREVRANGE tasks 0 -1 withscores
1) "order:id:001"
2) "4"
3) "order:id:002"
4) "1"
127.0.0.1:6379>

sorted_set 类型数据操作的注意事项

score保存的数据存储空间是64位,如果是整数范围是-9007199254740992~9007199254740992
score保存的数据也可以是一个双精度的double值,基于双精度浮点数的特征,可能会丢失精度,使用时候要慎重
sorted_set 底层存储还是基于set结构的,因此数据不能重复,如果重复添加相同的数据,score值将被反复覆盖,保留最后一次修改的结果

发布了410 篇原创文章 · 获赞 2 · 访问量 6988

猜你喜欢

转载自blog.csdn.net/qq_29860591/article/details/104981764