Redis Note 7: Sorted set of data types

Keywords: Google

Command Description Example
 ZADD adds sort member ZADD key score member [score] [member]
ZCARD gets the number of members ZCARD key
 How many ZCOUNT key min max members are there between min and Max for the score obtained by ZCOUNT
 ZINCRBY Increases the score of the specified member ZINCRBY key increment member
 ZRANGE traversal members and fractional ZRANGE key start stop [WITHSCORES]
ZRANGEBYSCORE Returns Members and Fractions between min and Max and ZRANGEBYSCORE key min max [WITHSCORES]
ZRANK returns the subscript ZRANK key member of a member
 ZREM deletes the designated member ZREM key member [member...]
ZREVRANGE traversal members and score from large to small ZREVRANGE key start stop [WITHSCORES]
ZREVRANK returns member subscriptions in the order from large to small ZREVRANK key member
 ZSCORE Gets the score of the specified member ZSCORE key member
 ZREVRANGEBYSCORE gets members and scores from high to low ZREVRANGEBYSCORE key max min [WITHSCORES]
ZREMRANGEBYRANK deletes data between subscripts ZREMRANGEBYRANK key start stop
 ZREMRANGEBYSCORE Deletes Members of Min and Max with Fractions ZREMRANGEBYSCORE key min max

1,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 an ordered set, update the score value of the member and ensure that the member is in the right place by re-inserting the member element. The score value can be an integer value or a double-precision floating-point number. If the key does not exist, create an empty ordered set and perform the ZADD operation. When the key exists but is not an ordered set type, an error is returned.
Return value: The number of new members successfully added, excluding those that have been updated and already exist.
Example:

127.0.0.1:6379[15]> ZADD page_rank 10 google.com            # Add a single element
(integer) 1

127.0.0.1:6379[15]> ZADD page_rank 9 baidu.com 8 bing.com   # Adding multiple elements
(integer) 2
127.0.0.1:6379[15]> ZRANGE page_rank 0 -1 WITHSCORES
1) "bing.com"
2) "8"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"

127.0.0.1:6379[15]> ZADD page_rank 10 google.com            # Add existing elements
(integer) 0
127.0.0.1:6379[15]> ZRANGE page_rank 0 -1 WITHSCORES 
1) "bing.com"
2) "8"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"

127.0.0.1:6379[15]> ZADD page_rank 6 bing.com               # Add existing elements, but change the score value
(integer) 0
127.0.0.1:6379[15]> ZRANGE page_rank 0 -1 WITHSCORES
1) "bing.com"
2) "6"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"

2,ZCARD key

Returns the cardinality of the ordered set key.
Return value: Returns the cardinality of an ordered set when the key exists and is an ordered set type. When key does not exist, return 0.
Example:

127.0.0.1:6379[15]> ZADD salary 2000 tom
(integer) 1
127.0.0.1:6379[15]> ZCARD salary
(integer) 1
127.0.0.1:6379[15]> ZADD salary 5000 jack 
(integer) 1
127.0.0.1:6379[15]> ZCARD salary
(integer) 2
127.0.0.1:6379[15]> EXISTS non_exists_key 
(integer) 0
127.0.0.1:6379[15]> ZCARD non_exists_key
(integer) 0

3,ZCOUNT key min max

Returns the number of members of the ordered set key whose score value is between min and Max (including the default score value equal to min or max).
** Return value: ** number of members whose score value is between min and max.
Example:

127.0.0.1:6379[15]> ZADD salary 2000 tom 3500 peter 5000 jack 
(integer) 2
127.0.0.1:6379[15]> ZRANGE salary 0 -1 WITHSCORES
1) "tom"
2) "2000"
3) "peter"
4) "3500"
5) "jack"
6) "5000"
127.0.0.1:6379[15]> ZCOUNT salary 2000 5000
(integer) 3
127.0.0.1:6379[15]> ZCOUNT salary 3000 5000
(integer) 2

4,ZINCRBY key increment member

Incremental increment is added to the score value of member member member of an ordered set key. By passing a negative value increment, score can be subtracted from the corresponding value, such as ZINCRBY key-5 member, which is to subtract the score value of member from 5. When key does not exist or member is not a member of key, ZINCRBY key increment member is equivalent to ZADD key increment member. When key is not an ordered set type, an error is returned. The score value can be an integer value or a double-precision floating-point number.
Return value: The new score value of a member, expressed as a string.
Example:

