数据库
首页 > 数据库> > redis基本数据类型 sorted_set类型

redis基本数据类型 sorted_set类型

作者:互联网

一、特点

        Redis 有序集合和集合一样也是 string 类型元素的集合,且不允许重复的成员。

        不同的是每个元素都会关联一个 double 类型的分数。redis 正是通过分数来为集合中的成员进行从小到大的排序。

        有序集合的成员是唯一的,但分数(score)却可以重复。

        集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。 集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。

二、操作

1)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

 

2)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

 

3)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

 

4)ZCARD key        得到一个排序集合的成员数

summary: Get the number of members in a sorted set
since: 1.2.0

 

5)ZCOUNT key min max      对分数在给定值内的排序集合中的成员进行计数  

summary: Count the members in a sorted set with scores within the given values
since: 2.0.0

 

6)ZDIFF numkeys key [key ...] [WITHSCORES] 求差集

summary: Subtract multiple sorted sets
since: 6.2.0

7)ZDIFFSTORE destination numkeys key [key ...] 求差集并将结果放入另一个集合中

summary: Subtract multiple sorted sets and store the resulting sorted set in a new key
since: 6.2.0

8)ZINCRBY key increment member        递增已排序集合中成员的分数

summary: Increment the score of a member in a sorted set
since: 1.2.0

9)ZINTER numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]        求交集

summary: Intersect multiple sorted sets

since: 6.2.0

10)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

11)ZLEXCOUNT key min max        计算在给定的字典范围之间的排序集合中的成员数

summary: Count the number of members in a sorted set between a given lexicographical range
since: 2.8.9

12)ZMSCORE key member [member ...]        获取一个排序集合中与给定成员相关联的分数

summary: Get the score associated with the given members in a sorted set
since: 6.2.0

13)ZPOPMAX key [count]        移除并返回排序集合中得分最高的成员

summary: Remove and return members with the highest scores in a sorted set
since: 5.0.0

14)ZPOPMIN key [count]          移除并返回排序集合中得分最低的成员

summary: Remove and return members with the lowest scores in a sorted set
since: 5.0.0

15)ZRANDMEMBER key [count [WITHSCORES]]        从一个排序集合中获取一个或多个随机元素

summary: Get one or multiple random elements from a sorted set
since: 6.2.0

16)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

17)ZRANGEBYLEX key min max [LIMIT offset count]            按字典序范围返回已排序集合中的成员范围   

summary: Return a range of members in a sorted set, by lexicographical range
since: 2.8.9

18)ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]    按分数返回已排序集合中的成员范围    

summary: Return a range of members in a sorted set, by score
since: 1.0.5

19)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

20)ZRANK key member        确定已排序集合中成员的索引

summary: Determine the index of a member in a sorted set
since: 2.0.0

21)ZREM key member [member ...]        从已排序集合中移除一个或多个成员

summary: Remove one or more members from a sorted set
since: 1.2.0

22)ZREMRANGEBYLEX key min max        移除已排序集合中给定词序范围之间的所有成员

summary: Remove all members in a sorted set between the given lexicographical range
since: 2.8.9

23)ZREMRANGEBYRANK key start stop        移除给定索引内已排序集合中的所有成员

summary: Remove all members in a sorted set within the given indexes
since: 2.0.0

24)ZREMRANGEBYSCORE key min max        移除给定分数内已排序集合中的所有成员

summary: Remove all members in a sorted set within the given scores
since: 1.2.0

25)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

26)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

27)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
28)since: 2.2.0

29)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

30)ZSCAN key cursor [MATCH pattern] [COUNT count]        递增迭代排序的集合元素和相关的分数

summary: Incrementally iterate sorted sets elements and associated scores
since: 2.8.0

31)ZSCORE key member        获取与已排序集合中给定成员相关联的分数

summary: Get the score associated with the given member in a sorted set
since: 1.2.0

32)ZUNION numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]        添加多个排序集

summary: Add multiple sorted sets
since: 6.2.0

33)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

三、应用场景

1、   排行榜:有序集合经典使用场景。例如视频网站需要对用户上传的视频做排行榜,榜单维护可能是多方面:按照时间、按照播放量、按照获得的赞数等。

2、用Sorted Sets来做带权重的队列,比如普通消息的score为1,重要消息的score为2,然后工作线程可以选择按score的倒序来获取工作任务。让重要的任务优先执行。

标签:set,数据类型,since,redis,summary,key,sorted,排序
来源: https://blog.csdn.net/codelmh/article/details/121888049