Home  >  Article  >  Database  >  Redis ordered collection uses knowledge point induction

Redis ordered collection uses knowledge point induction

WBOY
WBOYforward
2022-07-01 12:25:261971browse

This article brings you relevant knowledge about Redis, which mainly organizes the related issues of ordered sets. In ordered sets, each element consists of a member and a member It consists of associated scores. The members are stored as strings and the scores are stored as 64-bit double-precision floating point numbers. Let’s take a look at it together. I hope it will be helpful to everyone.

Redis ordered collection uses knowledge point induction

Recommended learning: Redis video tutorial

Redis data structure: ordered set

Yes sequenced collection. Each element consists of a member and a score associated with the member. The member is stored as a string and the score is stored as a 64-bit double-precision floating point number. Members cannot be repeated and are sorted by score. If they have the same score, they are sorted in dictionary order.

Data structure

  • String
  • Hash
  • List
  • Collection
  • Sorted collection
  • HyperLogLog
  • Bitmap
  • Geographic coordinates
  • Stream

Ordered collection (sorted set)

  • Add or update members
    1.zadd command Format: zadd key [NX|XX] [GT|LT] [CH] [INCR] score member [score member …]
    Returns the number of successfully added new members. If the member score is updated, 0 is returned.
    zadd key score member [score member ...]
    Redis ordered collection uses knowledge point induction
    The function of the XX option is to only update without adding, and return 0 after execution.
    zadd key XX score member [score member ...]
    Redis ordered collection uses knowledge point induction
    The function of the NX option is to only add but not update. If the execution is successful, the number of added elements will be returned.
    zadd key NX score member [score member ...]
    Redis ordered collection uses knowledge point induction
    The CH option is to return the number of modified members rather than the number of successfully added members. Modifications include new additions.
    zadd key CH score member [score member ...]
    Redis ordered collection uses knowledge point induction

  • ##Remove specified member

    1.zrem command Format: zrem key member [member …]
    Returns the number of removed members. If the member does not exist in the set, it is automatically ignored.

    zrem key member [member ...]
    Redis ordered collection uses knowledge point induction 2.zremrangebyrank command Format: zremrangebyrank key start stop
    Remove members within the specified ranking range and return The number of removed members, the ranking can use positive ranking or negative ranking.

    zremrangebyrank key start stop
    Redis ordered collection uses knowledge point induction 3.zremrangebyscore command Format: zremrangebyscore key min max
    Remove members within the specified score range and return the removed members quantity. Adding "(" before min or max means taking an open interval that does not include boundary values.

    zremrangebyscore key min max
    Redis ordered collection uses knowledge point induction 4. zremrangebylex command Format: zremrangebylex key min max
    For an ordered set arranged in lexicographic order (that is, with the same score), remove members within the specified lexicographic range. Return the number of removed members, min and max
    can take values ​​ Includes: Values ​​with "[" indicate that they include lexicographic boundaries, "(" indicates that they do not contain lexicographic boundaries, " " indicates infinity, and "-" indicates infinitesimal.
    zremrangebylex key min max
    Redis ordered collection uses knowledge point induction

  • Pop up elements
    1.zpopmax command Format: zpopmax key [count]
    Pop up the count members with the highest score. When there are multiple elements with the highest score, pop up the one with the largest lexicographic order. members, if count is not specified, the default is 1. After the execution is completed, the members and scores of the popped elements will be returned.
    zpopmax key [count]
    Redis ordered collection uses knowledge point induction
    2.zpopmin command Format: zpopmin key [count]
    Pops up the count members with the lowest score, and the lowest score is When there are multiple elements, the member with the smallest lexicographic order is popped. If count is not specified, the default is 1. After the execution is completed, the members and scores of the popped elements are returned.
    zpopmin key [count]
    3.bzpopmax command Format: bzpopmax key [key …] timeout
    Blocking zpopmax command, timeout is second-level precision. The command checks the given ordered set in turn and pops the element with the largest score from the first non-empty set. Otherwise, the current client is blocked until there is an element or nil is returned after the timeout period is exceeded. When an element is successfully popped, a list will be returned, including the ordered set, members, and scores of the popped element.
    bzpopmax key [key ...] timeout
    Redis ordered collection uses knowledge point induction
    4.bzpopmin command Format: bzpopmin key [key ...] timeout
    Blocking zpopmin command, timeout Second level accuracy. The command checks the given ordered set in turn and pops the element with the smallest score from the first non-empty set. Otherwise, the current client is blocked until there is an element or nil is returned after the timeout period is exceeded. When an element is successfully popped, a list will be returned, including the ordered set, members, and scores of the popped element.
    bzpopmin key [key ...] timeout
    Redis ordered collection uses knowledge point induction
    Client 2:
    Redis ordered collection uses knowledge point induction

  • Get Member score
    1.zscore command Format: zscore key member
    Get the score of a given member. Returns nil when the collection does not exist or the member does not exist.
    zscore key member
    Redis ordered collection uses knowledge point induction

  • Perform operations on member scores
    1.zincrby command Format: zincrby key increment member
    After execution, the current member score is returned. If the increment is positive, it will increase automatically, otherwise it will decrease. If the key does not exist or the member does not exist, the creation operation will be performed.
    zincrby key increment member
    Redis ordered collection uses knowledge point induction

  • ##Get the number of members

    1.zcard command Format: zcard key
    Return the collection The number of members contained, or 0 if the collection does not exist.

    zcard key
    Redis ordered collection uses knowledge point induction 2.zcount command Format: zcount key min max
    Get the number of members within the specified score range, add "(" before min or max " means taking an open interval that does not include boundary values, the value inf means infinity, -inf means infinitesimal.

    zcount key min max
    Redis ordered collection uses knowledge point induction 3.zlexcount command Format: zlexcount key min max
    For an ordered set arranged in lexicographic order (that is, with the same score), obtain the number of members within the specified lexicographic range.
    Possible values ​​of min and max include: with "[ "The value indicates that it contains lexicographic boundaries, "(" indicates that it does not contain lexicographic boundaries, " " indicates infinity, and "-" indicates infinitesimal.
    zlexcount key min max
    Redis ordered collection uses knowledge point induction

  • Get member ranking

    1.zrank command  Format: zrank key member
    Returns the ascending ranking of members, and returns nil when the set or member does not exist.

    zrank key member
    Redis ordered collection uses knowledge point induction 2.zrevrank command Format: zrevrank key member
    Returns the descending ranking of members, and returns nil when the collection or member does not exist.

    zrevrank key member
    Redis ordered collection uses knowledge point induction

  • Get members
    1.zrange command Format: zrange key min max [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
    Get members within the specified index range , sorted in ascending order by score. The WITHSCORES option can return the score after the command is executed. The odd digits in the returned list are members, and the even digits are the corresponding scores. Returns empty if the collection does not exist.
    zrange key min max [WITHSCORES]
    Redis ordered collection uses knowledge point induction
    2.zrevrange command Format: zrevrange key start stop [WITHSCORES]
    Get members within the specified index range, press Sorted in descending order of scores. The WITHSCORES option can return the score after the command is executed. The odd digits in the returned list are members, and the even digits are the corresponding scores. Returns empty if the collection does not exist.
    zrevrange key start stop [WITHSCORES]
    Redis ordered collection uses knowledge point induction
    3.zrangebyscore command Format: zrangebyscore key min max [WITHSCORES] [LIMIT offset count]
    Get the specified score Members within the range are arranged in ascending order by score. The WITHSCORES option can return the score after the command is executed. The odd digits in the returned list are members, and the even digits are the corresponding scores. Returns empty if the collection does not exist.
    zrangebyscore key min max [WITHSCORES] [LIMIT offset count]
    Redis ordered collection uses knowledge point induction
    The [LIMIT offset count] option is used to limit the number of returned members. The offset specification needs to be skipped. The number of members, count is executed to return the maximum number of members, and the absolute value is taken when count is negative.
    Redis ordered collection uses knowledge point induction
    Adding "(" before min or max means taking an open interval that does not include boundary values. The value inf means infinite, and -inf means infinitesimal.
    Redis ordered collection uses knowledge point induction
    4.zrevrangebyscore command Format: zrevrangebyscore key max min [WITHSCORES] [LIMIT offset count]
    Get the members within the specified score range, sort them in descending order by score, pay attention to the max min order.
    zrevrangebyscore key max min [WITHSCORES ] [LIMIT offset count]
    Redis ordered collection uses knowledge point induction
    5.zrangebylex command Format: zrangebylex key min max [LIMIT offset count]
    When the member scores are the same, get Specify members within the lexicographic range, sorted in ascending lexicographic order. Possible values ​​for min and max include: Values ​​with "[" indicate that they include lexicographic boundaries, and "(" indicates that they do not include lexicographic boundaries. , " " means infinite, "-" means infinitesimal.
    zrangebylex key min max [LIMIT offset count]
    Redis ordered collection uses knowledge point induction
    6. zrevrangebylex command Format: zrevrangebylex key max min [ LIMIT offset count]
    When the member scores are the same, get the members within the specified dictionary range and sort them in descending dictionary order. The available values ​​of max and min include: The value of "[" indicates that it contains lexicographic boundaries, "(" indicates that it does not contain lexicographic boundaries, " " indicates infinity, and "-" indicates infinitesimal.
    zrevrangebylex key max min [LIMIT offset count]

  • Set operation
    1. Union
    1) zunionstore command ˆ Format: zunionstore destination numkeys key [key …] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
    numkeys is the number of sets participating in the operation. If it does not correspond, an error is returned. After execution, the number of members of the new set is returned. The union result is a member set. If there are the same members in the sets participating in the operation, the default member score of the new set is the sum of the scores of the original set members.
    zunionstore destination numkeys key [key ...]
    Redis ordered collection uses knowledge point induction
    The AGGREGATE option is used to specify the aggregate function to be used. If not specified, the default is SUM. SUM refers to adding up the scores, MIN refers to taking the minimum score, and MAX refers to taking the maximum score.
    zunionstore destination numkeys key [key ...] [AGGREGATE SUM|MIN|MAX]
    Redis ordered collection uses knowledge point induction
    The WEIGHTS option is to set one for each collection before performing aggregation Weight, the weight is multiplied by the member score in the set to obtain a new score, and then the aggregation calculation is performed.
    Redis ordered collection uses knowledge point induction
    2) zunion command Format: zunion numkeys key [key …] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
    Returns the new set member after execution is completed.
    zunion numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
    2. Intersection
    1) zinterstore command Format: zinterstore destination numkeys key [key …] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
    Returns the number of members of the new set after execution.
    zinterstore destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
    2)zinter command Format: zinter numkeys key [key ...] [WEIGHTS weight ] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
    Returns the new collection member after execution.
    zinter numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
    Note:
    1. Collection The operation can use a set as input. By default, the score of the set member is regarded as 1 and participates in the operation. You can use the WEIGHTS option to set the weight to change the set score.

  • Common usage
    1. Ranking
    2. Timeline

Recommended learning: Redis video tutorial

The above is the detailed content of Redis ordered collection uses knowledge point induction. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete