Sorted Sets zset
Sorted Sets There cannot . Unlike the index subscript of the list, the sorting is based on the score, and the score can be repeated . Provides functions such as obtaining specified .
data structure | Whether to allow duplicate elements | Is it in order | orderly implementation | Application scenarios |
list | Yes | Yes | index subscript | Timeline, message queue, etc. |
gather | no | no | without | hashtags, social, etc. |
sorted set | no | Yes | Fraction | Leaderboards, social, etc. |
Command
1, within the collection
[list]
zadd key score member [score member ...]
zadd has nx, xx, ch, and incr options:
nx: for adding, member must not exist
xx: for updating, member must exist
ch: after returning the zadd operation, the number of ordered set elements and scores that have changed
incr: Increase the score, which is equivalent to
the time complexity of the zincrby command zadd is O(log(n)), while the sadd time complexity is O(1)
zcard key
zscore key member
zrank key member zrevrank key member
zrank is the rank of returned members from low to high score, zrevrank is vice versa
zrem key member [member ...]
zincrby key increment member
zrange key start end [withscores] zrevrange key start end [withscores]
Plus withscores will also return the member's score
zrangebyscore key min max [withscores] [limit offset count] zrevrangebyscore key min max [withscores] [limit offset count]
withscores will also return the score of the member
[limit offset count] limits the starting position and number of outputs
min and max support open intervals (parentheses) and closed intervals (square brackets), -inf and +inf represent infinitely small and infinite respectively Big
zcount key min max
zremrangebyrank key start end
zremrangebyscore key min max
[/list]
2. Between sets
[list]
zinterstore destination numkeys key [key...] [weights weight [weight ...]] [aggregate sum|min|max]
destination: the key to save the result of the intersection calculation
numkeys: the number of keys to be used for the intersection calculation
key [key ...]: the key to be calculated for the intersection
weights weight [weight ...]: the weight of each key, the default is 1 , when calculating, each member in the key will multiply the score by the weight, and each weight corresponds to each key above
aggregate sum|min|max: After calculating the intersection of members, the score can be aggregated according to sum, min, max, Default is sum
zunionstore destination numkeys key [key ...] [weights weight [weight ...]] [aggreagate sum|max|min]
[/list]
Internal coding
- ziplist: Compressed list, when the number of elements in the ordered set is less than the zset-max-ziplist-entries configuration (default 128), and the value of each element is less than the zset-max-ziplist-value configuration (default 64 bytes) , Redis will use ziplist as the internal implementation of ordered collection, which can effectively reduce memory usage
- skiplist: skip list. When the ziplist condition is not satisfied, skiplist is used as the internal implementation, because the read and write efficiency of ziplist will decrease at this time.
The ordered set of usage scenarios
is mainly used in the ranking system, which can be ranked according to different maintenance, such as time, number of plays, number of likes, etc.
For example, for the number of likes, record the ranking list of videos uploaded by users every day, including the following functions:
[list]
zadd user:ranking:2017_07_07 william 3 zincreby user:ranking:2017_07_07 william 1
zrem user:ranking:2017_07_07 william
zrevrangebyrank user:ranking:2017_07_07 0 9
zscore user:ranking:2017_07_07 william zrank user:ranking:2017_07_07 william
[/list]