Redis Tutorial (vi): sorted-sets data type _redis

Source: Internet
Author: User
Tags redis redis tutorial

First, overview:

Sorted-sets and sets types are very similar, and they are all collections of strings that do not allow duplicate members to appear in a set. The main difference between them is that each member of the sorted-sets will have a fraction (score) associated with it, and Redis is the sort of small to large members of the set through fractions. However, it is important to note that although the members in the Sorted-sets must be unique, the score (score) can be repeated.
Adding, deleting, or updating a member in Sorted-set is a very fast operation with a time complexity of the 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 implement in many other types of databases, in other words, it is very difficult to model in other databases to achieve the same efficiency as redis at that point.

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. Adds 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 in the argument already exists at the time of addition, the command updates the score for this member to the new value, and then reorder 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 a Score/member pair into it. If the key already exists, but its associated value 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 and returns 0 if the key does not exist.
zcountKey min max O (log (N) +m) N in 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 (score) between Min and Max. The additional explanation for the Min and Max parameters is that-inf and +inf represent the highest and lowest values of the fractions in sorted-sets respectively. by Default, the range that Min and Max represents is a closed interval range, where the members of min <= score <= Max are returned. However, we can add "(") to the Min and Max before "(") to indicate the open range, such as Min Max, min < score <= Max, and (min) (max says Min < score & Lt Max. The number of members in the specified range for the score.
ZincrbyKey Increment member O (log (N)) N in the time complexity represents the number of members in the Sorted-sets. This command will increase the specified number of points for the specified member in the specified key. If the member does not exist, the command adds the member and assumes that its initial score is 0, and then adds the increment to its score. If key does not exist, the command creates the key and its associated sorted-sets and contains the members specified by the parameter with a score of increment parameters. If the key is not associated with the Sorted-sets type, the associated error message is returned. The new score in the form of 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 represents the number of members returned. This command returns the members of the order in the range specified in the parameters start and stop, where the start and stop arguments 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 the stop is greater than the maximum index value, the command returns the last member from start to the collection.  If the command has an optional parameter withscores option, the command will include the fractional value of each member in the returned results, such as value1,score1,value2,score2 .... Returns a list of the 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 represents the number of members returned. This command returns all members of the score between Min and Max, members of the expression min <= score <= Max, where the returned members are returned in the 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. Optional parameter limit is used to limit the range of numbers returned by a member. An optional parameter offset represents the return of the count member at the beginning of the eligible offset member. The meaning of the optional parameter withscores refers to the description of the option in Zrange. The last thing to note is that the rules of Min and Max in the parameters can refer to 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 the Sorted-set are stored in the order of the score from lowest to highest, which returns the position value of the specified member in the parameter, where 0 represents the first member, which is the lowest score in the Sorted-set. If the member exists, its position index value is returned. otherwise return nil.
Zrem Key member [member ...] O (M log (N)) In the time complexity, n represents the number of members in the Sorted-set, and M represents the number of members that are deleted. This command removes the member specified in the parameter, where the nonexistent 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 represents the number of members returned. The function of this command is essentially the same as Zrange, except that the command is to get the members of the specified position in reverse order, that is, from highest to lowest. 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 command's functionality is essentially the same as Zrank, except that the command gets an index that is sorted from highest to lowest, and that 0 represents the first element, the highest-scoring member. If the member exists, its position index value is returned. otherwise return nil.
ZscoreKey Member O (1) Gets the score of the specified member of the specified key. If the member exists, returns its score as a string, otherwise returns 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 represents the number of members returned. The command, in addition to sorting from high to low fractions, 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 represents the number of members that are deleted. Deletes the member between start and stop at the index location, where start and stop are 0-based, that is, 0 represents the lowest member of the score,-1 represents the last member, the highest-scoring member. 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 represents the number of members that are deleted. Deletes all members of the score 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 an open interval, with the specific rules referenced zcount. The number of members that were deleted.

Third, the command example:

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

Copy Code code as follows:

#在Shell的命令行下启动Redis客户端工具.
/> REDIS-CLI
#添加一个分数为1的成员.
Redis 127.0.0.1:6379> zadd myzset 1 "one"
(integer) 1
#添加两个分数分别是2和3的两个成员.
Redis 127.0.0.1:6379> zadd myzset 2 "two" 3 "three"
(integer) 2
#0表示第一个成员,-1 represents the last member. The Withscores option indicates that the returned results contain each member and its fractions, otherwise only 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"
#获取成员one在Sorted the position index value in the-set. 0 represents the first position.
Redis 127.0.0.1:6379> Zrank Myzset One
(integer) 0
#成员four并不存在, so return nil.
Redis 127.0.0.1:6379> Zrank Myzset Four
(nil)
#获取myzset键中成员的数量.
Redis 127.0.0.1:6379> Zcard Myzset
(integer) 3
#返回与myzset关联的Sorted-set, the score satisfies the number of members of the expression 1 <= score <= 2.
Redis 127.0.0.1:6379> zcount myzset 1 2
(integer) 2
#删除成员one和two, returns the number of actual deleted members.
Redis 127.0.0.1:6379> Zrem Myzset One Two
(integer) 2
#查看是否删除成功.
Redis 127.0.0.1:6379> Zcard Myzset
(integer) 1
#获取成员three的分数. The return value is a string form.
Redis 127.0.0.1:6379> Zscore Myzset Three
"3"
#由于成员two已经被删除, so the command returns to nil.
Redis 127.0.0.1:6379> Zscore Myzset Two
(nil)
#将成员one的分数增加2, and returns the score after the member has been updated.
Redis 127.0.0.1:6379> Zincrby Myzset 2 One
"3"
#将成员one的分数增加-1 and returns the score after the member has been updated.
Redis 127.0.0.1:6379> Zincrby myzset-1 One
"2"
#查看在更新了成员的分数后是否正确.
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
Copy Code code as follows:

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
#获取分数满足表达式1 member of the <= score <= 2.
Redis 127.0.0.1:6379> zrangebyscore myzset 1 2
1) "One"
2) "Two"
Members of the #获取分数满足表达式1 < score <= 2.
Redis 127.0.0.1:6379> Zrangebyscore Myzset (1 2
1) "Two"
#-inf represents the first member, +inf represents the last member, and the argument behind the limit is used to limit the return member's own.
#2表示从位置索引 (0-based) is equal to 2 of the members start, go to the back 3 members.
Redis 127.0.0.1:6379> zrangebyscore myzset-inf +inf limit 2 3
1) "Three"
2) "Four"
#删除分数满足表达式1 <= score <= 2 member and returns the actual number of deletes.
Redis 127.0.0.1:6379> zremrangebyscore myzset 1 2
(integer) 2
#看出一下上面的删除是否成功.
Redis 127.0.0.1:6379> zrange Myzset 0-1
1) "Three"
2) "Four"
#删除位置索引满足表达式0 member of <= rank <= 1.
Redis 127.0.0.1:6379> zremrangebyrank myzset 0 1
(integer) 2
#查看上一条命令是否删除成功.
Redis 127.0.0.1:6379> Zcard Myzset
(integer) 0

3. Zrevrange/zrevrangebyscore/zrevrank:
Copy Code code as follows:

#为后面的示例准备测试数据.
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
#以位置索引从高到低的方式获取并返回此区间内的成员.
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"
#由于是从高到低的排序, 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) "Two"
3) "One"
#由于是从高到低的排序, so one's position is 3.
Redis 127.0.0.1:6379> Zrevrank Myzset One
(integer) 3
#由于是从高到低的排序, so the four position is 0.
Redis 127.0.0.1:6379> Zrevrank Myzset Four
(integer) 0
#获取分数满足表达式3 the members of the >= score >= 0, and output in reverse order, that is, from the high to the bottom of the order.
Redis 127.0.0.1:6379> Zrevrangebyscore myzset 3 0
1) "Three"
2) "Two"
3) "One"
The #该命令支持limit选项, which is equivalent to this option in Zrangebyscore, is computed and retrieved in reverse order at the point of calculation.
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 list. Whenever a player's score changes, you can perform a zadd command to update the player's score, and then obtain the user information for the top ten by Zrange command. Of course, we can also use the Zrank command through username to get the player's ranking information. Finally, we will combine using the Zrange and Zrank commands to quickly get information from other users who are close to one player's 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.