Operation of REDIS data storage types directly on a Redis-installed Linux machine-for sorted-sets operations

Source: Internet
Author: User

I. Overview:

    Sorted Set (sorted set) and Set types are very similar, they are a collection of strings, and do not allow duplicate members to appear in a Set. The main difference between them is that each member of the Sorted Set will have a score associated with it. Redis sorts the members of the set from small to large by the score. However, it needs to be pointed out that although the members in the Sorted Set must be unique, the scores are repeatable.
    Adding, deleting, or updating a member in a Sorted Set is a very fast operation, and its time complexity is the logarithm of the number of members in the set. Since the members of a Sorted Set are in an ordered position in the set, even accessing members in the middle of the set is still very efficient. In fact, this feature of Redis is difficult to achieve in many other types of databases. In other words, to achieve the same efficiency as Redis at this point, modeling in other databases is very difficult of.

Related commands

1.Add elements

Command prototype: ZADD key score member [score] [member]

Time complexity: O (log (N))

Command description: N in time complexity represents the number of members in the Sorted Set. Add all members and their scores specified in the parameter to the Sorted Set of the specified key. In this command, we can specify multiple sets of score / member as parameters. If a member in the parameter already exists at the time of addition, the command will update the member's score to a new value, and then reorder the member based on the new value. If the key does not exist, the command will create a new Sorted Set Value for the key and insert a score / member pair into it. If the key already exists, but the Value associated with it is not a Sorted Set type, the relevant error message will be returned.

Return value: The number of members actually inserted in this operation.

2.Get the number of elements in the collection

Command prototype: ZCARD key

Time complexity: O (1)

Command description: Get the number of members contained in the Sorted Set associated with this Key.

Return value: Returns the number of members in the Sorted Set, or 0 if the key does not exist.

3.Get the number of elements in the specified score range

Command prototype: ZCOUNT key min max

Time complexity: O (log (N) + M)

