ZADD key score value […score value]

将一个或多个值和score添加到有序集合。

score可以是整数也可以是双精度浮点数

127.0.0.1:6379> zadd test 1 1
(integer) 1
127.0.0.1:6379> zadd test 2 2
(integer) 1
127.0.0.1:6379> zadd test 2.33 4
(integer) 1
127.0.0.1:6379> zadd test 2.333 3
(integer) 1

ZCARD key

返回有序集合的个数

127.0.0.1:6379> zcard test
(integer) 4

ZCOUNT key min max

返回score在min和max区间的元素个数

127.0.0.1:6379> zcount test 0 2
(integer) 1

ZSCORE key value

获取集合value值的score

127.0.0.1:6379> zscore test 3
"2.3330000000000002"
127.0.0.1:6379> zscore test 4
"2.3300000000000001"
127.0.0.1:6379> zscore test 1
"1"

ZINCRBY key increment value

使集合value值的score增加increment

127.0.0.1:6379> zincrby test 1 3
"3.3330000000000002"
127.0.0.1:6379> zscore test 3
"3.3330000000000002"

ZRANGE|ZREVRANGE key start end [WITHSCORES]

获取某个区间的值

zrange:其中成员的位置按 score 值递增(从小到大)来排序。
zrevrange:其中成员的位置按 score 值递增(从大到小)来排序。
withscores:同时获取score

127.0.0.1:6379> zrange test 0 -1
1) "1"
2) "2"
3) "4"
4) "3"
127.0.0.1:6379> zrange test -2 -1
1) "4"
2) "3"
127.0.0.1:6379> zrevrange test 0 5
1) "3"
2) "4"
3) "2"
4) "1"
127.0.0.1:6379> zrevrange test 0 5 withscores
1) "3" # value
2) "3.3330000000000002" # score
3) "4"
4) "2.3300000000000001"
5) "2"
6) "2.3300000000000001"
7) "1"
8) "1"

ZRANGEBYSCORE|ZREVRANGEBYSOCRE key score_min score_max [WITHSCORES]

根据score区间获取值

ZRANGEBYSCORE:其中成员的位置按 score 值递增(从小到大)来排序。score:min max

ZREVRANGEBYSOCRE:其中成员的位置按 score 值递增(从打到小)来排序。score:max min

score_min和score_max可以用-inf和+inf来表示

127.0.0.1:6379> zrangebyscore test 0 5
1) "1"
2) "2"
3) "4"
4) "3"
127.0.0.1:6379> zrangebyscore test -inf +inf withscores #显示整个合集
1) "1"
2) "1"
3) "2"
4) "2.3300000000000001"
5) "4"
6) "2.3300000000000001"
7) "3"
8) "3.3330000000000002"
127.0.0.1:6379> zrangebyscore test -inf 2 withscores # 显示score<=2的数据
1) "1"
2) "1"
3) "5"
4) "2"
127.0.0.1:6379> zrevrangebyscore test +inf -inf withscores #显示整个合集
 1) "3"
 2) "3.3330000000000002"
 3) "4"
 4) "2.3300000000000001"
 5) "2"
 6) "2.3300000000000001"
 7) "5"
 8) "2"
 9) "1"
10) "1"
127.0.0.1:6379> zrevrangebyscore test 2 -inf withscores # 显示score<=2的数据
1) "5"
2) "2"
3) "1"
4) "1"
127.0.0.1:6379> zrevrangebyscore test 2 1 withscores # 显示score>=1且score<=2的数据
1) "5"
2) "2"
3) "1"
4) "1"

ZREM key value

从集合中移除元素value

127.0.0.1:6379> zrem test 3
(integer) 1
127.0.0.1:6379> zrange test 0 -1
1) "1"
2) "5"
3) "2"
4) "4"

ZREMRANGEBYRANK key start end

根据区间从集合中删除元素

127.0.0.1:6379> zremrangebyrank test 1 5
(integer) 3
127.0.0.1:6379> zrange test 0 -1
1) "1"

ZREMRANGEBYSCORE key min max

根据score的大小区间删除集合中的元素

127.0.0.1:6379> zrange test 0 -1
1) "1"
127.0.0.1:6379> zadd test 2 2
(integer) 1
127.0.0.1:6379> zadd test 3 3
(integer) 1
127.0.0.1:6379> zrange test 0 -1
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> zremrangebyscore test 2 3
(integer) 2
127.0.0.1:6379> zrange test 0 -1
1) "1"
127.0.0.1:6379>

ZINTERSCORE destination keynum key1 key2

计算给定的一个或多个有序集的交集,其中给定 key 的数量必须以 numkeys 参数指定,并将该交集(结果集)储存到 destination 。

默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之和.

weights:给每个集合的score一个乘法因子,默认为1

127.0.0.1:6379> zrange stu1 0 -1 withscores
1) "Alen"
2) "70.5"
3) "Kobe"
4) "90"
5) "Sam"
6) "100"
127.0.0.1:6379> zrange stu2 0 -1 withscores
1) "Sam"
2) "90.799999999999997"
3) "Alen"
4) "100"
5) "James"
6) "100"
7) "Kobe"
8) "100"
127.0.0.1:6379> zinterstore new_stu 2 stu1 stu2
(integer) 3
127.0.0.1:6379> zrange new_stu 0 -1 withscores
1) "Alen"
2) "170.5"
3) "Kobe"
4) "190"
5) "Sam"
6) "190.80000000000001"
127.0.0.1:6379> zinterstore new_stu2 2 stu1 stu2 weights 1 2
(integer) 3
127.0.0.1:6379> zrange new_stu2 0 -1 withscores
1) "Alen"
2) "270.5"
3) "Sam"
4) "281.60000000000002"
5) "Kobe"
6) "290"
127.0.0.1:6379> zinterstore new_stu3 2 stu1 stu2 weights 2 2
(integer) 3
127.0.0.1:6379> zrange new_stu3 0 -1 withscores
1) "Alen"
2) "341"
3) "Kobe"
4) "380"
5) "Sam"
6) "381.60000000000002"