在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一、前戏 Sorted-Sets和Sets类型极为相似,它们都是字符串的集合,都不允许重复的成员出现在一个Set中。它们之间的主要差别是Sorted-Sets中的每一个成员都会有一个分数(score)与之关联,Redis正是通过分数来为集合中的成员进行从小到大的排序。然而需要额外指出的是,尽管Sorted-Sets中的成员必须是唯一的,但是分数(score)却是可以重复的。 二、理论
三、实战 127.0.0.1:6379> zadd mykey 10 a 11 b
(integer) 2
127.0.0.1:6379> zcard mykey
(integer) 2
127.0.0.1:6379> zadd mykey 12 c 13 d
(integer) 2
127.0.0.1:6379> zcard mykey
(integer) 4
127.0.0.1:6379> zcount mykey 11 13
(integer) 3
127.0.0.1:6379> zincrby mykey 5 a
"15"
127.0.0.1:6379> zrange mykey 1 3
1) "c"
2) "d"
3) "a"
127.0.0.1:6379> zrange mykey 1 3 withscores
1) "c"
2) "12"
3) "d"
4) "13"
5) "a"
6) "15"
127.0.0.1:6379> zrangebyscore mykey 11 13
1) "b"
2) "c"
3) "d"
127.0.0.1:6379> zrangebyscore mykey 11 13 withscores
1) "b"
2) "11"
3) "c"
4) "12"
5) "d"
6) "13"
127.0.0.1:6379> zrangebyscore mykey 11 13 withscores
1) "b"
2) "11"
3) "c"
4) "12"
5) "d"
6) "13"
127.0.0.1:6379> zrank mykey a
(integer) 3
127.0.0.1:6379> zrem mykey a
(integer) 1
127.0.0.1:6379> zrevrange mykey 1 3 withscores
1) "c"
2) "12"
3) "b"
4) "11"
127.0.0.1:6379> zrevrank mykey a
(nil)
127.0.0.1:6379> zrevrank mykey b
(integer) 2
127.0.0.1:6379> zscore mykey b
"11"
127.0.0.1:6379> zrevrangebyscore mykey 13 11 withscores
1) "d"
2) "13"
3) "c"
4) "12"
5) "b"
6) "11"
127.0.0.1:6379> zremrangebyrank mykey 1 2
(integer) 2
127.0.0.1:6379> zremrangebyscore mykey 10 15
(integer) 1
127.0.0.1:6379> zrange mykey 0 4 withscores
(empty list or set)
127.0.0.1:6379> zcard mykey
(integer) 0
127.0.0.1:6379>
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论