Redis basic operation – sorted set

Time:2021-8-19

Redis ordered collections, like collections, are collections of string elements, 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 through scores.

Members of an ordered set are unique, but scores can be repeated.

 

1、 Add one or more members to an ordered collection, or update the scores of existing members

Command: zadd

Format: zadd key Score1 member1 [score2 member2]

127.0.0.1:6379> zadd k1 1.0 m1 2.0 m2
(integer) 2
127.0.0.1:6379> zadd k1 3.0 m1 4.0 m2
(integer) 0

2、 Returns the members in the specified interval of the ordered set through the index interval

Commands: zrange

Format: zrange key start stop [with scores]

127.0.0.1:6379> zadd k1 1.0 m1 2.0 m2
(integer) 2
127.0.0.1:6379> zrange k1 0 10
1) "m1"
2) "m2"
127.0.0.1:6379> zadd k1 3.0 m1 4.0 m3
(integer) 1
127.0.0.1:6379> zrange k1 0 10
1) "m2"
2) "m1"
3) "m3"
127.0.0.1:6379> zrange k1 0 10 WITHSCORES
1) "m2"
2) "2"
3) "m1"
4) "3"
5) "m3"
6) "4"

3、 Gets the number of members of an ordered collection

Command: zcard

Format: zcard key

127.0.0.1:6379> zadd k1 1.0 m1
(integer) 1
127.0.0.1:6379> zcard k1
(integer) 1
127.0.0.1:6379> zadd k1 2.0 m1 3.0 m2
(integer) 1
127.0.0.1:6379> zcard k1
(integer) 2
127.0.0.1:6379> zrange k1 0 10 WITHSCORES
1) "m1"
2) "2"
3) "m2"
4) "3"

4、 Calculates the number of members of a specified interval score in an ordered set

Command: zcount

Format: zcount key min max

127.0.0.1:6379> zadd k1 1.1 m1 1.2 m2 1.3 m3 2.1 m4 2.2 m5 2.3 m6
(integer) 6
127.0.0.1:6379> zrange k1 0 -1 withscores
 1) "m1"
 2) "1.1000000000000001"
 3) "m2"
 4) "1.2"
 5) "m3"
 6) "1.3"
 7) "m4"
 8) "2.1000000000000001"
 9) "m5"
10) "2.2000000000000002"
11) "m6"
12) "2.2999999999999998"
127.0.0.1:6379> zcount k1 1 2
(integer) 3
127.0.0.1:6379> zcount k1 1 3
(integer) 6

5、 Adds the increment increment to the score of the specified member in the ordered set

Commands: zincrby

Format: zinc key increment member

127.0.0.1:6379> zadd k1 1.1 m1 1.2 m2
(integer) 2
127.0.0.1:6379> zrange k1 0 -1 withscores
1) "m1"
2) "1.1000000000000001"
3) "m2"
4) "1.2"
127.0.0.1:6379> zincrby k1 0.5 m1
"1.6000000000000001"
127.0.0.1:6379> zrange k1 0 -1 withscores
1) "m2"
2) "1.2"
3) "m1"
4) "1.6000000000000001"

6、 Calculates the intersection of a given one or more ordered sets and stores the result set in a new ordered set key

Command: zinterstore

Format: zinterstore des numkeys key1 [key2]

