📜  Redis-排序集

📅  最后修改于: 2020-11-26 07:04:09             🧑  作者: Mango


Redis Sorted Sets与Redis Sets相似,具有存储在集合中的值的独特功能。不同之处在于,排序集的每个成员都与一个分数相关联,该分数用于从最小到最大分数中获取排序的排序集。

在Redis排序集中,添加,删除并测试O(1)中成员的存在(恒定时间,无论该集合中包含的元素数量如何)。列表的最大长度为2个32 – 1元件(4294967295,超过4十亿每组元素)。

redis 127.0.0.1:6379> ZADD tutorials 1 redis 
(integer) 1 
redis 127.0.0.1:6379> ZADD tutorials 2 mongodb 
(integer) 1 
redis 127.0.0.1:6379> ZADD tutorials 3 mysql 
(integer) 1 
redis 127.0.0.1:6379> ZADD tutorials 3 mysql 
(integer) 0 
redis 127.0.0.1:6379> ZADD tutorials 4 mysql 
(integer) 0 
redis 127.0.0.1:6379> ZRANGE tutorials 0 10 WITHSCORES  
1) "redis" 
2) "1" 
3) "mongodb" 
4) "2" 
5) "mysql" 
6) "4" 

在上面的示例中,通过命令ZADD将三个带有其分数的值插入到名为“ tutorials”的Redis排序集中。

Redis排序集命令

下表列出了一些与排序集相关的基本命令。

Sr.No Command & Description
1 ZADD key score1 member1 [score2 member2]

Adds one or more members to a sorted set, or updates its score, if it already exists

2 ZCARD key

Gets the number of members in a sorted set

3 ZCOUNT key min max

Counts the members in a sorted set with scores within the given values

4
ZINCRBY key increment member

Increments the score of a member in a sorted set

5 ZINTERSTORE destination numkeys key [key …]

Intersects multiple sorted sets and stores the resulting sorted set in a new key

6 ZLEXCOUNT key min max

Counts the number of members in a sorted set between a given lexicographical range

7 ZRANGE key start stop [WITHSCORES]

Returns a range of members in a sorted set, by index

8 ZRANGEBYLEX key min max [LIMIT offset count]

Returns a range of members in a sorted set, by lexicographical range

9 ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]

Returns a range of members in a sorted set, by score

10 ZRANK key member

Determines the index of a member in a sorted set

11 ZREM key member [member …]

Removes one or more members from a sorted set

12 ZREMRANGEBYLEX key min max

Removes all members in a sorted set between the given lexicographical range

13 ZREMRANGEBYRANK key start stop

Removes all members in a sorted set within the given indexes

14 ZREMRANGEBYSCORE key min max

Removes all members in a sorted set within the given scores

15 ZREVRANGE key start stop [WITHSCORES]

Returns a range of members in a sorted set, by index, with scores ordered from high to low

16 ZREVRANGEBYSCORE key max min [WITHSCORES]

Returns a range of members in a sorted set, by score, with scores ordered from high to low

17 ZREVRANK key member

Determines the index of a member in a sorted set, with scores ordered from high to low

18 ZSCORE key member

Gets the score associated with the given member in a sorted set

19 ZUNIONSTORE destination numkeys key [key …]

Adds multiple sorted sets and stores the resulting sorted set in a new key

20 ZSCAN key cursor [MATCH pattern] [COUNT count]

Incrementally iterates sorted sets elements and associated scores