Redis Sorted Set
A Redis sorted set, like a set, is a collection of string-type elements and does not allow duplicate members.
The difference is that each element is associated with a double-type score. Redis uses the score to sort the members of the set from smallest to largest.
Sorted set members are unique, but scores can be duplicate.
A set is implemented via a hash table, so the complexity of add, delete, and lookup operations is O(1). The maximum number of members in a set is 2^32 - 1 (4294967295, each set can store over 4 billion members).
Example
redis 127.0.0.1:6379> ZADD runoobkey 1 redis
(integer) 1
redis 127.0.0.1:6379> ZADD runoobkey 2 mongodb
(integer) 1
redis 127.0.0.1:6379> ZADD runoobkey 3 mysql
(integer) 1
redis 127.0.0.1:6379> ZADD runoobkey 3 mysql
(integer) 0
redis 127.0.0.1:6379> ZADD runoobkey 4 mysql
(integer) 0
redis 127.0.0.1:6379> ZRANGE runoobkey 0 10 WITHSCORES
1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"
In the example above we used the command ZADD to add three values to the redis sorted set and associate them with scores.
Redis Sorted Set Commands
The table below lists the basic redis sorted set commands:
| No. | Command and Description |
|---|---|
| 1 | ZADD key score1 member1 [score2 member2] |
| Add one or more members to the sorted set, or update the score of existing members | |
| 2 | ZCARD key |
| Get the number of members in the sorted set | |
| 3 | ZCOUNT key min max |
| Count the number of members in the sorted set within the specified score range | |
| 4 | ZINCRBY key increment member |
| Add the increment to the score of the specified member in the sorted set | |
| 5 | ZINTERSTORE destination numkeys key [key ...] |
| Compute the intersection of the given one or more sorted sets and store the result in the new sorted set destination | |
| 6 | ZLEXCOUNT key min max |
| Count the number of members in the specified lexicographic range in the sorted set | |
| 7 | ZRANGE key start stop [WITHSCORES] |
| Return the members in the specified range of the sorted set by index | |
| 8 | ZRANGEBYLEX key min max [LIMIT offset count] |
| Return the members of the sorted set within the lexicographic range | |
| 9 | ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] |
| Return the members in the specified score range of the sorted set | |
| 10 | ZRANK key member |
| Return the index of the specified member in the sorted set | |
| 11 | ZREM key member [member ...] |
| Remove one or more members from the sorted set | |
| 12 | ZREMRANGEBYLEX key min max |
| Remove all members in the given lexicographic range from the sorted set | |
| 13 | ZREMRANGEBYRANK key start stop |
| Remove all members in the given rank range from the sorted set | |
| 14 | ZREMRANGEBYSCORE key min max |
| Remove all members in the given score range from the sorted set | |
| 15 | ZREVRANGE key start stop [WITHSCORES] |
| Return the members in the specified range of the sorted set by index, with scores from high to low | |
| 16 | ZREVRANGEBYSCORE key max min [WITHSCORES] |
| Return the members within the specified score range of the sorted set, sorted by score from high to low | |
| 17 | ZREVRANK key member |
| Return the rank of the specified member in the sorted set, with members sorted by score in descending order | |
| 18 | ZSCORE key member |
| Return the score of a member in the sorted set | |
| 19 | ZUNIONSTORE destination numkeys key [key ...] |
| Compute the union of the given one or more sorted sets and store it in the new key | |
| 20 | ZSCAN key cursor [MATCH pattern] [COUNT count] |
| Iterate over the elements in the sorted set (including member and score) |