Redis learning Manual (sorted-sets data type)

Source: Internet
Author: User

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.
zcount key 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, -INF and + INF indicate the highest and lowest scores of sorted-sets, respectively. by default, min and Max indicate a closed range. That is, members in min <= score <= max are returned. However, we can add the " (" character before min and Max to indicate the open interval, for example, (min max indicates min , while (min (max indicates min . Number of Members in the score range.
zincrby key 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. new score expressed as a string.
zrange key start stop [withscores] O (log (n) + m) in time complexity, N indicates the number of members in sorted-set, 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 withscores option, the command returns a score value for each member, such as value1, score1, value2, score2 .... returns the list of members of the index between start and stop.
zrangebyscore key min max [withscores] [limit offset count] O (log (n) + m) in time complexity, N indicates the number of members in sorted-set, 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 parameters 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.
zrevrange key Start stop [withscores] O (log (n) + m) in time complexity, N indicates the number of members in sorted-set, 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. return the list of specified members.
ZrevrankKey Member O (log (n )) N in time complexity indicates the number of sorted-set members. Functions andZrankBasically the same. 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. In addition to sorting by scores from high to low, this command has the same meaningsZrangebyscoreSame. 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, referZcount. 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.
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 one member 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
# Return the number of Members whose scores meet the expression 1 <= score <= 2 in the sorted-set associated with myzset.
Redis 127.0.0.1: 6379> Zcount myzset 1 2
(Integer) 2
# Delete one and two members, and return the actual number of 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 a member one 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 to-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 the Members whose scores meet the 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. parameters following limit are used to restrict the returned member,
#2 indicates that, starting from a Member whose position index (0-based) is equal to 2, the next three members are removed.
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 return the actual number of deleted members.
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 Member whose location index meets the condition of expression 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 fou R
(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 it is sorted from high to low, if the position is equal to 0, it is four, 1 is three, 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, the location of one is 3.
Redis 127.0.0.1: 6379> Zrevrank myzset one
(Integer) 3
# Because it is sorted from high to low, the four position is 0.
Redis 127.0.0.1: 6379> Zrevrank myzset four
(Integer) 0
# Obtain the Members whose scores meet the expression 3> = score> = 0, and output them 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, but is calculated and obtained in reverse order when calculating the 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 to rank points 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.

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.