TheRiver | blog

You have reached the world's edge, none but devils play past here

0%

redis sorted set

redis有序结合的命令部分熟悉

BZPOPMAX

BZPOPMAX key [key …] timeout

1
2
3
4
5
6
7
8
9
10
11
12
13
127.0.0.1:6379> bzpopmax set2 set3 1    //阻塞1s后返回nil
(nil)
(1.02s)
127.0.0.1:6379> bzpopmax set2 set3 0 //set2和set3都是empty,timeout=0永久阻塞
^C
127.0.0.1:6379> bzpopmax set0 set1 0 //set0和set1都是非空,返回set0的第一个max元素
1) "set0"
2) "b"
3) "20"
127.0.0.1:6379> bzpopmax set2 set1 0 //set2是空,set1非空,返回非空的set1
1) "set1"
2) "a"
3) "1"

BZPOPMIN

同上,一个最大一个最小

ZADD

options

  • XX 只更新已存在的成员,不新增
  • NX 不更新已经存在的成员,总是新增
  • LT 如果新的分数比原来的小则更新,不新增 //ver>=6.2: Added the GT and LT options.
  • GT 如果新的分数比原来的大则更新,不新增 //ver>=6.2: Added the GT and LT options.
  • CH 修改返回值为发生变化的成员个数(区别原来是返回新增的成员个数)
  • INCR 类似ZINCRBY命令,增加成员的分数,只能增加一个成员

return value

  • 默认返回新增的成员数量,不包括更新的 If the INCR option is specified, the return value will be Bulk string reply:
  • The new score of member (a double precision floating point number) represented as string, or nil if the operation was aborted (when called with either the XX or the NX option).

demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
127.0.0.1:6379> ZADD set0 10 a 20 b
(integer) 2
127.0.0.1:6379> ZADD set0 XX 11 a 30 c //更新a,新增c失败
(integer) 0
127.0.0.1:6379> ZRANGE set0 0 -1 WITHSCORES
1) "a"
2) "11"
3) "b"
4) "20"
127.0.0.1:6379> ZADD set0 NX 40 d 12 a //新增d,更新a失败
(integer) 1
127.0.0.1:6379> ZRANGE set0 0 -1 WITHSCORES
1) "a"
2) "11"
3) "b"
4) "20"
5) "d"
6) "40"
127.0.0.1:6379> ZADD set0 LT 10 a 21 b
(error) ERR syntax error
127.0.0.1:6379> ZADD set0 LT 10 a //当前版本不支持
(error) ERR syntax error
127.0.0.1:6379> ZADD set0 CH 12 a 30 c //变化变化的总数
(integer) 2
127.0.0.1:6379> ZRANGE set0 0 -1 WITHSCORES
1) "a"
2) "12"
3) "b"
4) "20"
5) "c"
6) "30"
7) "d"
8) "40"
127.0.0.1:6379> ZADD set0 CH XX 11 a //配合其他参数
(integer) 1
127.0.0.1:6379> ZADD set0 XX 13 a //区别老的返回值
(integer) 0
127.0.0.1:6379> ZADD set0 50.1 e //支持浮点数
(integer) 1
127.0.0.1:6379> ZRANGE set0 0 -1 WITHSCORES
1) "a"
2) "13"
3) "b"
4) "20"
5) "c"
6) "30"
7) "d"
8) "40"
9) "e"
10) "50.100000000000001"

ZCARD

返回key的元素个数,如果key不存在,返回0

1
2
3
4
5
6
127.0.0.1:6379> zadd set1 1 a 2 b 3 c
(integer) 3
127.0.0.1:6379> zcard set1
(integer) 3
127.0.0.1:6379> zcard set10
(integer) 0

ZCOUNT

ZCOUNT key min max

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> zadd set7 1 a 3 b 3 c 4 d
(integer) 4
127.0.0.1:6379> zcount set7 1 4
(integer) 4
127.0.0.1:6379> zcount set7 [1 (4 //不支持[
(error) ERR min or max is not a float
127.0.0.1:6379> zcount set7 1 (4
(integer) 3
127.0.0.1:6379> zcount set7 -inf +inf
(integer) 4

ZINCRBY

ZINCRBY key increment member

给元素的分数增加/递减

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
127.0.0.1:6379> zrange set7 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "3"
5) "c"
6) "3"
7) "d"
8) "4"
127.0.0.1:6379> zincrby set7 2 a
"3"
127.0.0.1:6379> zincrby set7 -1 a
"2"
127.0.0.1:6379> zincrby set7 5 e
"5"

