Redis Tutorial (vi): Sorted-sets data types

Source: Internet
Author: User
Tags redis tutorial



Reproduced in: http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/133.html



First, overview:



Sorted-sets and sets types are very similar, both are collections 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-sets has a fraction (score) associated with it, and Redis is sorting the members of the collection from small to large by fractions. However, it is important to note that although the members in the Sorted-sets must be unique, the fractions (score) can be duplicated.
Adding, deleting, or updating a member in Sorted-set is a very fast operation with a time complexity of a logarithm of the number of members in the collection. Because the members in the Sorted-sets are in an orderly position in the collection, even accessing the members in the middle of the collection is still very efficient. In fact, this feature of Redis is difficult to achieve in many other types of databases, in other words, it is very difficult to model in other databases in order to achieve the same efficiency as Redis.



Ii. List of related commands:



T


Command prototypes Complexity of Time Command description return value
Zadd key score member [score] [member] O (log (N)) N in the time complexity represents the number of members in the Sorted-sets. Add all the members specified in the parameter and their fractions to the sorted-set of the specified key, in which we can specify multiple sets of Score/member as parameters. If a member already exists in the parameter when it is added, the command updates the member's score to the new value, and then re-sorts the member based on the new value. If the key does not exist, the 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 a sorted-sets type, the associated error message is returned. The number of members actually inserted in this operation.
Zcard Key O (1) Gets the number of members contained in the sorted-sets associated with the key. Returns the number of members in the Sorted-sets, or 0 if the key does not exist.
zcountKey min max O (log (N) +m) N in the time complexity represents the number of members in the Sorted-sets, and M represents the number of elements between Min and Max. This command is used to get the number of members of the fraction (score) between Min and Max. For the min and Max parameters, it is necessary to note that-inf and +inf represent the highest and lowest values of the scores in the Sorted-sets, respectively. By default, Min and Max represent a range of closed intervals, that is, members within min <= score <= Max will be returned. However, by adding "(" characters before Min and Max, we can indicate an open interval, such as (min max = min < score <= Max, and (min) (max = min < score & Lt Max. SCORE specifies the number of members within the range.
ZincrbyKey Increment member O (log (N)) N in the time complexity represents the number of members in the Sorted-sets. The command increments 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, and then adds its score to increment. If the key does not exist, the command creates the key and its associated sorted-sets, and contains the member specified by the parameter, with a fraction of the increment parameter. If the key is not associated with the Sorted-sets type, the associated error message is returned. A new score expressed as a string.
Zrangekey start stop [Withscores] O (log (N) +m) N in the time complexity represents the number of members in the Sorted-set, and M indicates the number of members returned. The command returns a member in the order of the parameter start and stop specified, where the start and stop parameters are 0-based, that is, 0 represents the first member, and 1 represents the last member. If start is greater than the maximum index value in the Sorted-set, or Start > Stop, an empty collection is returned. If stop is greater than the maximum index value, the command returns the last member from start to the collection. If the command has the optional parameter withscores option, the command will include the fractional value of each member in the returned result, 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) N in the time complexity represents the number of members in the Sorted-set, and M indicates the number of members returned. The command returns all the members of the fraction between Min and Max, which satisfies the member of the expression min <= score <= Max, where the returned members are returned in order of their fractions from low to high, and if the members have the same score, they are returned in the dictionary order of the members. The optional parameter limit is used to restrict the number range of returned members. An optional parameter, offset, returns from the eligible offset member, returning count members. The meaning of the optional parameter withscores refers to the description of this option in Zrange. Finally, it is necessary to note that the rules for Min and Max in the parameters can be referenced by the command zcount. Returns a list of members in a specified range of fractions.
Zrank Key Member O (log (N)) N in the time complexity represents the number of members in the Sorted-set. The members in Sorted-set are stored in sequential order from low to high, and the command returns the position value of the specified member in the parameter, where 0 represents the first member, which is the member with the lowest score in Sorted-set. If the member exists, the index value of its position is returned. Otherwise, nil is returned.
Zrem Key member [member ...] O (M log (N)) In time complexity, n represents the number of members in the Sorted-set, and M indicates the number of members to be deleted. The command removes the member specified in the parameter, and the non-existent member is ignored. If the value associated with the key is not sorted-set, the corresponding error message is returned. The number of members that were actually deleted.
zrevrange Key Startstop[withscores] O (log (N) +m) N in the time complexity represents the number of members in the Sorted-set, and M indicates the number of members returned. The function and Zrange of the command are basically the same, except that the command is to get the members of the specified position in reverse order, that is, from high to low. If members have the same score, they are sorted in descending dictionary order. Returns the specified list of members.
ZrevrankKey Member O (log (N)) N in the time complexity represents the number of members in the Sorted-set. The function and Zrank of the command are basically the same, except that the command gets an index from high to low, and the same 0 represents the first element, the member with the highest score. If the member exists, the index value of its position is returned. Otherwise, nil is returned.
ZscoreKey Member O (1) Gets the fraction of the specified member of the specified key. If the member exists, returns its fraction as a string, otherwise nil.
zrevrangebyscorekey Max min [withscores] [LIMIT offset Count] O (log (N) +m) N in the time complexity represents the number of members in the Sorted-set, and M indicates the number of members returned. This command, in addition to sorting based on the order of fractions from high to low, has the same meaning as Zrangebyscore for other functions and parameters. Returns a list of members in a specified range of fractions.
Zremrangebyrankkey Start stop O (log (N) +m) N in the time complexity represents the number of members in the Sorted-set, and M indicates the number of members to be deleted. The delete index position is the member between start and stop, both start and stop are 0-based, that is, 0 represents the lowest fraction of the member, 1 represents the last member, that is, the member with the highest score. The number of members that were deleted.
zremrangebyscoreKey min max O (log (N) +m) N in the time complexity represents the number of members in the Sorted-set, and M indicates the number of members to be deleted. Removes all members of the fraction between Min and Max, which satisfies all members of the expression min <= score <= max. The min and max parameters can be expressed in an open interval, with specific rules referencing zcount. The number of members that were deleted.


#p # pagination Title #e#






Examples of commands:



1. Zadd/zcard/zcount/zrem/zincrby/zscore/zrange/zrank:



The code is as follows:



#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 "," 3 "three"
(integer) 2
# 0 为 第一 会员, 1 represents the last member. The Withscores option indicates that each member and its score are included in the returned result, or only members are returned.
Redis 127.0.0.1:6379> zrange myzset 0-1 withscores
1) "One"
twenty one"
3) "Both"
4) "2"
5) "Three"
6) "3"
#Get member one in Sorted the position index value in-set. 0 indicates the first position.
Redis 127.0.0.1:6379> Zrank Myzset One
(integer) 0
#Member four does not exist, so return nil.
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
#Return the Sorted-set associated with myzset, the fraction satisfies the number of members of the expression 1 <= score <= 2.
Redis 127.0.0.1:6379> zcount myzset 1 2
(integer) 2
#Delete members one and two, returns the number of actual deleted members.
Redis 127.0.0.1:6379> Zrem Myzset One
(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 in the form of a string.
Redis 127.0.0.1:6379> Zscore Myzset Three
"3"
#Since member two has been deleted, so the command returns nil.
Redis 127.0.0.1:6379> Zscore Myzset
(nil)
# Increase the score of member one by 2 and returns the updated score for the member.
Redis 127.0.0.1:6379> Zincrby Myzset 2 One
"3"
# Increase member's one's score by -1, and returns the updated score for the member.
Redis 127.0.0.1:6379> Zincrby myzset-1 One
"2"
# See if it's correct after updating the member's score.
Redis 127.0.0.1:6379> zrange myzset 0-1 withscores
1) "One"
twenty two"
3) "Both"
4) "2"
5) "Three"
6) "3"




