Redis data type -- sets

Source: Internet
Author: User
Tags sorts
-- Reprinted:

I. Overview:

Sorted-sets and sets have similar types. They are both string sets and duplicate members are not allowed to appear in a set. The main difference between them is that each member in sorted-sets has a score associated with it. redis uses scores to sort members in the set from small to large. However, it should be pointed out that although the members in sorted-sets must be unique, scores can be repeated.
Adding, deleting, or updating a member in sorted-set is a fast operation. The time complexity is the logarithm of the number of members in the set. Because sorted-sets members are ordered in the set, it is very efficient to access members in the center of the set. In fact, this feature of redis is hard to implement in many other types of databases. In other words, to achieve the same efficiency as redis, modeling in other databases is very difficult.

Ii. Related command list:

Command prototype Time Complexity Command description Return Value
ZaddKey score member [score] [member] O (log (n )) N in time complexity indicates the number of sorted-sets members. Add all members specified in the parameter and their scores to the sorted-set of the specified key. In this command, we can specify multiple groups of score/member as the parameter. If a member in the parameter already exists at the time of addition, the command updates the score of the member to a new value and sorts the member based on the new value. If the key does not exist, this command creates a new sorted-sets value for the key and inserts the score/member pair into it. If the key already exists, but the value associated with it is not of the sorted-sets type, related error messages will be returned. The number of actually inserted members in this operation.
ZcardKey O (1) Obtain the number of sorted-sets associated with the key. Returns the number of sorted-sets members. If the key does not exist, 0 is returned.
ZcountKey min max O (log (n) + M) In time complexity, N indicates the number of members in sorted-sets, and M indicates the number of elements between min and Max. This command is used to obtain the number of members of a score between min and Max.For Min and Max parameters,-INFAnd+ INFThe highest and lowest scores in sorted-sets.By default, Min and Max indicate a closed range, that isMin <= score <= max. However, we can add"("Character to indicate the open interval, for example, min maxMin <score <= maxAnd (min (max)Min <score <Max. The number of members in the specified range.
ZincrbyKey increment Member O (log (n )) N in time complexity indicates the number of sorted-sets members. This command adds the specified score to the specified Member of the specified key. If the member does not exist, this command adds the Member and assumes that its initial score is 0, and then adds the score to the increment. If the key is not saved, this command creates the key and its associated sorted-sets, and contains the members specified by the parameter. The score is the increment parameter. If the associated key is not of the sorted-sets type, the error message is returned. The new score in the form of a string.
ZrangeKey start stop [withscores] O (log (n) + M) In time complexity, N indicates the number of sorted-set members, and M indicates the number of returned Members. This command returns the members in the specified range of the start and stop parameters. Here, the start and stop parameters are both 0-based, that is, 0 indicates the first member, and-1 indicates the last member. If start is greater than the maximum index value in the sorted-set, or start> stop, an empty set is returned. If the stop value is greater than the maximum index value, the command returns the last Member from start to the set.If the command contains the optional parameter withscores, the returned result contains the score values of each member, such as value1, score1, value2, score2 .... Returns the list of members of the index between start and stop.
ZrangebyscoreKey min max [withscores] [limit offset count] O (log (n) + M) In time complexity, N indicates the number of sorted-set members, and M indicates the number of returned Members. This command returns all the Members whose scores are between min and Max, that is, the members that meet the expression min <= score <= max. The returned Members are returned in the order from low to high, if the members have the same scores, the results are returned in alphabetical order. The optional parameter limit is used to limit the number of returned Members. The optional parameter offset indicates that the return starts from the offset member that meets the condition and returns the Count member. For the meaning of the optional parameter withscores, refer to the description of this option in zrange.Note that the min and Max rules in the parameter can be referred to the zcount command. Returns the list of Members whose scores are in the specified range.
ZrankKey Member O (log (n )) N in time complexity indicates the number of sorted-set members. Members in sorted-set are stored in the order of low to high scores. This command returns the Location Value of the specified member in the parameter, where 0 indicates the first member, it is the member with the lowest score in sorted-set. If this member exists, its location index value is returned. Otherwise, Nil is returned.
ZremKey member [member...] O (m log (n )) In time complexity, N indicates the number of sorted-set members, and M indicates the number of deleted members. This command removes the specified member from the parameter, and the nonexistent member is ignored. If the value associated with the key is not sorted-set, the error message is returned. The actual number of deleted members.
ZrevrangeKey startstop [withscores] O (log (n) + M) In time complexity, N indicates the number of sorted-set members, and M indicates the number of returned Members. The function of this command is basically the same as that of zrange. The only difference is that this command obtains the members at the specified position through reverse sorting, that is, the order from high to low. If the members have the same scores, they are sorted alphabetically in descending order. Returns the list of specified members.
ZrevrankKey Member O (log (n )) N in time complexity indicates the number of sorted-set members. The function of this command is basically the same as that of zrank. The only difference is that the index obtained by this command is the position from high to low. Similarly, 0 indicates the first element, that is, the member with the highest score. If this member exists, its location index value is returned. Otherwise, Nil is returned.
ZscoreKey Member O (1) Obtains the score of a specified member of a specified key. If the member exists, the score is returned as a string; otherwise, Nil is returned.
ZrevrangebyscoreKey Max min [withscores] [limit offset count] O (log (n) + M) In time complexity, N indicates the number of sorted-set members, and M indicates the number of returned Members. This command not only sorts scores from high to low, but also has the same meanings as zrangebyscore. Returns the list of Members whose scores are in the specified range.
ZremrangebyrankKey start stop O (log (n) + M) In time complexity, N indicates the number of sorted-set members, and M indicates the number of deleted members. Delete the Members whose index positions are between start and stop. Both start and stop are 0-based, that is, 0 indicates the member with the lowest score, and-1 indicates the last Member, that is, the member with the highest score. The number of deleted members.
ZremrangebyscoreKey min max O (log (n) + M) In time complexity, N indicates the number of sorted-set members, and M indicates the number of deleted members. Delete all members whose scores are between min and Max, that is, all members whose expressions meet Min <= score <= max. Min and Max parameters can be expressed in the open interval mode. For specific rules, see zcount. The number of deleted members.


Iii. Command example:

1. zadd/zcard/zcount/zrem/zincrby/zscore/zrange/zrank: # Start the redis client tool under the shell 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 respectively. 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 the returned result contains each member and its score. Otherwise, only the Members are returned. Redis 127.0.0.1: 6379> zrange myzset 0-1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" # obtain the index value of a member one in sorted-set. 0 indicates the first position. Redis 127.0.0.1: 6379> zrank myzset one (integer) 0 # the four member does not exist, so NIL is returned. Redis 127.0.0.1: 6379> zrank myzset four (NiL) # obtain the number of members in the myzset key. Redis 127.0.0.1: 6379> zcard myzset (integer) 3 # In the sorted-set associated with myzset, the number of Members whose scores meet the expression 1 <= score <= 2. Redis 127.0.0.1: 6379> zcount myzset 1 2 (integer) 2 # delete one and two members and return the number of actually deleted members. Redis 127.0.0.1: 6379> zrem myzset one two (integer) 2 # Check whether the deletion is successful. Redis 127.0.0.1: 6379> zcard myzset (integer) 1 # obtain the score of the member three. The return value is in the string format. Redis 127.0.0.1: 6379> zscore myzset three "3" # because the Member two has been deleted, this command returns nil. Redis 127.0.0.1: 6379> zscore myzset two (NiL) # increase the score of one member by 2 and return the updated score of the member. Redis 127.0.0.1: 6379> zincrby myzset 2 one "3" # increase the score of one member by-1 and return the updated score of the member. Redis 127.0.0.1: 6379> zincrby myzset-1 one "2" # Check whether the score of the updated member is correct. Redis 127.0.0.1: 6379> zrange myzset 0-1 withscores 1) "one" 2) "2" 3) "two" 4) "2" 5) "three" 6) "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 # obtain a Member whose score meets expression 1 <= score <= 2. Redis 127.0.0.1: 6379> zrangebyscore myzset 1 2 1) "one" 2) "two" # obtain a Member whose score satisfies expression 1 <score <= 2. Redis 127.0.0.1: 6379> zrangebyscore myzset (1 2 1) "two" #-INF indicates the first member, and + INF indicates the last member, the parameters after limit are used to restrict the returned Members. #2 indicates that the first three members are removed from the list of members whose position index (0-based) is equal to 2. Redis 127.0.0.1: 6379> zrangebyscore myzset-INF + INF limit 2 3 1) "three" 2) "four" # delete a Member whose score meets expression 1 <= score <= 2, and returns the actual number of deletions. Redis 127.0.0.1: 6379> zremrangebyscore myzset 1 2 (integer) 2 # Check whether the above deletion is successful. Redis 127.0.0.1: 6379> zrange myzset 0-1 1) "three" 2) "four" # delete a location index that contains a Member whose expression is 0 <= rank <= 1. Redis 127.0.0.1: 6379> zremrangebyrank myzset 0 1 (integer) 2 # Check whether the previous command is successfully deleted. Redis 127.0.0.1: 6379> zcard myzset (integer) 0 3. zrevrange/zrevrangebyscore/zrevrank: # Prepare test data for the following example. 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 # obtain and return the members in the range from high to low as the location index. Redis 127.0.0.1: 6379> zrevrange myzset 0-1 withscores 1) "four" 2) "4" 3) "three" 4) "3" 5) "two" 6) "2" 7) "one" 8) "1" # because the order is from high to low, the four position equals 0, and the three position equals 0, and so on. Redis 127.0.0.1: 6379> zrevrange myzset 1 3 1) "three" 2) "two" 3) "one" # because it is sorted from high to low, so the location of one is 3. Redis 127.0.0.1: 6379> zrevrank myzset one (integer) 3 # The four position is 0 because it is sorted from high to low. Redis 127.0.0.1: 6379> zrevrank myzset four (integer) 0 # obtain a Member whose scores meet the expression 3> = score> = 0 and output the results in reverse order, that is, the order from the height to the end. Redis 127.0.0.1: 6379> zrevrangebyscore myzset 3 0 1) "three" 2) "two" 3) "one" # This command supports the limit option, which is equivalent to this option in zrangebyscore, it is only calculated and obtained in the reverse order of the computing position. Redis 127.0.0.1: 6379> zrevrangebyscore myzset 4 0 limit 1 2 1) "three" 2) "two"

 



Iv. Application Scope:

1). It can be used for the point ranking of a large online game. When the score of a player changes, you can execute the zadd command to update the score of the player, and then use the zrange command to obtain the top ten user information of the points. Of course, we can also use the zrank command to obtain the ranking information of players through username. Finally, we will use the zrange and zrank commands in combination to quickly obtain information of other users with similar player points.
2) The sorted-sets type can also be used to build index data.

Redis data type -- sets

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.