127.0.0.1:6379> zadd k1 1.0 m1 2.0 m2 3.0 m3
(integer) 3
127.0.0.1:6379> zadd k2 1.0 m3 2.0 m4 3.0 m5
(integer) 3
127.0.0.1:6379> zrange k1 0 -1 withscores
1) "m1"
2) "1"
3) "m2"
4) "2"
5) "m3"
6) "3"
127.0.0.1:6379> zrange k2 0 -1 withscores
1) "m3"
2) "1"
3) "m4"
4) "2"
5) "m5"
6) "3"
127.0.0.1:6379> zinterstore d1 2 k1 k2
(integer) 1
127.0.0.1:6379> zrange d1 0 -1 withscores
1) "m3"
2) "4"
127.0.0.1:6379> zinterstore d2 2 k2 k1
(integer) 1
127.0.0.1:6379> zrange d2 0 -1 withscores
1) "m3"
2) "4"
127.0.0.1:6379> zadd k3 3.0 m1 4.0 m3 5.0 m5
(integer) 3
127.0.0.1:6379> zinterstore d3 3 k1 k2 k3
(integer) 1
127.0.0.1:6379> zrange d3 0 -1 withscores
1) "m3"
2) "8"
127.0.0.1:6379> zinterstore d4 3 k3 k2 k1
(integer) 1
127.0.0.1:6379> zrange d4 0 -1 withscores
1) "m3"
2) "8"
127.0.0.1:6379> zinterstore d5 3 k2 k3 k1
(integer) 1
127.0.0.1:6379> zrange d5 0 -1 withscores
1) "m3"
2) "8"

7、 In the ordered set of the same score, calculate the number of members in the specified dictionary interval

Command: zlexcount

Format: zlexcount key min max

Note: 1. For ordered sets with different scores, the value obtained may be incorrect.

2. For min / max, you can write “-” (minimum), “+” (maximum), or start with “[“

127.0.0.1:6379> zadd k1 1 m1 1 m2 1 m3 1 m4 1 m5 1 n1 1 n2 1 n3
(integer) 8
127.0.0.1:6379> zrange k1 0 -1 wighscores
(error) ERR syntax error
127.0.0.1:6379> zrange k1 0 -1 withscores
 1) "m1"
 2) "1"
 3) "m2"
 4) "1"
 5) "m3"
 6) "1"
 7) "m4"
 8) "1"
 9) "m5"
10) "1"
11) "n1"
12) "1"
13) "n2"
14) "1"
15) "n3"
16) "1"
127.0.0.1:6379> zlexcount k1 [m1 [m2
(integer) 2
127.0.0.1:6379> zlexcount k1 [m [n
(integer) 5
127.0.0.1:6379> zlexcount k1 - [n
(integer) 5
127.0.0.1:6379> zlexcount k1 - +
(integer) 8

8、 In the ordered set with the same score, the members of the ordered set are returned through the dictionary interval

Command: zrangebylex

Format: zrangebylex key min max [limit offset count]

127.0.0.1:6379> zadd k1 1 m1 1 m2 1 m3 1 m4 1 m5 1 n1 1 n2 1 n3
(integer) 8
127.0.0.1:6379> zrangebylex k1 [m [n
1) "m1"
2) "m2"
3) "m3"
4) "m4"
5) "m5"
127.0.0.1:6379> zrangebylex k1 [n1 +
1) "n1"
2) "n2"
3) "n3"
127.0.0.1:6379> zrangebylex k1 [m [n LIMIT 2 3
1) "m3"
2) "m4"
3) "m5"
127.0.0.1:6379> zrangebylex k1 [n1 + LIMIT 2 3
1) "n3"

9、 Returns the members in the specified interval of an ordered set through scores

Command: zrangebyscore

Format: zrangebyscore key min max [withscores] [limit offset count]

127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e
(integer) 5
127.0.0.1:6379> zrangebyscore k1 1 2
1) "a"
2) "b"
127.0.0.1:6379> zrangebyscore k1 3 5
1) "c"
2) "d"
3) "e"
127.0.0.1:6379> zrangebyscore k1 3 5 withscores
1) "c"
2) "3"
3) "d"
4) "4"
5) "e"
6) "5"
127.0.0.1:6379> zrangebyscore k1 3 5 withscores limit 2 1
1) "e"
2) "5"

10、 Returns the index of the specified member in an ordered collection

Commands: zrank

Format: zrank key member

