Redis 有序集合和集合一样也是 string 类型元素的集合,且不允许重复的成员。
不同的是每个元素都会关联一个 double 类型的分数。redis 正是通过分数来为集合中的成员进行从小到大的排序。
有序集合的成员是唯一的,但分数(score)却可以重复。
集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。 集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。
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
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
summary: Add one or more members to a sorted set, or update its score if it already exists
since: 1.2.0
summary: Get the number of members in a sorted set
since: 1.2.0
summary: Count the members in a sorted set with scores within the given values
since: 2.0.0
summary: Subtract multiple sorted sets
since: 6.2.0
summary: Subtract multiple sorted sets and store the resulting sorted set in a new key
since: 6.2.0
summary: Increment the score of a member in a sorted set
since: 1.2.0
summary: Intersect multiple sorted sets
since: 6.2.0
summary: Intersect multiple sorted sets and store the resulting sorted set in a new key
since: 2.0.0
summary: Count the number of members in a sorted set between a given lexicographical range
since: 2.8.9
summary: Get the score associated with the given members in a sorted set
since: 6.2.0
summary: Remove and return members with the highest scores in a sorted set
since: 5.0.0
summary: Remove and return members with the lowest scores in a sorted set
since: 5.0.0
summary: Get one or multiple random elements from a sorted set
since: 6.2.0
summary: Return a range of members in a sorted set
since: 1.2.0
summary: Return a range of members in a sorted set, by lexicographical range
since: 2.8.9
summary: Return a range of members in a sorted set, by score
since: 1.0.5
summary: Store a range of members from sorted set into another key
since: 6.2.0
summary: Determine the index of a member in a sorted set
since: 2.0.0
summary: Remove one or more members from a sorted set
since: 1.2.0
summary: Remove all members in a sorted set between the given lexicographical range
since: 2.8.9
summary: Remove all members in a sorted set within the given indexes
since: 2.0.0
summary: Remove all members in a sorted set within the given scores
since: 1.2.0
summary: Return a range of members in a sorted set, by index, with scores ordered from high to low
since: 1.2.0
summary: Return a range of members in a sorted set, by lexicographical range, ordered from higher to lower strings.
since: 2.8.9
summary: Return a range of members in a sorted set, by score, with scores ordered from high to low
28)since: 2.2.0
summary: Determine the index of a member in a sorted set, with scores ordered from high to low
since: 2.0.0
summary: Incrementally iterate sorted sets elements and associated scores
since: 2.8.0
summary: Get the score associated with the given member in a sorted set
since: 1.2.0
summary: Add multiple sorted sets
since: 6.2.0
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的倒序来获取工作任务。让重要的任务优先执行。