127.0.0.1:6379[15]> ZADD salary 2000 tom 3500 peter 5000 jack
(integer) 3
127.0.0.1:6379[15]> ZSCORE salary tom
"2000"
127.0.0.1:6379[15]> ZINCRBY salary 2000 tom
"4000"
127.0.0.1:6379[15]> ZSCORE salary tom
"4000"

5,ZRANGE key start stop [WITHSCORES]

Returns the key of the ordered set, specifying the members in the interval.
Return value: A list of ordered integrators with score values (optional) within a specified interval.
Example:

127.0.0.1:6379[15]> ZADD salary 3500 jack 5000 tom 10086 boss
(integer) 3
127.0.0.1:6379[15]> ZRANGE salary 1 2 WITHSCORES
1) "tom"
2) "5000"
3) "boss"
4) "10086"
127.0.0.1:6379[15]> ZRANGE salary 0 200000 WITHSCORES 
1) "jack"
2) "3500"
3) "tom"
4) "5000"
5) "boss"
6) "10086"
127.0.0.1:6379[15]> ZRANGE salary 0 -1 WITHSCORES 
1) "jack"
2) "3500"
3) "tom"
4) "5000"
5) "boss"
6) "10086"
127.0.0.1:6379[15]> ZRANGE salary 200000 3000000 WITHSCORES
(empty list or set)

6,ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

Returns members of the ordered set key whose score values are between min and max, including those equal to min or max. Ordered integrators are arranged in order of increasing score value (from small to large).
Return value: A list of ordered integrators with score values (optional) within a specified interval.
Example:

127.0.0.1:6379[15]> ZADD salary 2500 jack 
(integer) 1
127.0.0.1:6379[15]> ZADD salary 5000 tom
(integer) 1
127.0.0.1:6379[15]> ZADD salary 12000 peter
(integer) 1
127.0.0.1:6379[15]> ZRANGEBYSCORE salary -inf +inf 
1) "jack"
2) "tom"
3) "peter"
127.0.0.1:6379[15]> ZRANGEBYSCORE salary -inf +inf WITHSCORES
1) "jack"
2) "2500"
3) "tom"
4) "5000"
5) "peter"
6) "12000"
127.0.0.1:6379[15]> ZRANGEBYSCORE salary -inf 5000 WITHSCORES 
1) "jack"
2) "2500"
3) "tom"
4) "5000"
127.0.0.1:6379[15]> ZRANGEBYSCORE salary (5000 400000 
1) "peter"

7,ZRANK key member

Returns the ranking of member members in the ordered set key. The ordered integrators are arranged in the order of increasing score value (from small to large). The bottom ranking is zero, that is to say, members with the smallest Score Rank zero. Using the ZREVRANK command, members can be ranked in descending order of score value (from large to small).
Return value: If member is a member of the ordered set key, return member ranking. If member is not a member of the ordered set key, return nil.
Example:

127.0.0.1:6379[15]> ZRANGE salary 0 -1 WITHSCORES
1) "tom"
2) "2000"
3) "peter"
4) "3500"
5) "jack"
6) "5000"
127.0.0.1:6379[15]> ZRANK salary tom
(integer) 0

8,ZREM key member [member …]

Remove one or more members of an ordered set key, and nonexistent members will be ignored. When the key exists but is not an ordered set type, an error is returned.
Return value: Number of members successfully removed, excluding neglected members.
Example:

127.0.0.1:6379[15]> ZRANGE page_rank 0 -1 WITHSCORES
1) "bing.com"
2) "6"
3) "baidu.com"
4) "9"
5) "google.com"
6) "10"             

127.0.0.1:6379[15]> ZREM page_rank google.com           # Remove a single element
(integer) 1
127.0.0.1:6379[15]> ZRANGE page_rank 0 -1 WITHSCORES
1) "bing.com"
2) "6"
3) "baidu.com"
4) "9"

127.0.0.1:6379[15]> ZREM page_rank baidu.com bing.com   # Remove multiple elements
(integer) 2
127.0.0.1:6379[15]> ZRANGE page_rank 0 -1 WITHSCORES
(empty list or set)

127.0.0.1:6379[15]> ZREM page_rank non-exists-element   # Remove non-existent elements
(integer) 0

9,ZREVRANGE key start stop [WITHSCORES]