127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e
(integer) 5
127.0.0.1:6379> zrank k1 b
(integer) 1
127.0.0.1:6379> zrange k1 0 -1 
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> zrank k1 e
(integer) 4

11、 Remove one or more members from an ordered collection

Command: zrem

Format: zrem key member1 [member2]

127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e
(integer) 5
127.0.0.1:6379> zrange k1 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> zrem k1 a b c
(integer) 3
127.0.0.1:6379> zrange k1 0 -1
1) "d"
2) "e"

12、 Removes all members of a given dictionary interval in an ordered set of the same score

Command: zremrangebylex

Format: zremrangebylex key min max

127.0.0.1:6379> zadd k1 1 m1 1 m2 1 m3 1 m4 1 m5 1 n1 1 n2 1 n3
(integer) 8
127.0.0.1:6379> zrange k1 0 -1
1) "m1"
2) "m2"
3) "m3"
4) "m4"
5) "m5"
6) "n1"
7) "n2"
8) "n3"
127.0.0.1:6379> zremrangebylex k1 [m [n
(integer) 5
127.0.0.1:6379> zrange k1 0 -1
1) "n1"
2) "n2"
3) "n3"
127.0.0.1:6379> del k1
(integer) 1
127.0.0.1:6379> zadd k1 1 m1 1 m2 1 m3 1 m4 1 m5 1 n1 1 n2 1 n3
(integer) 8
127.0.0.1:6379> zremrangebylex k1 [m [n1
(integer) 6
127.0.0.1:6379> zrange k1 0 -1
1) "n2"
2) "n3"

13、 Removes all members of a given ranking interval from an ordered set

Command: zremrangebyrank

Format: zremrangebyrank key start stop

127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e
(integer) 5
127.0.0.1:6379> zrange k1 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
127.0.0.1:6379> zremrangebyrank k1 2 3
(integer) 2
127.0.0.1:6379> zrange k1 0 -1
1) "a"
2) "b"
3) "e"

14、 Removes all members of a given score interval from an ordered set

Command: zremrangebyscore

Format: zremrangebyscore key min max

127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e
(integer) 5
127.0.0.1:6379> zrange k1 0 -1 withscores
 1) "a"
 2) "1"
 3) "b"
 4) "2"
 5) "c"
 6) "3"
 7) "d"
 8) "4"
 9) "e"
10) "5"
127.0.0.1:6379> zremrangebyscore k1 3 4
(integer) 2
127.0.0.1:6379> zrange k1 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "2"
5) "e"
6) "5"

15、 Returns the members in the specified interval in the ordered set. Through the index, the score is from high to low

Commands: zrevrange

Format: zrevrange key start stop [with scores]

127.0.0.1:6379> zadd k1 1 a 2 b 3 c 4 d 5 e
(integer) 5
127.0.0.1:6379> zrevrange k1 1 3
1) "d"
2) "c"
3) "b"
127.0.0.1:6379> zrevrange k1 1 3 withscores
1) "d"
2) "4"
3) "c"
4) "3"
5) "b"
6) "2"
127.0.0.1:6379> zrange k1 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"

16、 Returns the members within the specified score range in the ordered set, and the scores are sorted from high to low

Command: zrevrangebyscore

Format: zrevrangebyscore key max min [with scores]  [ LIMIT offset count]

127.0.0.1:6379> zrange k1 0 -1 withscores
 1) "a"
 2) "1"
 3) "b"
 4) "2"
 5) "c"
 6) "3"
 7) "d"
 8) "4"
 9) "e"
10) "5"
127.0.0.1:6379> zrevrangebyscore k1 5 3 
1) "e"
2) "d"
3) "c"
127.0.0.1:6379> zrevrangebyscore k1 5 3 withscores
1) "e"
2) "5"
3) "d"
4) "4"
5) "c"
6) "3"
127.0.0.1:6379> zrevrangebyscore k1 5 3 withscores limit 2 1
1) "c"
2) "3"