ZINTER

https://redis.io/commands/zinter

ZINTERSTORE

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

取有序集合的交合到一个key中http://redis.cn/commands/zinterstore.html

ZLEXCOUNT

ZLEXCOUNT key min max

计算有序集合元素之间的元素数量,key的元素分数相同才有意义http://redis.cn/commands/zlexcount.html

ZMSCORE

https://redis.io/commands/zmscore

ZPOPMAX

弹出最大的值,默认1个,可加参数调整弹出的个数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
127.0.0.1:6379> zrange set0 0 -1 withscores
1) "a"
2) "16.010000000000002"
3) "b"
4) "20"
5) "g"
6) "70"
7) "g0"
8) "70"
9) "g1"
10) "70"
11) "g2"
12) "70"
13) "g3"
14) "70"
127.0.0.1:6379> zpopmax set0
1) "g3"
2) "70"
127.0.0.1:6379> zpopmax set0 3
1) "g2"
2) "70"
3) "g1"
4) "70"
5) "g0"
6) "70"
127.0.0.1:6379> zrange set0 0 -1 withscores
1) "a"
2) "16.010000000000002"
3) "b"
4) "20"
5) "g"
6) "70"

ZPOPMIN

同上,弹出最小的值,默认1个,可加参数调整弹出的个数

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> zpopmin set0 1
1) "c"
2) "1"
127.0.0.1:6379> zpopmin set0 3
1) "c1"
2) "1"
3) "c2"
4) "1"
5) "c3"
6) "1"

ZRANGE

ZRANGE key start stop [WITHSCORES]

返回一个区间的结果数组,strat从0开始,stop从zcard-1/-1开始,WITHSCORES结果带上score

结果按分数升序排列,相同分数的按key的字典正序排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
127.0.0.1:6379> zrange set1 0 3 withscores  //3大于右边界,退化成2
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"
127.0.0.1:6379> zrange set1 0 0 withscores
1) "a"
2) "1"
127.0.0.1:6379> zrange set1 0 2 withscores
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"
127.0.0.1:6379> zrange set1 0 -1 withscores //-1表示右边第一个值
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"
127.0.0.1:6379> zrange set1 0 -2 withscores //-2表示右边第二个值
1) "a"
2) "1"
3) "b"
4) "2"
127.0.0.1:6379> zrange set1 -1 -2 withscores //左边-1就越界了
(empty list or set)
127.0.0.1:6379> zrange set1 0 -1 withscores //分数升序,字典正序
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"
7) "c1"
8) "3"
9) "c2"
10) "3"

ZRANGEBYLEX

ZRANGEBYLEX key min max [LIMIT offset count]

对分数相同的元素进行ascii排序,通过memcmp函数,如果key中元素分数不一致就不要用

min,max的使用

  • -表示最左边的元素,+表示最右边的元素
  • [是包含,(不包含
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
127.0.0.1:6379> zadd set3 0 aaa 0 aba 0 abc 0 bbb 0 bcb 0 bcc 0 ccc
(integer) 7
127.0.0.1:6379> zrangebylex set3 - + //列出所有
1) "aaa"
2) "aba"
3) "abc"
4) "bbb"
5) "bcb"
6) "bcc"
7) "ccc"
127.0.0.1:6379> zrangebylex set3 - + LIMIT 0 7 //offset count
1) "aaa"
2) "aba"
3) "abc"
4) "bbb"
5) "bcb"
6) "bcc"
7) "ccc"
127.0.0.1:6379> zrangebylex set3 - + LIMIT 1 2
1) "aba"
2) "abc"
127.0.0.1:6379> zrangebylex set3 [a (b //以a开头的
1) "aaa"
2) "aba"
3) "abc"
127.0.0.1:6379> zrangebylex set3 [ab (ac //以ab开头的
1) "aba"
2) "abc"

ZRANGEBYSCORE

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