Returns the key of the ordered set, specifying the members in the interval. The positions of members are arranged in descending order of score (from large to small). Members with the same score value are arranged in reverse lexicographic order. In addition to the fact that members are arranged in descending order of score values, other aspects of the ZREVRANGE command are the same as the ZRANGE command.
Return value: A list of ordered integrators with fractional values (optional) within a specified interval.
Example:

127.0.0.1:6379[15]> ZRANGE salary 0 -1 WITHSCORES 
1) "tom"
2) "2000"
3) "peter"
4) "3500"
5) "jack"
6) "5000"
127.0.0.1:6379[15]> ZREVRANGE salary 0 -1 WITHSCORES
1) "jack"
2) "5000"
3) "peter"
4) "3500"
5) "tom"
6) "2000"

10,ZREVRANK key member

Returns the ranking of member members in the ordered set key. The ordered integrators are sorted by score value decreasing (from large to small). The bottom ranking is zero, that is to say, members with the largest score rank zero. Using the ZRANK command, you can get the ranking of members in incremental (from small to large) order by score value.
Return value: If member is a member of the ordered set key, return member ranking. If member is not a member of the ordered set key, return nil.
Example:

127.0.0.1:6379[15]>  ZRANGE salary 0 -1 WITHSCORES
1) "jack"
2) "3500"
3) "tom"
4) "5000"
5) "boss"
6) "10086"
127.0.0.1:6379[15]> ZREVRANK salary tom
(integer) 1
127.0.0.1:6379[15]> ZREVRANK salary boss
(integer) 0

11,ZSCORE key member

Returns the score value of the member member member in the ordered set key. If the member element is not a member of the ordered set key, or the key does not exist, return nil.
** Return value: ** member's score value, expressed as a string.
Example:

127.0.0.1:6379[15]> ZADD salary 2000 tom 3500 peter 5000 jack
(integer) 3
127.0.0.1:6379[15]>  ZSCORE salary peter
"3500"

12,ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]

Returns all members of the ordered set key whose score value is between Max and min (including, by default, all members equal to max or min). Ordered integrators are arranged in descending order of score values (from large to small).
Return value: A list of ordered integrators with score values (optional) within a specified interval.
Example:

127.0.0.1:6379[15]> ZADD salary 10086 jack 5000 tom 7000 peter 3500 joe
(integer) 4
127.0.0.1:6379[15]> ZREVRANGEBYSCORE salary +inf -inf
1) "jack"
2) "peter"
3) "tom"
4) "joe"
127.0.0.1:6379[15]> ZREVRANGEBYSCORE salary +inf -inf
1) "jack"
2) "peter"
3) "tom"
4) "joe"
127.0.0.1:6379[15]> ZREVRANGEBYSCORE salary 10000 2000
1) "peter"
2) "tom"
3) "joe"

13,ZREMRANGEBYRANK key start stop

Remove all members of the ordered set key and specify the rank interval. The intervals are indicated by the following scalar parameters: start and stop, including start and stop. The subscript parameters start and stop are based on 0, that is to say, 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 negative subscripts, with - 1 for the last member, and - 2 for the penultimate member, and so on.
Return value: Number of members removed.
Example:

127.0.0.1:6379[15]> ZADD salary 2000 jack
(integer) 1
127.0.0.1:6379[15]> ZADD salary 5000 tom
(integer) 1
127.0.0.1:6379[15]> ZADD salary 3500 peter
(integer) 1
127.0.0.1:6379[15]> ZREMRANGEBYRANK salary 0 1 
(integer) 2
127.0.0.1:6379[15]> ZRANGE salary 0 -1 WITHSCORES
1) "tom"
2) "5000"

14,ZREMRANGEBYSCORE key min max

Remove all members of the ordered set key whose score values are between min and Max (including those equal to min or max). Since version 2.1.6, members whose score value equals min or max can also be excluded.
Return value: Number of members removed.
Example:

127.0.0.1:6379[15]> ZADD salary 2000 tom 3500 peter 5000 jack 
(integer) 3
127.0.0.1:6379[15]> ZRANGE salary 0 -1 WITHSCORES
1) "tom"
2) "2000"
3) "peter"
4) "3500"
5) "jack"
6) "5000"
127.0.0.1:6379[15]> ZREMRANGEBYSCORE salary 1500 3500
(integer) 2
127.0.0.1:6379[15]> ZRANGE salary 0 -1 WITHSCORES
1) "jack"
2) "5000"

(end)

Reference resources: http://blog.csdn.net/thinkercode/article/details/46575813

Posted by ajcrm125 on Wed, 10 Apr 2019 04:36:31 -0700