Redis development and operation and maintenance reading notes eighth

Chapter 2 Understanding and Using APIs Part 6

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]
  • Add members
  • 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)
  • count the number of members
  • zcard key
  • Calculate a member's score
  • zscore key member
  • Calculate member's rank
  • zrank key member
    zrevrank key member

    zrank is the rank of returned members from low to high score, zrevrank is vice versa
  • remove member
  • zrem key member [member ...]
  • Increase member's score
  • zincrby key increment member
  • Returns members of the specified rank range
  • zrange key start end [withscores]
    zrevrange key start end [withscores]

    Plus withscores will also return the member's score
  • Returns members of the specified score range
  • 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
  • Returns the number of members in the specified score range
  • zcount key min max
  • removes elements in ascending order within the specified rank
  • zremrangebyrank key start end
  • Removes members of the specified score range
  • zremrangebyscore key min max

    [/list]
    2. Between sets
    [list]
  • intersection
  • 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
  • union
  • 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]
  • Add user likes
  • Recorded by zadd and zincrby, william uploads a video that gets 3 likes and then 1 like
    zadd user:ranking:2017_07_07 william 3
    zincreby user:ranking:2017_07_07 william 1
    
  • Cancel user likes
  • Remove user william from the list
    zrem user:ranking:2017_07_07 william
  • Show the 10 users with the most likes
  • zrevrangebyrank user:ranking:2017_07_07 0 9
  • Display user score
  • zscore user:ranking:2017_07_07 william
    zrank user:ranking:2017_07_07 william

    [/list]

    Guess you like

    Origin http://43.154.161.224:23101/article/api/json?id=326217001&siteId=291194637