Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

NoSQL--Redis 2.4--Sorted Set

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

First, Overview: Sorted-Set is an upgraded version of Set, which adds sorting function on the basis of Set. Sorted-Sets has a score associated with each member, and 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 members in the Sorted-Sets are orderly in the collection, it is still very efficient to access the members 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. It 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. Methods:

1. Zadd method:

ZADD key score member [[score member] [score member]...]

Add one or more member elements and their score values to the ordered set key.

If a member is already a member of the ordered set, update the score value of the member and ensure that the member is in the correct place by reinserting the member element.

The score value can be an integer value or a double-precision floating point number.

If key does not exist, create an empty ordered set and perform the ZADD operation.

An error is returned when key exists but is not an ordered set type.

For more information on ordered sets, see sorted set.

# add a single element redis > ZADD page_rank 10 google.com (integer) "add multiple elements redis > ZADD page_rank 9 baidu.com 8 bing.com (integer) 2redis > ZRANGE page_rank 0-1 WITHSCORES1)" bing.com "2)" 8 "3)" baidu.com "4)" 9 "5)" google.com "6)" 10 "# add existing elements And the score value remains the same redis > ZADD page_rank 10 google.com (integer) 0redis > ZRANGE page_rank 0-1 WITHSCORES # does not change 1) "bing.com" 2) "8" 3) "baidu.com" 4) "9" 5) "google.com" 6) "10" # add existing elements But changing the score value redis > ZADD page_rank 6 bing.com (integer) 0redis > ZRANGE page_rank 0-1 WITHSCORES # bing.com element's score value is changed 1) "bing.com" 2) "6" 3) "baidu.com" 4) "9" 5) "google.com" 6) "10"

2. Zrem method:

ZREM key member [member...]

Remove one or more members from the ordered set key, and members that do not exist will be ignored.

An error is returned when key exists but is not an ordered set type.

# Test data redis > ZRANGE page_rank 0-1 WITHSCORES1) "bing.com" 2) "8" 3) "baidu.com" 4) "9" 5) "google.com" 6) "10" # remove single element redis > ZREM page_rank google.com (integer) 1redis > ZRANGE page_rank 0-1 WITHSCORES1) "bing.com" 2) "8" 3) "baidu.com" 4) "9" # remove multiple elements redis > ZREM page_rank baidu.com bing.com (integer) 2redis > ZRANGE page_rank 0-1 WITHSCORES (empty list or set) # remove element redis > ZREM page_rank non-exists-element (integer) 0

3. Zincrby method:

ZINCRBY key increment member

Add incremental increment to the score value of member, a member of the ordered set key.

You can also subtract the value of score from the corresponding value, such as ZINCRBY key-5 member, by passing a negative value of increment, which subtracts the score of member by 5.

When key does not exist, or when member is not a member of key, ZINCRBY key increment member is equivalent to ZADD key increment member.

An error is returned when key is not an ordered set type.

The score value can be an integer value or a double-precision floating point number.

Redis > ZSCORE salary tom "2000" redis > ZINCRBY salary 2000 tom # tom raise! "4000"

4. Zscore method:

ZSCORE key member

Returns the score value of the member member in the ordered set key.

Returns nil if the member element is not a member of the ordered set key, or if key does not exist.

Redis > ZRANGE salary 0-1 WITHSCORES # shows all members and their score values 1) "tom" 2) "2000" 3) "peter" 4) "3500" 5) "jack" 6) "5000" redis > ZSCORE salary peter # Note the return value is the string "3500"

5. Zcard method:

ZCARD key

Returns the number of all members of the ordered set key.

Redis > ZADD salary 2000 tom # add a member (integer) 1redis > ZCARD salary (integer) 1redis > ZADD salary 5000 jack # add another member (integer) 1redis > ZCARD salary (integer) 2redis > EXISTS non_exists_key # ZCARD operation (integer) 0redis > ZCARD non_exists_key (integer) 0 for key that does not exist

6. Zcount method:

ZCOUNT key min max

Returns the number of members in the sorted set key whose score value is between min and max (including that the score value is equal to min or max by default).

Redis > ZRANGE salary 0-1 WITHSCORES # shows all members and their score 1) "jack" 2) "2000" 3) "peter" 4) "3500" 5) "tom" 6) "5000" redis > ZCOUNT salary 2000 5000 # number of people whose salary is between 2000 and 5000 (integer) 3redis > ZCOUNT salary 3000 5000 # number of people with salary between 3000 and 5, 000 (integer) 2

