概述
有序集合类型(Sorted Set)
有序集合类型与上一篇的集合类型的区别就是“有序”二字。
在集合类型的基础上有序集合为集合中的每个元素都关联一个分数,这使得我们不仅可以完成插入、删除和判断元素是否存在等集合类型支持的操作,还能获得分数最高(或最低)的前N个元素、获得指定分数范围内的元素等与分数有关的操作。虽然集合中每个元素都是不同的,但它们的分数却可以相同。
有序集合类型在某些方面和列表类型有些相似:
(1)二者都是有序的。
(2)二者都可以获得某一范围的元素。
但是二者有着很大的区别,应用场景也不同:
(1)列表类型是通过链表实现的,获取靠近两端的数据速度极快,而当元素增多后,访问中间数据的速度会较慢,所以它更加适合如“新鲜事”或“日志”这样很少访问中间元素的应用;
(2)有组集合类型是使用散列表和跳跃表(Skip list)实现的,所以即使读取位于中间部分的数据速度也很快(时间复杂度O(log(N)));
(3)列表中不能简单地调整某个元素的位置,单有序集合可以(通过更改这个元素的分数);
(4)有序集合要比列表类型更耗内存。
命令
1、增加元素
ZADD key score member [score member ...]
ZADD 命令用来向有序集合中加入一个元素和该元素的分数,如果该元素已经存在则会用新的分数替换原有的分数。ZADD命令的返回值是新加入到集合中的元素个数(不包含之前已经存在的元素)。
模拟数据,有分数板记录a,b,c 三名运动员的分数(分别是89分,67分和100分)
127.0.0.1:6379> zadd scoreboard 89 a 67 b 100 c
(integer) 3
修改b的分数
127.0.0.1:6379> zadd scoreboard 76 b
(integer) 0
分数不仅可以是整数,还可以支持双精度浮点数:
127.0.0.1:6379> zadd test 17E+307 a
(integer) 1
127.0.0.1:6379> zadd test 1.5 b
(integer) 1
127.0.0.1:6379> zadd test +inf c
(integer) 1
127.0.0.1:6379> zadd test -inf d
(integer) 1
其中+inf和-inf分别表示正无穷和负无穷。
2、获得元素的分数
ZSCORE key member
示例如下:
127.0.0.1:6379> zscore test b
"1.5"
3、获得排名在某个范围的元素列表
ZRANGE key start stop [WITHSCORES]
ZREVRANGE key start stop [WITHSCORES]
ZRANGE 命令会按照元素分数从小到大的顺序返回索引从start到stop之间的所有元素(包含两端的元素)。ZRANGE命令与LRANGE十分相似,如索引都是从0开始,负数代表从后向前查找(-1表示最后一个元素)。如:
127.0.0.1:6379> zrange scoreboard 0 2
1) "b"
2) "a"
3) "c"
127.0.0.1:6379> zrange scoreboard 1 -1
1) "a"
2) "c"
如果需要同时获得元素的分数的话,可以在ZRANGE命令的尾部加上WITHSCORES参数,这时返回数据格式就从“元素1,元素2,…,元素n”变为“元素1,分数1,元素2,分数2,…,元素n,分数n”,如:
127.0.0.1:6379> zrange scoreboard 0 -1 withscores
1) "b"
2) "76"
3) "a"
4) "89"
5) "c"
6) "100"
如果两个元素的分数相同,Redis会按照字典顺序(即“0”<“9”<“A”<“Z”<“a”<"z"这样的顺序)来进行排序。如:
127.0.0.1:6379> zadd sametest 10 2tts 10 3ttd 20 ADS 20 DRE 20 3ED 20 ass 20 dfg
(integer) 7
127.0.0.1:6379> zrange sametest 0 -1
1) "2tts"
2) "3ttd"
3) "3ED"
4) "ADS"
5) "DRE"
6) "ass"
7) "dfg"
ZREVRANGE 命令和ZRANGE的唯一不同在于ZREVRANGE 命令是按照元素分数从大到小的顺序给出结果的。
4、获得指定分数范围的元素
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
ZRANGEBYSCORE 命令参数虽然多,但是都很好理解。该命令按照元素分数从小到大的顺序返回分数在min和max之间(包含min和max)的元素:
127.0.0.1:6379> zrangebyscore scoreboard 80 100
1) "a"
2) "c"
如果希望分数范围不包含端点值,可以在分数前加上“(”符号,如,希望返回“80到100的数据,包含80,但不包含100”:
127.0.0.1:6379> zrangebyscore scoreboard 80 (100
1) "a"
min和max也支持无穷大:
127.0.0.1:6379> zrangebyscore scoreboard 80 +inf
1) "a"
2) "c"
WITHSCORES参数的用法和ZRANGE命令一样;
LIMIT offset count,即在获得的元素列表的基础上向后偏移offset个元素,并且只获取前count个元素,如:
127.0.0.1:6379> zadd scoreboard 56 d 92 e 67 f
(integer) 3
127.0.0.1:6379> zrange scoreboard 0 -1 withscores
1) "d"
2) "56"
3) "f"
4) "67"
5) "b"
6) "76"
7) "a"
8) "89"
9) "e"
10) "92"
11) "c"
12) "100"
127.0.0.1:6379> zrangebyscore scoreboard 60 +inf limit 1 3
1) "b"
2) "a"
3) "e"
若是想获取分数低于或等于100的前3个人,可以 使用ZREVRANGEBYSCORE命令实现,结果是按照元素分数从大到小的顺序排列,并且它的min和max参数顺序和ZRANGEBYSCORE命令是相反的。如:
127.0.0.1:6379> zrevrangebyscore scoreboard 100 0 limit 0 3
1) "c"
2) "e"
3) "a"
5、增加某个元素的分数
ZINCRBY key increment member
ZINCRBY 命令可以增加一个元素的分数,返回值是更改后的分数,如给b加4分:
127.0.0.1:6379> zincrby scoreboard 4 b
"80"
increment 也可以是个负数,表示减分,如给b减4分:
127.0.0.1:6379> zincrby scoreboard -4 b
"76"
如果指定的元素不存在,Redis在执行命令前会先建立它并将它的分数赋为0再执行操作。
6、获得集合中元素的数量
ZCARD key
如:
127.0.0.1:6379> zcard scoreboard
(integer) 6
7、获得指定分数范围内的元素个数
ZCOUNT key min max
如:
127.0.0.1:6379> zcount scoreboard 80 100
(integer) 3
127.0.0.1:6379> zcount scoreboard (70 +inf
(integer) 4
8、删除一个或多个元素
ZREM key member [member ...]
ZREM 命令的返回值是成功删除的元素数量(不包含本来就不存在的元素)。如:
127.0.0.1:6379> zrem scoreboard a
(integer) 1
127.0.0.1:6379> zrem scoreboard a
(integer) 0
127.0.0.1:6379> zcard scoreboard
(integer) 5
9、按照排名范围删除元素
ZREMRANGEBYRANK key start stop
ZREMRANGEBYRANK 命令按照元素分数从小到大的顺序(即索引0表示最小的值)删除处在指定排名范围内的所有元素,并返回删除的元素数量。如:
127.0.0.1:6379> zadd testrem 1 a 2 b 3 c 4 d 5 e 6 f
(integer) 6
127.0.0.1:6379> zremrangebyrank testrem 0 2
(integer) 3
127.0.0.1:6379> zrange testrem 0 -1
1) "d"
2) "e"
3) "f"
10、按照分数范围删除元素
ZREMRANGEBYSCORE key min max
ZREMRANGEBYSCORE 命令会删除指定分数范围内的所有元素,参数min和max的特性和ZRANGEBYSCORE命令中的一样。返回值是删除的元素数量,如:
127.0.0.1:6379> zremrangebyscore testrem (4 5
(integer) 1
127.0.0.1:6379> zrange testrem 0 -1
1) "d"
2) "f"
11、获得元素的排名
ZRANK key member
ZREVRANK key member
ZRANK 命令会按照元素分数从小到大的顺序获得指定的元素的排名(从0开始,即分数最小的元素排名为0)。如:
127.0.0.1:6379> zrank scoreboard d
(integer) 0
ZREVRANK 命令则相反(分数最大的元素排名为0):
127.0.0.1:6379> zrevrank scoreboard d
(integer) 4
12、计算有序集合的交集
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
ZINTERSTORE 命令用来计算多个有序集合的交集,并将结果存储在destination 键中(同样以有序集合类型存储),其返回值为destination 中的元素个数。
destination 键中元素的分数是由AGGREGATE 参数决定的
(1)当AGGREGATE 是SUM时(也就是默认值),destination 键中元素的分数是每个参与计算的几个中该元素分数的和。如:
127.0.0.1:6379> zadd test1 1 a 2 b
(integer) 2
127.0.0.1:6379> zadd test2 10 a 20 b
(integer) 2
127.0.0.1:6379> zinterstore testres 2 test1 test2
(integer) 2
127.0.0.1:6379> zrange testres 0 -1 withscores
1) "a"
2) "11"
3) "b"
4) "22"
(2)当AGGREGATE 是MIN时,destination 键中元素的分数是每个参与计算的集合中该元素分数的最小值。如:
127.0.0.1:6379> zinterstore testres2 2 test1 test2 aggregate min
(integer) 2
127.0.0.1:6379> zrange testres2 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "2"
(3)当AGGREGATE 是MAX时,destination 键中元素的分数是每个参与计算的集合中该元素分数的最大值。如:
127.0.0.1:6379> zinterstore testres3 2 test1 test2 aggregate max
(integer) 2
127.0.0.1:6379> zrange testres3 0 -1 withscores
1) "a"
2) "10"
3) "b"
4) "20"
ZINTERSTORE 命令还能够通过WEIGHTS参数设置每个集合的权重,每个集合在参与计算时元素的分数会被乘以该集合的权重。如:
127.0.0.1:6379> zinterstore testres4 2 test1 test2 weights 1 0.1
(integer) 2
127.0.0.1:6379> zrange testres4 0 -1 withscores
1) "a"
2) "2"
3) "b"
4) "4"
test1 中元素乘以1,test2中元素乘以0.1
ZUNIONSTORE命令的用法和ZINTERSTORE 一样,是计算集合间的并集。
官方提供
127.0.0.1:6379> help @sorted_set
BZPOPMAX key [key ...] timeout
summary: Remove and return the member with the highest score from one or more sorted sets, or block until one is available
since: 5.0.0
BZPOPMIN key [key ...] timeout
summary: Remove and return the member with the lowest score from one or more sorted sets, or block until one is available
since: 5.0.0
ZADD key [NX|XX] [GT|LT] [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
ZCOUNT key min max
summary: Count the members in a sorted set with scores within the given values
since: 2.0.0
ZDIFF numkeys key [key ...] [WITHSCORES]
summary: Subtract multiple sorted sets
since: 6.2.0
ZDIFFSTORE destination numkeys key [key ...]
summary: Subtract multiple sorted sets and store the resulting sorted set in a new key
since: 6.2.0
ZINCRBY key increment member
summary: Increment the score of a member in a sorted set
since: 1.2.0
ZINTER numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
summary: Intersect multiple sorted sets
since: 6.2.0
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
ZLEXCOUNT key min max
summary: Count the number of members in a sorted set between a given lexicographical range
since: 2.8.9
ZMSCORE key member [member ...]
summary: Get the score associated with the given members in a sorted set
since: 6.2.0
ZPOPMAX key [count]
summary: Remove and return members with the highest scores in a sorted set
since: 5.0.0
ZPOPMIN key [count]
summary: Remove and return members with the lowest scores in a sorted set
since: 5.0.0
ZRANDMEMBER key [count [WITHSCORES]]
summary: Get one or multiple random elements from a sorted set
since: 6.2.0
ZRANGE key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
summary: Return a range of members in a sorted set
since: 1.2.0
ZRANGEBYLEX key min max [LIMIT offset count]
summary: Return a range of members in a sorted set, by lexicographical range
since: 2.8.9
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
summary: Return a range of members in a sorted set, by score
since: 1.0.5
ZRANGESTORE dst src min max [BYSCORE|BYLEX] [REV] [LIMIT offset count]
summary: Store a range of members from sorted set into another key
since: 6.2.0
ZRANK key member
summary: Determine the index of a member in a sorted set
since: 2.0.0
ZREM key member [member ...]
summary: Remove one or more members from a sorted set
since: 1.2.0
ZREMRANGEBYLEX key min max
summary: Remove all members in a sorted set between the given lexicographical range
since: 2.8.9
ZREMRANGEBYRANK key start stop
summary: Remove all members in a sorted set within the given indexes
since: 2.0.0
ZREMRANGEBYSCORE key min max
summary: Remove all members in a sorted set within the given scores
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
ZREVRANGEBYLEX key max min [LIMIT offset count]
summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.
since: 2.8.9
ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
summary: Return a range of members in a sorted set, by score, with scores ordered from high to low
since: 2.2.0
ZREVRANK key member
summary: Determine the index of a member in a sorted set, with scores ordered from high to low
since: 2.0.0
ZSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate sorted sets elements and associated scores
since: 2.8.0
ZSCORE key member
summary: Get the score associated with the given member in a sorted set
since: 1.2.0
ZUNION numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
summary: Add multiple sorted sets
since: 6.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
author:su1573
鄙人记录生活点滴,学习并分享,请多指教!!!
如需交流,请联系 sph1573@163.com,鄙人看到会及时回复
最后
以上就是轻松黑夜为你收集整理的Redis(七):基础篇 - 有序集合类型有序集合类型(Sorted Set)的全部内容,希望文章能够帮你解决Redis(七):基础篇 - 有序集合类型有序集合类型(Sorted Set)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复