Redis ordered set (sorted set)
Redis ordered set, like a set, is also 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 scores to sort the members of the collection from small to large.
The members of an ordered set are unique, but the 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 collection is 232 - 1 (4294967295, each collection can store more than 4 billion members).
Example
redis 127.0.0.1:6379> ZADD w3ckey 1 redis (integer) 1 redis 127.0.0.1:6379> ZADD w3ckey 2 mongodb (integer) 1 redis 127.0.0.1:6379> ZADD w3ckey 3 mysql (integer) 1 redis 127.0.0.1:6379> ZADD w3ckey 3 mysql (integer) 0 redis 127.0.0.1:6379> ZADD w3ckey 4 mysql (integer) 0 redis 127.0.0.1:6379> ZRANGE w3ckey 0 10 WITHSCORES 1) "redis" 2) "1" 3) "mongodb" 4) "2" 5) "mysql" 6) "4"
In the above example, we added three values to the ordered collection of redis and associated the score through the command ZADD.
Redis ordered set commands
The following table lists the basic commands of redis ordered set:
Serial number | Command and description |
---|---|
1 | ZADD key score1 member1 [score2 member2] Add one or more members to the ordered set, or update the score of an existing member |
2 | ZCARD key Get the members of the ordered set Number |
3 | ZCOUNT key min max Calculate the number of members with a specified interval score in the ordered set |
4 | ZINCRBY key increment member The score of the specified member in the ordered set plus the increment |
ZINTERSTORE destination numkeys key [key ...] | Calculate the intersection of one or more given ordered sets and store the result set in the new ordered set key |
ZLEXCOUNT key min max | Calculate the number of members in the specified dictionary range in an ordered set |
ZRANGE key start stop [WITHSCORES ] | Return an ordered set through the index range to form members in the specified range |
ZRANGEBYLEX key min max [LIMIT offset count] | Through dictionary The interval returns the members of the ordered set |
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT] | Returns the members in the specified interval of the ordered set through scores |
ZRANK key member | Returns the index of the specified member in the ordered set |
ZREM key member [member ...] | Remove one or more members from the ordered set |
ZREMRANGEBYLEX key min max | Remove all members of the given dictionary range from the ordered set |
ZREMRANGEBYRANK key start stop | Remove the given dictionary range from the ordered set All members of a given ranking interval |
ZREMRANGEBYSCORE key min max | Remove all members of a given score interval from the ordered set |
ZREVRANGE key start stop [WITHSCORES] | Returns the members in the specified interval in the ordered set, through the index, the score is from high to low |
ZREVRANGEBYSCORE key max min [WITHSCORES] | Returns the members within the specified score range in the ordered set, sorted from high to low |
ZREVRANK key member | Returns the ranking of the specified member in the ordered set. The members of the ordered set are sorted by decreasing score (from large to small) |
ZSCORE key member | Returns the score value of the member in the ordered set |
ZUNIONSTORE destination numkeys key [key ... ] | Calculate the union of one or more given ordered sets and store it in the new key|
ZSCAN key cursor [MATCH pattern] [COUNT count] | Iterate over the elements in the ordered set (including element members and element scores)