Command description: N in time complexity represents the number of members in Sorted-Sets, and M represents the number of elements between min and max. This command is used to get the number of members whose score is between min and max. For the min and max parameters, it is necessary to specify that -inf and + inf represent the highest and lowest scores in Sorted-Sets, respectively. By default, the range represented by min and max is a closed interval range, that is, members within min <= score <= max will be returned. However, we can indicate the open interval by adding "(" characters in front of min and max, such as (min max means min <score <= max, and (min (max means min <score <max.

Returns: the number of members in the specified range of the score.

4.Get the score of the element

Command prototype: ZSCORE key member

Time complexity: O (1)

Command description: Get the score of the specified member of the specified Key.

Return value: If the member exists, its score is returned as a string, otherwise nil.

5.Increase the score of an element

Command prototype: ZINCRBY key increment member

Time complexity: O (log (N))

Command description: N in time complexity represents the number of members in the Sorted Set. This command will increase the specified score for the specified member in the specified Key. If the member does not exist, the command adds the member and assumes its initial score is 0, after which its score is added to increment. If the key does not exist, the command will create the key and its associated Sorted Set, and include the members specified by the parameters, whose score is the increment parameter. If the Key is not associated with the Sorted Set type, the related error message will be returned.

Returns: the new score as a string.

Get a list of elements ranked in a range

Command prototype: ZRANGE key start stop [WITHSCORES]

Time complexity: O (log (N) + M)

Command description: N in the time complexity represents the number of members in the Sorted Set, and M represents the number of members returned. This command returns members whose order is within the range specified by the parameters start and stop. Here, the start and stop parameters are both 0-based, that is, 0 is the first member, and -1 is the last member. If start is greater than the maximum index value in the Sorted Set, or start> stop, an empty set will be returned. If stop is greater than the maximum index value, the command returns from start to the last member of the collection. If the command has the optional WITHSCORES option, the command will include the score value of each member in the returned result, such as value1, score1, value2, score2 ....

Return value: Returns a list of members whose indexes are between start and stop.

7. Get a list of elements ranked in a certain range (element scores are sorted from large to small)

Command prototype: ZREVRANGE key start stop [WITHSCORES]

Time complexity: O (log (N) + M)

Command description: N in the time complexity represents the number of members in the Sorted Set, and M represents the number of members returned. The function of this command is basically the same as ZRANGE. The only difference is that the command is to obtain the members at the specified position by reverse order, that is, from high to low. If members have the same score, they are sorted in descending lexicographic order.

Return value: Returns the specified member list.

8, get the elements of the specified score range

Command prototype: ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

Time complexity: O (log (N) + M)

Command description: N in the time complexity represents the number of members in the Sorted Set, and M represents the number of members returned. This command will return all members with scores between min and max, that is, members that satisfy the expression min <= score <= max, where the returned members are returned in descending order of their scores, if members have the same Scores are returned in lexicographic order of members. The optional parameter LIMIT is used to limit the number of returned members. The optional parameter offset means to return from the offset member that meets the conditions, and return count members at the same time. For the meaning of the optional parameter WITHSCORES, refer to the description of the option in ZRANGE. Finally, it should be noted that the rules of min and max in the parameters can refer to the command ZCOUNT.

Return value: Returns a list of members with scores within the specified range.

9, get the elements of the specified score range (element scores are sorted from large to small)

Command prototype: ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]

Time complexity: O (log (N) + M)

Command description: N in the time complexity represents the number of members in the Sorted Set, and M represents the number of members returned. This command has the same function as ZRANGEBYSCORE except that the ordering method is based on high-to-low scores. It should be noted that the order of the min and max parameters in this command is opposite to that of the ZRANGEBYSCORE command.

Return value: Returns a list of members with scores within the specified range.

10.Get the ranking of elements

Command prototype: ZRANK key member

Time complexity: O (log (N))

Command description: N in time complexity represents the number of members in the Sorted Set. The members in the Sorted Set are stored in descending order of score. This command returns the position value of the specified member in the parameter, where 0 represents the first member and it is the member with the lowest score in the Sorted Set.

Return value: If the member exists, its position index is returned. Otherwise, it returns nil.

11, get the ranking of the elements (element scores are sorted from large to small)

Command prototype: ZREVRANK key member

Time complexity: O (log (N))

Command description: N in time complexity represents the number of members in the Sorted Set. The function of this command is basically the same as ZRANK. The only difference is that the index obtained by this command is the position after being sorted from high to low. Similarly, 0 indicates the first element, which is the member with the highest score.

Return value: If the member exists, its position index is returned. Otherwise, it returns nil.

Delete one or more elements

Command prototype: ZREM key member [member ...]

Time complexity: O (M log (N))

Command description: In time complexity, N represents the number of members in the Sorted Set, and M represents the number of members deleted. The command removes the members specified in the parameters, and members that do not exist are ignored. If the Value associated with the Key is not a Sorted Set, the corresponding error message will be returned.

Return value: The number of members actually deleted.

13.Remove elements by ranking range

Command prototype: ZREMRANGEBYRANK key start stop

Time complexity: O (log (N) + M)

Command description: N in the time complexity represents the number of members in the Sorted Set, and M represents the number of members deleted. Delete members whose index positions are between start and stop. Both start and stop are 0-based, that is, 0 is the member with the lowest score, and -1 is the last member, that is, the member with the highest score.

Return value: number of members deleted

14.Remove elements by score range

Command prototype: ZREMRANGEBYSCORE key start stop

Time complexity: O (log (N) + M)

Command description: N in the time complexity represents the number of members in the Sorted Set, and M represents the number of members deleted. Delete all members with scores between min and max, that is, all members that satisfy the expression min <= score <= max. For the min and max parameters, it can be expressed in open intervals, and the specific rules refer to ZCOUNT.

Return value: The number of members deleted.

Command example

1. ZADD / ZCARD / ZCOUNT / ZREM / ZINCRBY / ZSCORE / ZRANGE / ZRANK:
    #Start the Redis client tool from the shell's command line.
    /> redis-cli
    #Add a member with a score of 1.
    redis 127.0.0.1:6379> zadd myzset 1 "one"
    (integer) 1
    #Add two members whose scores are 2 and 3.
    redis 127.0.0.1:6379> zadd myzset 2 "two" 3 "three"
    (integer) 2
    # 0 indicates the first member, and -1 indicates the last member. The WITHSCORES option indicates that each member and its score are included in the returned result, otherwise only members are returned.
    redis 127.0.0.1:6379> zrange myzset 0 -1 WITHSCORES
    1) "one"
    twenty one"
    3) "two"
    4) "2"
    5) "three"
    6) "3"
    #Get the position index of member one in Sorted-Set. 0 means the first position.
    redis 127.0.0.1:6379> zrank myzset one
    (integer) 0
    #Memberfour does not exist, so nil is returned.
    redis 127.0.0.1:6379> zrank myzset four
    (nil)
    #Get the number of members in the myzset key.
    redis 127.0.0.1:6379> zcard myzset
    (integer) 3
    #Returns the number of members in the Sorted-Set associated with myzset whose score satisfies the expression 1 <= score <= 2.
    redis 127.0.0.1:6379> zcount myzset 1 2
    (integer) 2
    #Delete members one and two, return the number of members actually deleted.
    redis 127.0.0.1:6379> zrem myzset one two
    (integer) 2
    #Check if the deletion was successful.
    redis 127.0.0.1:6379> zcard myzset
    (integer) 1
    #Get members three scores. The return value is a string.
    redis 127.0.0.1:6379> zscore myzset three
    "3"
    #Since member two has been deleted, this command returns nil.
    redis 127.0.0.1:6379> zscore myzset two
    (nil)
    #Increase the score of member one by 2 and return the updated score of the member.
    redis 127.0.0.1:6379> zincrby myzset 2 one
    "2"
    #Increase the member's score by -1 and return the updated score of the member.
    redis 127.0.0.1:6379> zincrby myzset -1 one
    "1"
    #Check if the member's score has been updated correctly.
    redis 127.0.0.1:6379> zrange myzset 0 -1 WITHSCORES
    1) "one"
    twenty one"
    3) "three"
    4) "3"

 2. ZRANGEBYSCORE / ZREMRANGEBYRANK / ZREMRANGEBYSCORE
    redis 127.0.0.1:6379> del myzset
    (integer) 1
    redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
    (integer) 4
    #Get members whose score satisfies the expression 1 <= score <= 2.
    redis 127.0.0.1:6379> zrangebyscore myzset 1 2
    1) "one"
    2) "two"
    #Get members whose score satisfies the expression 1 <score <= 2.
    redis 127.0.0.1:6379> zrangebyscore myzset (1 2
    1) "two"
    # -inf indicates the first member, + inf indicates the last member, and the parameter after limit is used to limit the members of the returned members.
    # 2 means start with the member whose position index (0-based) is equal to 2, and take the next 3 members.
    redis 127.0.0.1:6379> zrangebyscore myzset -inf + inf limit 2 3
    1) "three"
    2) "four"
    #Delete members whose score satisfies expression 1 <= score <= 2 and return the number of actual deletions.
    redis 127.0.0.1:6379> zremrangebyscore myzset 1 2
    (integer) 2
    #See if the deletion above was successful.
    redis 127.0.0.1:6379> zrange myzset 0 -1
    1) "three"
    2) "four"
    #Delete members whose position index satisfies the expression 0 <= rank <= 1.
    redis 127.0.0.1:6379> zremrangebyrank myzset 0 1
    (integer) 2
    #Check whether the previous command was deleted successfully.
    redis 127.0.0.1:6379> zcard myzset
    (integer) 0

 3. ZREVRANGE / ZREVRANGEBYSCORE / ZREVRANK:
    # Prepare test data for the following examples.
    redis 127.0.0.1:6379> del myzset
    (integer) 0
    redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
    (integer) 4
    #Get and return the members in this interval with the position index from high to low.
    redis 127.0.0.1:6379> zrevrange myzset 0 -1 WITHSCORES
    1) "four"
    twenty four"
    3) "three"
    4) "3"
    5) "two"
    6) "2"
    7) "one"
    8) "1"
    #Since it is sorted from high to low, the position equal to 0 is four, 1 is three, and so on.
    redis 127.0.0.1:6379> zrevrange myzset 1 3
    1) "three"
    2) "two"
    3) "one"
    #Since it is sorted from high to low, the position of one is 3.
    redis 127.0.0.1:6379> zrevrank myzset one
    (integer) 3
    #Since it is sorted from high to low, the position of four is 0.
    redis 127.0.0.1:6379> zrevrank myzset four
    (integer) 0
    #Get members whose score satisfies expression 3> = score> = 0 and output in reverse order, that is, from highest to lowest.
    redis 127.0.0.1:6379> zrevrangebyscore myzset 3 0
    1) "three"
    2) "two"
    3) "one"
    #This command supports the limit option, which has the same meaning as the option in zrangebyscore, but it is calculated and obtained in the reverse order when calculating the position.
    redis 127.0.0.1:6379> zrevrangebyscore myzset 4 0 limit 1 2
    1) "three"
    2) "two"

Application range

     1). A leaderboard that can be used for a large online game. Whenever the player's score changes, you can execute the ZADD command to update the player's score, and then use the ZRANGE command to obtain the user information of the TOP TEN points. Of course, we can also use the ZRANK command to obtain player ranking information by username. Finally, we will use the ZRANGE and ZRANK commands to quickly obtain information about other users who have similar points to a player.
     2). Sorted-Sets type can also be used to build index data.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.