ZRANGEBYSCORE和ZRANGE配合WITHSCORES还是有区别的.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
127.0.0.1:6379> zadd set4 1 a  2 b  3 c  4 d 5 e 6 f
(integer) 6
127.0.0.1:6379> zrangebyscore set4 - + //不支持这种格式
(error) ERR min or max is not a float
127.0.0.1:6379> zrangebyscore set4 -inf +inf //所有元素
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"
127.0.0.1:6379> zrangebyscore set4 -inf +inf LIMIT 1 2 //limit
1) "b"
2) "c"
127.0.0.1:6379> zrangebyscore set4 -inf +inf WITHSCORES LIMIT 1 2 //withscores
1) "b"
2) "2"
3) "c"
4) "3"
127.0.0.1:6379> zrangebyscore set4 [1 [3 WITHSCORES LIMIT 1 2 //不支持[
(error) ERR min or max is not a float
127.0.0.1:6379> zrangebyscore set4 1 3 WITHSCORES LIMIT 1 2
1) "b"
2) "2"
3) "c"
4) "3"
127.0.0.1:6379> zrangebyscore set4 (1 (3 WITHSCORES LIMIT 1 2
(empty list or set)
127.0.0.1:6379> zrangebyscore set4 (1 (3 WITHSCORES //支持(
1) "b"
2) "2"

ZRANK

ZRANK key member

返回元素按score的排名,从低到高,不存在的话返回nil

1
2
3
4
5
6
7
8
127.0.0.1:6379> zadd set5 1 a 2 b 3 c 4 d 5 e
(integer) 5
127.0.0.1:6379> zrank set5 a
(integer) 0
127.0.0.1:6379> zrank set5 b
(integer) 1
127.0.0.1:6379> zrank set5 k
(nil)

ZREM

ZREM key member [member …]

remove元素,支持一次删除多个(ver>=2.4),返回删除成功的元素个数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
127.0.0.1:6379> zrange set5 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> zrem set5 a b c o
(integer) 3
127.0.0.1:6379> zrange set5 0 -1 withscores
1) "d"
2) "4"
3) "e"
4) "5"
127.0.0.1:6379> zrem set5 p
(integer) 0

ZREMRANGEBYLEX

ZREMRANGEBYLEX key min max

rangeby只对分数相同的key有效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
127.0.0.1:6379> zrange set3 0 -1 withscores
1) "aaa"
2) "0"
3) "aba"
4) "0"
5) "abc"
6) "0"
7) "bbb"
8) "0"
9) "bcb"
10) "0"
11) "bcc"
12) "0"
13) "ccc"
14) "0"
127.0.0.1:6379> zrangebylex set3 - +
1) "aaa"
2) "aba"
3) "abc"
4) "bbb"
5) "bcb"
6) "bcc"
7) "ccc"
127.0.0.1:6379> zremrangebylex set3 [a (b //删除以a开头的
(integer) 3
127.0.0.1:6379> zrange set3 0 -1 withscores
1) "bbb"
2) "0"
3) "bcb"
4) "0"
5) "bcc"
6) "0"
7) "ccc"
8) "0"

ZREMRANGEBYRANK

ZREMRANGEBYRANK key start stop

删除按分数排名的区间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
127.0.0.1:6379> zrange set4 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"
7) "d"
8) "4"
9) "e"
10) "5"
11) "f"
12) "6"
127.0.0.1:6379> zremrangebyrank set4 0 2 //删除前三个
(integer) 3
127.0.0.1:6379> zrange set4 0 -1 withscores
1) "d"
2) "4"
3) "e"
4) "5"
5) "f"
6) "6"
127.0.0.1:6379> zremrangebyrank set4 -3 -2 //删除倒数第2,3个
(integer) 2
127.0.0.1:6379> zrange set4 0 -1 withscores
1) "f"
2) "6"

ZREMRANGEBYSCORE

ZREMRANGEBYSCORE key min max

https://redis.io/commands/zremrangebyscore

ZREVRANGE

同上取反

ZREVRANGEBYLEX

同上取反

ZREVRANGEBYSCORE

同上取反

ZREVRANK

同上取反

ZSCAN

ZSCORE

ZUNION

ZUNIONSTORE

time comlexity

reference

[1]https://redis.io/commands/zrangebylex

[2]http://redis.cn/commands/zrangebyscore.html

----------- ending -----------