Redis Sorted Set

Redis sorted sets, like sets, are collections of string-type elements, and duplicate members are not allowed.

The difference is that each element is associated with a double-type score. Redis uses the score to sort the members in the set from smallest to largest.

The members of a sorted set are unique, but scores can be repeated.

Sets are implemented through hash tables, so the complexity of adding, deleting, and searching is O(1). The maximum number of members in a set is 232- 1 (4294967295, each set can store more than 4 billion members).

Example

redis 127.0.0.1:6379> ZADD examplekey 1 redis
(integer) 1
redis 127.0.0.1:6379> ZADD examplekey 2 mongodb
(integer) 1
redis 127.0.0.1:6379> ZADD examplekey 3 mysql
(integer) 1
redis 127.0.0.1:6379> ZADD examplekey 3 mysql
(integer) 0
redis 127.0.0.1:6379> ZADD examplekey 4 mysql
(integer) 0
redis 127.0.0.1:6379> ZRANGE examplekey 0 10 WITHSCORES

1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"

In the above example, we used the commandZADDto add three values to the Redis sorted set and associate them with scores.


Redis Sorted Set Commands

The following table lists the basic commands of Redis sorted sets:

No.Command and Description
1ZADD key score1 member1 [score2 member2]
Add one or more members to a sorted set, or update the scores of existing members.
2ZCARD key
Get the number of members in a sorted set.
3ZCOUNT key min max
Count the number of members in a sorted set whose scores are within the specified range.
4ZINCRBY key increment member
Add an increment to the score of a specified member in a sorted set.
5ZINTERSTORE destination numkeys key [key ...]
Compute the intersection of one or more given sorted sets and store the result set in a new sorted set named destination.
6ZLEXCOUNT key min max
Count the number of members in a sorted set within the specified lexicographical range.
7ZRANGE key start stop [WITHSCORES]
Return members within the specified range of a sorted set by index range.
8ZRANGEBYLEX key min max [LIMIT offset count]
Return members of a sorted set by lexicographical range.
9ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]
Return members within the specified score range of a sorted set.
10ZRANK key member
Return the index of a specified member in a sorted set.
11ZREM key member [member ...]
Remove one or more members from a sorted set.
12ZREMRANGEBYLEX key min max
Remove all members from a sorted set within the given lexicographical range.
13ZREMRANGEBYRANK key start stop
Remove all members from a sorted set within the given rank range.
14ZREMRANGEBYSCORE key min max
Remove all members from a sorted set within the given score range.
15ZREVRANGE key start stop [WITHSCORES]
Return members within the specified range of a sorted set, by index, with scores from high to low.
16ZREVRANGEBYSCORE key max min [WITHSCORES]
Return members within the specified score range of a sorted set, sorted by scores from high to low.
17ZREVRANK key member
Return the rank of a specified member in a sorted set, with the sorted set members sorted by score value in descending order (from largest to smallest).
18ZSCORE key member
Return the score value of a member in a sorted set.
19ZUNIONSTORE destination numkeys key [key ...]
Compute the union of one or more given sorted sets and store it in a new key.
20ZSCAN key cursor [MATCH pattern] [COUNT count]
Iterate over elements in a sorted set (including element members and element scores).
Other Extensions