上文中介绍了Redis中的Set集合,Set集合是无序的不可重复的。而我们本文要介绍的ZSet其实是在Set的基础上绑定了一个score来实现集合数据按照score排序的集合。
有序集合和集合一样也是string类型元素的集合,且不允许重复的成员。
不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。
有序集合的成员是唯一的,但分数(score)却可以重复。
ZAdd
向有序集合中添加一个或者多个元素(分数/元素),如果元素已经存在,则更新该元素的分数,并调整到对应的位置。按分数从小到大排列
127.0.0.1:6379> zadd student 60 a1 70 a2 80 a3 90 a4 (integer) 4
ZScore
获取有序集合中元素对应的分数值
127.0.0.1:6379> zscore student a1 "60" 127.0.0.1:6379> zscore student a4 "90"
ZRange
获取集合中指定的元素信息,如果加上withscores参数则会连同分数一并返回
127.0.0.1:6379> zrange student 0 -1 1) "a1" 2) "a2" 3) "a3" 4) "a4" 127.0.0.1:6379> zrange student 0 -1 withscores 1) "a1" 2) "60" 3) "a2" 4) "70" 5) "a3" 6) "80" 7) "a4" 8) "90"
ZRevRange
和zrange命令类似,只是结果倒序显示
127.0.0.1:6379> zrevrange student 0 3 1) "a4" 2) "a3" 3) "a2" 4) "a1" 127.0.0.1:6379> zrevrange student 0 3 withscores 1) "a4" 2) "90" 3) "a3" 4) "80" 5) "a2" 6) "70" 7) "a1" 8) "60"
ZCard
返回集合中元素的个数
127.0.0.1:6379> zcard student (integer) 4
ZCount
统计集合中元素的分数在min和max之间的个数,如果不需要保持min或者max,在其前面加(即可,如下
127.0.0.1:6379> zcount student 60 90 (integer) 4 127.0.0.1:6379> zcount student 60 (90 (integer) 3 127.0.0.1:6379> zcount student (60 90 (integer) 3 127.0.0.1:6379> zcount student (60 (90 (integer) 2
ZRangeByScore
可以根据score范围来查找集合中的元素,加上withscores也可以一并查询出对应的分数。
127.0.0.1:6379> zrangebyscore student 60 90 1) "a1" 2) "a2" 3) "a3" 4) "a4" 127.0.0.1:6379> zrangebyscore student 60 90 withscores 1) "a1" 2) "60" 3) "a2" 4) "70" 5) "a3" 6) "80" 7) "a4" 8) "90" 127.0.0.1:6379> zrangebyscore student (60 (90 withscores 1) "a2" 2) "70" 3) "a3" 4) "80"
ZRank
获取元素在集合中的排名,从小到大排序,最小的排名是0,不存在的返回 nil
127.0.0.1:6379> zrank student a3 (integer) 2 127.0.0.1:6379> zrank student a66 (nil) 127.0.0.1:6379> zrank student a1 (integer) 0
ZRevRank
获取元素在集合中的排名,从大到小排名,和ZRank命令刚好相反
127.0.0.1:6379> zrevrank student a3 (integer) 1 127.0.0.1:6379> zrevrank student a1 (integer) 3
ZIncrBy
给集合中的元素增加分数,如果元素不存在则新建元素,并设置分数初始为0然后在增加设置的分数。
127.0.0.1:6379> zrange student 0 -1 withscores 1) "a1" 2) "60" 3) "a2" 4) "70" 5) "a3" 6) "80" 7) "a4" 8) "90" 127.0.0.1:6379> zincrby student 5 a1 "65" 127.0.0.1:6379> zrange student 0 -1 withscores 1) "a1" 2) "65" 3) "a2" 4) "70" 5) "a3" 6) "80" 7) "a4" 8) "90" 127.0.0.1:6379> zincrby student 5 aa "5" 127.0.0.1:6379> zrange student 0 -1 withscores 1) "aa" 2) "5" 3) "a1" 4) "65" 5) "a2" 6) "70" 7) "a3" 8) "80" 9) "a4" 10) "90"
ZInterStore
计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中
127.0.0.1:6379> zadd s1 2 a1 3 a2 4 a3 (integer) 3 127.0.0.1:6379> zadd s2 5 a1 6 a4 7 a3 (integer) 3 127.0.0.1:6379> zinterstore s3 2 s1 s2 (integer) 2 127.0.0.1:6379> zrange s3 0 -1 withscores 1) "a1" 2) "7" 3) "a3" 4) "11"
还可以在命令后跟上权重值,score会乘以该权重值。
127.0.0.1:6379> zinterstore s5 2 s1 s2 weights 3 1 (integer) 2 127.0.0.1:6379> zrange s5 0 -1 withscores 1) "a1" 2) "11" 3) "a3" 4) "19"
ZRem
从集合中弹出一个元素
127.0.0.1:6379> zrange s5 0 -1 withscores 1) "a1" 2) "11" 3) "a3" 4) "19" 127.0.0.1:6379> zrem s5 a1 (integer) 1 127.0.0.1:6379> zrange s5 0 -1 withscores 1) "a3" 2) "19"
ZLexCount
计算有序集合中指定字典区间内成员数量
127.0.0.1:6379> zadd myzset 0 a 0 b 0 c 0 d 0 e 0 f (integer) 6 127.0.0.1:6379> zlexcount myzset - + (integer) 6 127.0.0.1:6379> zlexcount myzset [b [e (integer) 4
注意 -+表示最小值和最大值,如果我们需要通过元素查找的话需要加[。
ZRangeByLex
获取集合中指定成员区间的元素
127.0.0.1:6379> zlexcount myzset1 - + (integer) 5 127.0.0.1:6379> zlexcount myzset1 [b [e (integer) 4 127.0.0.1:6379> zrangebylex myzset [b [e 1) "b" 2) "c" 3) "d" 4) "e"
~好了本文到此为止