7. Zrange method:

ZRANGE key start end [WITHSCORES]

Returns the members of the specified interval in the ordered set key.

The position of the members is sorted by the increment of the score value (from small to large).

Members with the same score value are arranged in lexicographical order order.

If you need members to be sorted by decreasing score values (from large to small), use the ZREVRANGE command.

The subscript parameters start and stop are based on 0, that is, 0 represents the first member of the ordered set, 1 represents the second member of the ordered set, and so on.

You can also use a negative subscript, with-1 for the last member,-2 for the penultimate member, and so on.

Out-of-range subscripts do not cause errors.

For example, when the value of start is greater than the largest subscript of an ordered set, or when start > stop, the ZRANGE command simply returns an empty list.

On the other hand, if the value of the stop parameter is larger than the largest subscript of the ordered set, Redis treats stop as the largest subscript.

You can use the WITHSCORES option to return a member with its score value, and the return list is expressed in the format value1,score1,..., valueN,scoreN.

Client libraries may return more complex data types, such as arrays, tuples, and so on.

Redis > ZADD salary 5000 tom (integer) 1redis > ZADD salary 10086 boss (integer) 1redis > ZADD salary 3500 jack (integer) 1redis > ZRANGE salary 0-1 WITHSCORES # shows the members of the entire ordered set 1) "jack" 2) "3500" 3) "tom" 4) "5000" 5) "boss" 6) "10086" redis > ZRANGE salary 12 WITHSCORES # shows members of the ordered set subscript range 1 to 2 1) "tom" 2) "5000" 3) "boss" 4) "10086" redis > ZRANGE salary 0 200000 WITHSCORES # Test end subscript exceeds the maximum subscript 1) "jack" 2) "3500" 3) "tom" 4) "5000" 5) "boss" 6) "10086" redis > ZRANGE salary 200000 3000000 WITHSCORES # Test when a given interval does not exist in an ordered set (empty list or set)

8. Zrank method:

ZRANK key member

Returns the ranking of member member in the ordered set key. The members of the ordered set are arranged in the order of increasing the score value (from small to large).

The ranking is based on 0, that is, the member with the lowest score ranks 0.

Using the ZREVRANK command, you can get a ranking of members in descending order of score (from large to small).

Redis > ZRANGE salary 0-1 WITHSCORES # shows all members and their score 1) "peter" 2) "3500" 3) "tom" 4) "4000" 5) "jack" 6) "5000" redis > ZRANK salary tom # shows tom's salary ranking, second (integer) 1

9. Zrevrank method:

ZREVRANK key member

Returns the ranking of member member in the ordered set key. Where ordered set members are sorted by decreasing score values (from large to small).

The ranking is based on 0, that is, the member with the highest score ranks 0.

Using the ZRANK command, you can get a ranking of members by score increment (from small to large).

Redis > ZADD salary 2000 jack (integer) 1redis > ZADD salary 5000 tom (integer) 1redis > ZADD salary 3500 peter (integer) 1redis > ZREVRANK salary peter # peter second salary (integer) 1redis > ZREVRANK salary tom # tom highest wage (integer) 0

10. Zinterstore method:

ZINTERSTORE destination numkeys key [key...] [WEIGHTS weight [weight...]] [AGGREGATE SUM | MIN | MAX]

Calculates the intersection of one or more given ordered sets, where the number of given key must be specified as the numkeys parameter, and the intersection (result set) is stored in destination.

By default, the score value of a member in the result set is the sum of the score values of that member in all given sets.

Redis > ZADD mid_test 70 "Li Lei" (integer) 1redis > ZADD mid_test 70 "Han Meimei" (integer) 1redis > ZADD mid_test 99.5 "Tom" (integer) 1redis > ZADD fin_test 88 "Li Lei" (integer) 1redis > ZADD fin_test 75 "Han Meimei" (integer) 1redis > ZADD fin_test 99.5 "Tom" (integer) 1redis > ZINTERSTORE sum_point 2 mid_test fin_test (integer) 3redis > ZRANGE sum_point 0-1 WITHSCORES # explicit set All members and their score 1) "Han Meimei" 2) "1453)" Li Lei "4)" 158" 5) "Tom" 6) "199"

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report