redis基本操作 —— sorted set

Redis 有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。

不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。

有序集合的成员是唯一的,但分数(score)却可以重复。

 

一、向有序集合添加一个或多个成员,或者更新已存在成员的分数

  命令:zadd

  格式: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

二、通过索引区间返回有序集合指定区间内的成员

  命令:zrange

  格式:zrange key start stop [WITHSCORES]

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"

三、获取有序集合的成员数

  命令:zcard

  格式: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"

四、计算在有序集合中指定区间分数的成员数

  命令:zcount

  格式: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

五、有序集合中对指定成员的分数加上增量 increment

  命令:zincrby

  格式:zincrby 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"

六、计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中

  命令:zinterstore

  格式: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"

七、在相同分数的有序集合中,计算指定字典区间内成员数量

  命令:zlexcount

  格式:zlexcount key min max

  说明:1、对于分数不相同的有序集合使用时,得到的值可能不正确。

             2、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 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

八、在相同分数的有序集合中,通过字典区间返回有序集合的成员

  命令:zrangebylex

  格式: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"

九、通过分数返回有序集合指定区间内的成员

  命令:zrangebyscore

  格式: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"

十、返回有序集合中指定成员的索引

  命令:zrank

  格式: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

十一、移除有序集合中的一个或多个成员

  命令:zrem

  格式: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"

十二、移除相同分数的有序集合中给定的字典区间的所有成员

  命令:zremrangebylex

  格式: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"

十三、移除有序集合中给定的排名区间的所有成员

  命令:zremrangebyrank

  格式: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"

十四、移除有序集合中给定的分数区间的所有成员

  命令:zremrangebyscore

  格式: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"

十五、返回有序集中指定区间内的成员,通过索引,分数从高到低

  命令:zrevrange

  格式:zrevrange key start stop [WITHSCORES]

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"

十六、返回有序集中指定分数区间内的成员,分数从高到低排序

  命令:zrevrangebyscore

  格式:zrevrangebyscore key max min [WITHSCORES] [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"

十七、返回有序集合中指定成员的排名,有序集成员按分数值递减(从大到小)排序

  命令:zrevrank

  格式: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

十八、返回有序集中,成员的分数值

  命令:zscore

  格式: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"

十九、计算给定的一个或多个有序集的并集,并存储在新的 key 中

  命令:zunionstore

  格式: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"

二十、迭代有序集合中的元素(包括元素成员和元素分值)

  命令:zscan

  格式: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"

 

参考链接:https://www.runoob.com/redis/redis-strings.html

 

posted @ 2020-06-06 18:09  曾经时光  阅读(1063)  评论(0编辑  收藏  举报