2. Zrangebyscore / zremrangebyrank / zremrangebyscore
#p # pagination Title # e #





The code is as follows:




Redis 127.0.0.1:6379> del Myzset
(integer) 1
Redis 127.0.0.1:6379> zadd myzset 1 One 2 3 three 4 four
(integer) 4
#Get score satisfying expression 1 member of <= score <= 2.
Redis 127.0.0.1:6379> zrangebyscore myzset 1 2
1) "One"
2) "Both"
#Get score satisfying expression 1 <score member of <= 2.
Redis 127.0.0.1:6379> Zrangebyscore Myzset (1 2
1) "Both"
# -inf represents the first member, + inf represents the last member, and the parameter after limit is used to restrict the return member's own,
# 2 means (0-based) equals 2 of the members to start, to go back to 3 members.
Redis 127.0.0.1:6379> zrangebyscore myzset-inf + inf limit 2 3
1) "Three"
2) "Four"
#Deletion score satisfies the expression 1 <= score <= 2 members and returns the actual number of 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 position index satisfies the expression 0 a member of <= rank <= 1.
Redis 127.0.0.1:6379> zremrangebyrank myzset 0 1
(integer) 2
#Check if the previous command was deleted successfully.
Redis 127.0.0.1:6379> Zcard Myzset
(integer) 0




3. Zrevrange / zrevrangebyscore / zrevrank:





The code is as follows:




# 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 3 three 4 four
(integer) 4
#Get and return the members in this interval in 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) "Both"
6) "2"
7) "One"
8) "1"
#Since it is sorted from high to low, so the position equals 0 is four, 1 is three, and so on.
Redis 127.0.0.1:6379> zrevrange myzset 1 3
1) "Three"
2) "Both"
3) "One"
#Since it is sorted from high to low, so 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, so the location of four is 0.
Redis 127.0.0.1:6379> Zrevrank Myzset Four
(integer) 0 # p # page title # e #
#Get score satisfying expression 3




























>= score >= 0 members and output in reverse order, that is, from high to the end of the order.
Redis 127.0.0.1:6379> Zrevrangebyscore myzset 3 0
1) "Three"
2) "Both"
3) "One"
#该命令支持limit选项, the meaning is equivalent to this option in zrangebyscore, and is calculated and fetched in reverse order when the position is calculated.
Redis 127.0.0.1:6379> zrevrangebyscore myzset 4 0 Limit 1 2
1) "Three"
2) "Both"



Iv. Scope of application:






1). Can be used for a large online game of the points leaderboard. Each time a player's score changes, the Zadd command can be executed to update the player's score, and then the Zrange command gets the top ten user information. Of course we can also use the Zrank command to get the player's ranking information through username. Finally, we will combine the Zrange and Zrank commands to quickly get information about other users with a similar player score.
2). The Sorted-sets type can also be used to build index data.






Redis Tutorial (vi): Sorted-sets data types


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.