First, overview:
Sortedsets 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 sortedsets 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 Sortedsets must be unique, the fractions (score) can be duplicated.
Adding, deleting, or updating a member in Sortedset 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 Sortedsets 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] /td> 
o (log (N)) 

This operation actually inserts the number of members. 
zcard key 
o (1) 

Returns the number of members in Sortedsets, or 0 if the key does not exist. 
zcountkey min Max 
o (log (N) +m) 

score Specifies the number of members in the range. 
zincrbykey Increment member 
o (log (N)) 


zrangekey start stop [withscores] 
o (log (N) +m) 

Returns the list of members of the index between start and stop. 
zrangebyscore key min Max [Withscores] [LIMIT offset Count] 
o (log (N) +m) 

Returns the list of members in the specified range of fractions. 
Zrank Key Member 
O (log (N)) 
N in the time complexity represents the number of members in the Sortedset. The members in Sortedset 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 Sortedset. 
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 Sortedset, and M indicates the number of members to be deleted. The command removes the member specified in the parameter, and the nonexistent member is ignored. If the value associated with the key is not sortedset, 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 Sortedset, 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 Sortedset. 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 Sortedset, 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 Sortedset, 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 0based, 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 Sortedset, 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. 
Examples of commands:
1. Zadd/zcard/zcount/zrem/zincrby/zscore/zrange/zrank:
#Start the Redis client tool at the shell's command line.
/> rediscli
#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 results include each member and its score, 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 SortedSet. 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 SortedSet 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
"3"
#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
"2"
#Check if the member's score has been updated correctly.
redis 127.0.0.1:6379> zrange myzset 0 1 WITHSCORES
1) "one"
twenty two"
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
#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 (0based) is equal to 2, and go to 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 examples that follow.
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, except that the position is calculated and obtained in the reverse order.
redis 127.0.0.1:6379> zrevrangebyscore myzset 4 0 limit 1 2
1) "three"
2) "two"
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 Sortedsets type can also be used to build index data.
The above is the Redis tutorial (vi): Sortedsets data type content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!