有序集合命令——Redis学习笔记

1:ZADD key score member [score member …]
添加一个或多个成员到有序集合,如果该成员已经存在,更新其分数(成员不存在,分数相同时,也会创建新的成员)

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
127.0.0.1:6379> zadd zset1 1 one 2 two 3 three
(integer) 3
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
127.0.0.1:6379> zadd zset1 4 three
(integer) 0
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "4"
127.0.0.1:6379> zadd zset1 4 five
(integer) 1
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "five"
6) "4"
7) "three"
8) "4"

2:ZCARD key
返回key的有序结合元素个数

1
2
127.0.0.1:6379> zcard zset1
(integer) 4

3:ZCOUNT key min max
返回有序集key中,score值在min和max之间(默认包括score值等于min或max)的成员数量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "five"
6) "4"
7) "three"
8) "4"
127.0.0.1:6379> zcard zset1
(integer) 4
127.0.0.1:6379> zcount zset1 1 4
(integer) 4
127.0.0.1:6

3:ZINCRBY key increment member
为有序集合增加或减少成员的分数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
127.0.0.1:6379> zincrby zset1 1 one
"2"
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "one"
2) "2"
3) "two"
4) "2"
5) "five"
6) "4"
7) "three"
8) "4"
127.0.0.1:6379> zincrby zset1 5 one
"7"
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "two"
2) "2"
3) "five"
4) "4"
5) "three"
6) "4"
7) "one"
8) "7"

4:ZINTERSTORE destination numkeys key [key …]
计算给定的numkeys个有序集合的交集,并且把结果放到destination中(默认情况下,结果中一个元素的分数是有序集合中该元素分数之和)

1
2
3
4
5
6
7
8
9
10
11
12
13
127.0.0.1:6379> zadd zset1 1 one 2two
(error) ERR syntax error
127.0.0.1:6379> zadd zset1 1 one 2 two
(integer) 2
127.0.0.1:6379> zadd zset2 1 one 2 two 3 three
(integer) 3
127.0.0.1:6379> zinterstore zset3 2 zset1 zset2
(integer) 2
127.0.0.1:6379> zrange zset3 0 -1 withscores
1) "one"
2) "2"
3) "two"
4) "4"

5:ZRANGE key start stop [WITHSCORES]
返回有序集key中,指定区间内的成员。其中成员按score值递增(从小到大)来排序。具有相同score值的成员按字典序来排列

1
2
3
4
5
127.0.0.1:6379> zrange zset3 0 -1 withscores
1) "one"
2) "2"
3) "two"
4) "4"

6:ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
按分数返回一个成员范围的有序集合。

1
2
3
4
5
6
7
8
9
10
11
12
13
127.0.0.1:6379> zadd zset1 1 one 2 two 3 three 4 four 5 five 6 six 7 seven
(integer) 7
127.0.0.1:6379> zrange zset1 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
5) "five"
6) "six"
7) "seven"
127.0.0.1:6379> zrangebyscore zset1 2 3
1) "two"
2) "three"

7:ZRANK key member
有序集合中,确定成员的索引值

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> zrange zset1 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
5) "five"
6) "six"
7) "seven"
127.0.0.1:6379> zrank zset1 two
(integer) 1

8:ZREM key member [member …]
从有序集合中删除一个或多个成员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
127.0.0.1:6379> zrange zset1 0 -1
1) "one"
2) "two"
3) "three"
4) "four"
5) "five"
6) "six"
7) "seven"
127.0.0.1:6379> zrem zset1 one two
(integer) 2
127.0.0.1:6379> zrange zset1 0 -1
1) "three"
2) "four"
3) "five"
4) "six"
5) "seven"

9:ZREMRANGEBYRANK key start stop
移除有序集集合中,指定索引(rank)区间内的所有成员

1
2
3
4
5
6
7
8
9
10
11
12
127.0.0.1:6379> zrange zset1 0 -1
1) "three"
2) "four"
3) "five"
4) "six"
5) "seven"
127.0.0.1:6379> zremrangebyrank zset1 2 3
(integer) 2
127.0.0.1:6379> zrange zset1 0 -1
1) "three"
2) "four"
3) "seven"

10:ZREMRANGEBYSCORE key min max
移除有序集集合中,指定分数(score)区间内的所有成员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "four"
8) "4"
127.0.0.1:6379> zremrangebyscore zset1 3 4
(integer) 2
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"

11:ZREVRANGE key start stop [WITHSCORES]
返回一个成员范围的有序集合,索引降序排序

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
127.0.0.1:6379> zrevrange zset1 0 -1 withscores
1) "two"
2) "2"
3) "one"
4) "1"

12:ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
返回一个成员范围的有序集合,分数降序排序

1
2
3
4
5
6
7
8
9
10
11
127.0.0.1:6379> zrange zset1 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> zrevrangebyscore zset1 0 -1
(empty list or set)
127.0.0.1:6379> zrevrangebyscore zset1 1 2
(empty list or set)
127.0.0.1:6379> zrevrangebyscore zset1 2 1
1) "two"
2) "one"

13:ZREVRANK key member
返回一个反转的有序集合中成员member的排名

1
2
3
4
5
6
7
8
127.0.0.1:6379> zrange zset1 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> zrevrank zset1 one
(integer) 2
127.0.0.1:6379> zrevrank zset1 three
(integer) 0

14:ZSCORE key member
获取一个有序集合中指定成员的分数

1
2
3
4
5
6
7
8
127.0.0.1:6379> zrange zset1 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> zscore zset1 one
"1"
127.0.0.1:6379> zscore zset1 two
"2"

15:ZUNIONSTORE destination numkeys key [key …]
获取多个指定数量的有序集合的并集,并存储在新的有序集合中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
127.0.0.1:6379> zadd zset1 1 one 2 two
(integer) 2
127.0.0.1:6379> zadd zset2 2 two 3 three
(integer) 2
127.0.0.1:6379> zadd zset3 3 three 4 four 5 five
(integer) 3
127.0.0.1:6379> zunionstore zset4 3 zset1 zset2 zset3
(integer) 5
127.0.0.1:6379> zrange zset4 0 -1
1) "one"
2) "four"
3) "two"
4) "five"
5) "three"