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