In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the introduction of the Sorted-Sets type and the use of commands in redis. The content of the article is of high quality, so I hope you can get something after reading this article.
I. Overview:
Redis ordered collections Sorted-Sets, like collections, are collections of elements of type string, and duplicate members are not allowed.
The difference is that each element is associated with a score of type double. Redis sorts the members of the collection from small to large by scores.
The members of an ordered set are unique, but the score can be repeated.
The collection is implemented through a hash table, so the complexity of adding, deleting, and finding is all O (1). The maximum number of members in the collection is 4294967295 (each collection can store more than 4 billion members).
The Sorted-Sets and Sets types are very similar in that they are both 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 score associated with it. Redis uses scores to sort the members of the collection from small to large.
However, it is important to note that although the members of the Sorted-Sets must be unique, the score is repeatable.
Adding, deleting, or updating a member in Sorted-Set is a very fast operation, and its time complexity is the logarithm of the number of members in the collection. Because the positions of members in the Sorted-Sets are ordered in the collection.
Therefore, it is still very efficient to visit members located in the middle of the collection. 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 to achieve the same efficiency as Redis at this point.
2. Scope of application:
1) can be used in the ranking of points for a large online game. Whenever the player's score changes, the ZADD command can be executed to update the player's score, and then the user information of the integral TOP TEN can be obtained through the ZRANGE command.
Of course, we can also use the ZRANK command to get the player's ranking information through username. Finally, we will use a combination of ZRANGE and ZRANK commands to quickly get information about other users who are close to a player's points.
2) the Sorted-Sets type can also be used to build index data.
3. List of related commands:
The command prototype time complexity command describes the return value ZADD key score member [score] [member] O (log (N)) the N in the time complexity represents the number of members in the Sorted-Sets. Add all the members and their scores specified in the parameter to the Sorted-Set of the specified key, in which we can specify multiple sets of score/member as parameters. If a member of the parameter already exists when it is added, the command updates the member's score to the new value and reorders 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 of type Sorted-Sets, the associated error message will be returned. The actual number of members inserted in this operation. ZCARD keyO (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. N in ZCOUNT key min maxO (log (N) + M) time complexity represents the number of members in Sorted-Sets, and M represents the number of elements between min and max. This command is used to get the number of members whose score (score) is between min and max. Additional note for the min and max parameters is that-inf and + inf represent the highest and lowest values of the score in the Sorted-Sets, respectively. By default, the range represented by min and max is a closed range, that is, min zrank myzset four (nil) # gets the number of members in the myzset key. Redis 127.0.0.1 redis 6379 > zcard myzset (integer) 3 # returns the Sorted-Set associated with myzset, and the score satisfies the expression 1 zrem myzset one two (integer) 2 # to see if the deletion is successful. Redis 127.0.0.1 integer 6379 > zcard myzset (integer) 1 # gets the score of the member three. The return value is a string. Redis 127.0.0.1 two 6379 > nil "3" # this command returns nil because the member two has been deleted. Redis 127.0.0.1 nil 6379 > zscore myzset two (nil) # increases the score of the member one by 2 and returns the updated score of that member. Redis 127.0.0.1 one 6379 > zincrby myzset 2 one "3" # increases the score of member one by-1 and returns the updated score of that member. Redis 127.0.0.1 one 6379 > zincrby myzset-1 one "2" # to see if it is correct after updating the member's score. Redis 127.0.0.1 WITHSCORES 6379 > one 0-1 WITHSCORES 1) "one" 2) "2" 3) "two" 4) "2" 5) "three" 6) "3"
2 、 ZRANGEBYSCORE/ZREMRANGEBYRANK/ZREMRANGEBYSCORE
Redis 127.0.0.1 redis 6379 > del myzset (integer) 1 redis 127.0.0.1 integer 6379 > zadd myzset 1 one 2 two 3 three 4 four (integer) 4 # get the score satisfying expression 1 zrangebyscore myzset-inf + inf limit 2 31) "three" 2) "four" # deletion score satisfies expression 1 zrange myzset 0-11) "three" 2) "four" # delete bit Set the index to satisfy the expression 0 zcard myzset (integer) 0
3 、 ZREVRANGE/ZREVRANGEBYSCORE/ZREVRANK:
# prepare test data for the following example. Redis 127.0.0.1 redis 6379 > del myzset (integer) 0 redis 127.0.0.1 integer 6379 > zadd myzset 1 one 2 two 3 three 4 four (integer) 4 # gets and returns the members of this interval in a positional index from high to low. Redis 127.0.0.1 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, the position equal to 0 is three, and so on. Redis 127.0.1 three 6379 > zrevrange myzset 131) "three" 2) "two" 3) "one" # because it is sorted from high to low, the position of one is 3. Redis 127.0.0.1 four 6379 > zrevrank myzset one (integer) 3 # because it is sorted from the highest to the lowest, the position of the four is 0. Redis 127.0.0.1 redis 6379 > zrevrank myzset four (integer) 0 # takes the member whose score satisfies the expression 3 > = score > = 0 and outputs it in reverse order, that is, from the highest to the lowest. Redis 127.0.0.1 zrevrangebyscore myzset 301) "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 limit 6379 > zrevrangebyscore myzset 40 limit 121) "three" 2) "two" above is the introduction of the Sorted-Sets type and the use of commands in redis. Have you learned anything after reading it? If you want to know more about it, you are welcome to follow the industry information. Thank you for reading.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.