目录
- 1.概述
- 2.常见操作
- 2.1 ZADD
- 2.2 ZRANGE
- 2.3 ZREVRANGE
- 2.4 ZRANGEBYSCORE
- 2.5 ZSCORE
- 2.6 ZCARD
- 2.6 ZREM
- 2.7 ZINCRBY
- 2.8 ZCOUNT
- 2.9 ZMPOP
- 2.10 ZRANK
- 2.11 ZREVRANK
- 3.总结
1.概述
- ZSet和Set一样也是String类型元素的集合,且不允许重复的成员,不同的是ZSet每个元素都会关联一个double类型的分数,Redis正是通过分数来为集合中的成员进行从小到大的排序。
- ZSet的成员是唯一的,但分数(score)却可以重复。
- ZSet集合是通过哈希表实现的,所以添加,删除,査找的复杂度都是O(1)。
- ZSet集合中最大的成员数为2³²-1。
2.常见操作
2.1 ZADD
向有序集合中添加元素和元素的分数
ZADD key score member [score member ...]
例:
127.0.0.1:6379> zadd zset1 10 v1 20 v2 30 v3 40 v4
(integer) 4
2.2 ZRANGE
遍历,0到-1代表遍历所有,WITHSCORES
结果带着分数
ZRANGE key start stop [WITHSCORES]
例:遍历
127.0.0.1:6379> zrange zset1 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
例:遍历,结果带着分数
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "v1"
2) "10"
3) "v2"
4) "20"
5) "v3"
6) "30"
7) "v4"
8) "40"
2.3 ZREVRANGE
根据分数反转
127.0.0.1:6379> ZREVRANGE zset1 0 -1
1) "v4"
2) "v3"
3) "v2"
4) "v1"
127.0.0.1:6379> ZREVRANGE zset1 0 -1 withscores
1) "v4"
2) "40"
3) "v3"
4) "30"
5) "v2"
6) "20"
7) "v1"
8) "10"
2.4 ZRANGEBYSCORE
获取指定分数范围的元素
ZRANGEBYSCORE key [(]min max [WITHSCORES] [LIMIT offset count]
(
:不包含
min
:分数from
max
:分数to
offset
:开始下标
count
:数量
例:获取分数区间[20, 30]的元素
127.0.0.1:6379> ZRANGE zset1 0 -1 withscores
1) "v1"
2) "10"
3) "v2"
4) "20"
5) "v3"
6) "30"
7) "v4"
8) "40"
127.0.0.1:6379> ZRANGEBYSCORE zset1 20 30
1) "v2"
2) "v3"
127.0.0.1:6379> ZRANGEBYSCORE zset1 20 30 withscores
1) "v2"
2) "20"
3) "v3"
4) "30"
例:获取分数区间(20, 40]的元素
127.0.0.1:6379> ZRANGE zset1 0 -1 withscores
1) "v1"
2) "10"
3) "v2"
4) "20"
5) "v3"
6) "30"
7) "v4"
8) "40"
127.0.0.1:6379> ZRANGEBYSCORE zset1 (20 40
1) "v3"
2) "v4"
例:limit限制返回的数量
127.0.0.1:6379> ZRANGE zset1 0 -1 withscores
1) "v1"
2) "10"
3) "v2"
4) "20"
5) "v3"
6) "30"
7) "v4"
8) "40"
127.0.0.1:6379> ZRANGEBYSCORE zset1 10 40 limit 1 2
1) "v2"
2) "v3"
2.5 ZSCORE
获取元素分数
127.0.0.1:6379> zscore zset1 v3
"30"
2.6 ZCARD
元素个数
127.0.0.1:6379> zcard zset1
(integer) 4
2.6 ZREM
删除某个元素
127.0.0.1:6379> zrem zset1 v3
(integer) 1
2.7 ZINCRBY
为元素member
增加分数increment
ZINCRBY key increment member
例:对元素v1加3分
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "v1"
2) "10"
3) "v2"
4) "20"
5) "v4"
6) "40"
127.0.0.1:6379> zincrby zset1 3 v1
"13"
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "v1"
2) "13"
3) "v2"
4) "20"
5) "v4"
6) "40"
2.8 ZCOUNT
获得指定分数范围内的元素个数
min
: 最小分数
max
: 最大分数
ZCOUNT key min max
例:
127.0.0.1:6379> zrange zset1 0 -1 WITHSCORES
1) "v1"
2) "10"
3) "v2"
4) "20"
5) "v3"
6) "30"
7) "v4"
8) "40"
9) "v5"
10) "50"
11) "v6"
12) "60"
13) "v7"
14) "70"
127.0.0.1:6379> zcount zset1 30 50
(integer) 3
2.9 ZMPOP
7.0新特性,在指定的numkeys个集合中,弹出分数最大(MAX
)或最小(MIN
)的count个元素(分数和值成对),可以实现在一个或多个集合中,取出最小或最大的几个元素
ZMPOP numkeys key [key ...] <MIN | MAX> [COUNT count]
例:在1个zset1集合中,弹出最小的1个元素
127.0.0.1:6379> zrange zset1 0 -1 WITHSCORES
1) "v1"
2) "10"
3) "v2"
4) "20"
5) "v3"
6) "30"
7) "v4"
8) "40"
9) "v5"
10) "50"
11) "v6"
12) "60"
13) "v7"
14) "70"
127.0.0.1:6379> ZMPOP 1 zset1 min count 1
1) "zset1"
2) 1) 1) "v1"
2) "10"
127.0.0.1:6379>
2.10 ZRANK
正序下标,集合的某个元素,正序处于集合第几个
127.0.0.1:6379> zrange zset1 0 -1
1) "v2"
2) "v3"
3) "v4"
4) "v5"
5) "v6"
6) "v7"
127.0.0.1:6379> zrank zset1 v2
(integer) 0
127.0.0.1:6379> zrank zset1 v3
(integer) 1
127.0.0.1:6379> zrank zset1 v4
(integer) 2
127.0.0.1:6379> zrank zset1 v5
(integer) 3
2.11 ZREVRANK
倒序下标,集合的某个元素,倒序处于集合第几个
127.0.0.1:6379> zrange zset1 0 -1
1) "v2"
2) "v3"
3) "v4"
4) "v5"
5) "v6"
6) "v7"
127.0.0.1:6379> zrevrank zset1 v2
(integer) 5
127.0.0.1:6379> zrevrank zset1 v3
(integer) 4
127.0.0.1:6379> zrevrank zset1 v4
(integer) 3
127.0.0.1:6379> zrevrank zset1 v5
(integer) 2
3.总结
排序集合大量应用于项目,例如实时展示热销商品统计,打赏点赞数量排行榜统计。将销量和点赞打赏数作为分数绑定在值上面即可。