17、 Returns the ranking of the specified members in the ordered set, and the members of the ordered set are sorted by decreasing points (from large to small)

Commands: zrevrank

Format: zrevrank key member

127.0.0.1:6379> zrange k1 0 -1 withscores
 1) "a"
 2) "1"
 3) "b"
 4) "2"
 5) "c"
 6) "3"
 7) "d"
 8) "4"
 9) "e"
10) "5"
127.0.0.1:6379> zrevrank k1 a
(integer) 4
127.0.0.1:6379> zrevrank k1 b
(integer) 3
127.0.0.1:6379> zrevrank k1 c
(integer) 2
127.0.0.1:6379> zrevrank k1 d
(integer) 1
127.0.0.1:6379> zrevrank k1 e
(integer) 0

18、 Returns the score value of a member in an ordered set

Command: zscore

Format: zscore key member

127.0.0.1:6379> zrange k1 0 -1 withscores
 1) "a"
 2) "1"
 3) "b"
 4) "2"
 5) "c"
 6) "3"
 7) "d"
 8) "4"
 9) "e"
10) "5"
127.0.0.1:6379> zscore k1 a
"1"
127.0.0.1:6379> zscore k1 b
"2"
127.0.0.1:6379> zscore k1 c
"3"
127.0.0.1:6379> zscore k1 d
"4"
127.0.0.1:6379> zscore k1 e
"5"

19、 The union of one or more given ordered sets is calculated and stored in a new key

Command: zunionstore

Format: zunionstore des numbers key1 [key2]

127.0.0.1:6379> zadd k1 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> zadd k2 1 a 2 c 3 e
(integer) 3
127.0.0.1:6379> zadd k3 1 a 2 e 3 f
(integer) 3
127.0.0.1:6379> zunionstore d1 2 k1 k2
(integer) 4
127.0.0.1:6379> zrange d1 0 -1 withscores
1) "a"
2) "2"
3) "b"
4) "2"
5) "e"
6) "3"
7) "c"
8) "5"
127.0.0.1:6379> zunionstore d2 3 k1 k2 k3
(integer) 5
127.0.0.1:6379> zrange d2 0 -1 withscores
 1) "b"
 2) "2"
 3) "a"
 4) "3"
 5) "f"
 6) "3"
 7) "c"
 8) "5"
 9) "e"
10) "5"

20、 Iterate the elements in the ordered set (including element members and element scores)

Command: zscan

Format: zscan key cursor [match pattern] [count count]

127.0.0.1:6379> zadd k1 1 m1 2 m2 3 m3 4 m4 5 m5 6 member1 7 member2 8 member3 9 member4 10 member5
(integer) 10
127.0.0.1:6379> zrange k1 0 -1 withscores
 1) "m1"
 2) "1"
 3) "m2"
 4) "2"
 5) "m3"
 6) "3"
 7) "m4"
 8) "4"
 9) "m5"
10) "5"
11) "member1"
12) "6"
13) "member2"
14) "7"
15) "member3"
16) "8"
17) "member4"
18) "9"
19) "member5"
20) "10"
127.0.0.1:6379> zscan k1 0 match m*
1) "0"
2)  1) "m1"
    2) "1"
    3) "m2"
    4) "2"
    5) "m3"
    6) "3"
    7) "m4"
    8) "4"
    9) "m5"
   10) "5"
   11) "member1"
   12) "6"
   13) "member2"
   14) "7"
   15) "member3"
   16) "8"
   17) "member4"
   18) "9"
   19) "member5"
   20) "10"
127.0.0.1:6379> zscan k1 0 match me*
1) "0"
2)  1) "member1"
    2) "6"
    3) "member2"
    4) "7"
    5) "member3"
    6) "8"
    7) "member4"
    8) "9"
    9) "member5"
   10) "10"

 

Reference link:https://www.runoob.com/redis/redis-strings.html