zset类型以及其操作
Posted guoDaXia的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zset类型以及其操作相关的知识,希望对你有一定的参考价值。
sorted set类型
sorted sets类型以及其操作
zset是set的一格升级版本,它在set的基础上增加了一格顺序属性,这一属性在添加元素的同时可以指定,每次指定后,zset会自动重新按照新的值调整顺序。可以理解为有两列的mysql表,一列存储value,一列存储顺序。操作中key理解成zset的名字。
zadd
向名称为key的zset中添加元素member,score用于排序,如果该元素存在,则更新其顺序
127.0.0.1:6379> zadd key [NX|XX] [CH] [INCR] score member [score member ...] 127.0.0.1:6379> zadd zset 0 one (integer) 1 127.0.0.1:6379> zadd zset -1 two (integer) 1
zrange
遍历集合的元素
127.0.0.1:6379> zrange key start stop [WITHSCORES] //withscores代表的是否显示顺序号 start和stop代表所在的位置的索引。可以这样理解:将集合元素依照顺序值升序排序再输出,start和stop限制遍历的限制范围
127.0.0.1:6379> zadd zset -1 hhh (integer) 1 127.0.0.1:6379> zrange zset 0 0 1) "hhh"。 127.0.0.1:6379> zrange zset 0 -1 withscore 1) "two" 2) "-1" 3) "one" 4) "0"
zset元素也是不重复的,不要将它理解成为map了,score只是附加的,并不是array中的Index,而是代表该元素的顺序号
同一个顺序可以有多个元素:
127.0.0.1:6379> zrange zset 0 -1 withscores
1) "one"
2) "0"
3) "two"
4) "0"
zrem
删除集合中的member元素
127.0.0.1:6379> zrange zset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" 127.0.0.1:6379> zrem zset one (integer) 1 127.0.0.1:6379> zrange zset 0 -1 1) "two" 2) "three"
zincrby
如果在名称为key的zset中已经存在元素member,则该元素的score增加incremenr否则向该集合中添加该元素,其score的值为increment
127.0.0.1:6379> zincrby key increment member 127.0.0.1:6379> zincrby zset 2 two "4" 127.0.0.1:6379> zincrby zset 3 four "3" 127.0.0.1:6379> zrange zset 0 -1 withscores 1) "four" 2) "3" 3) "three" 4) "3" 5) "two" 6) "4" 127.0.0.1:6379> 127.0.0.1:6379> zincrby zset -2 two "2"
zrank
返回名称为key的zset中member元素的排名(类似索引从0开始)(按score从小到大排序)即下标
127.0.0.1:6379> zrank key member 127.0.0.1:6379> zrange zset 0 -1 withscores 1) "two" 2) "2" 3) "four" 4) "3" 5) "three" 6) "3" 127.0.0.1:6379> zrank zset two (integer) 0 127.0.0.1:6379> zrank zset four (integer) 1 127.0.0.1:6379> zrank zset three (integer) 2
注意得到的是排名而不是顺序值score
zrevrank
rev:代表翻转
返回名称为key的zset中member元素的排名(按score从大到小排序),即下标
127.0.0.1:6379> zrange zset 0 -1 withscores 1) "hhh" 2) "-1" 3) "hi" 4) "2" 5) "two" 6) "2" 7) "four" 8) "3" 9) "three" 10) "3" 127.0.0.1:6379> zrank zset hhh (integer) 0 127.0.0.1:6379> zrevrank zset hhh (integer) 4
zrevrange
按照顺序值降序排序然后显示指定下标内的值
127.0.0.1:6379> zrevrange key start stop [WITHSCORES] 127.0.0.1:6379> zrevrange zset 0 -1 withscores 1) "three" 2) "3" 3) "four" 4) "3" 5) "two" 6) "2" 7) "hi" 8) "2" 9) "hhh" 10) "-1" 127.0.0.1:6379> zrange zset 0 -1 withscores 1) "hhh" 2) "-1" 3) "hi" 4) "2" 5) "two" 6) "2" 7) "four" 8) "3" 9) "three" 10) "3"
zrangebyscore
就是利用score升序排序,而是返回集合中score给定区间内的元素。范围内的元素一句score升序排序。start和stop代表的不再是index而是score
127.0.0.1:6379> zrangebyscore key min max [WITHSCORES] [LIMIT offset count] 127.0.0.1:6379> zrange zset 0 -1 withscores 1) "hhh" 2) "-1" 3) "hi" 4) "2" 5) "two" 6) "2" 7) "four" 8) "3" 9) "three" 10) "3" 127.0.0.1:6379> zrangebyscore zset -1 2 withscores 1) "hhh" 2) "-1" 3) "hi" 4) "2" 5) "two" 6) "2"
zcount
返回集合中score指定区间中元素的数量
127.0.0.1:6379> zcount key min max 127.0.0.1:6379> zcount zset -1 2 (integer) 3
zcard
返回集合中的集合元素
127.0.0.1:6379> zcard zset (integer) 5
zremrangebyrank
删除集合中排名在指定区间内的元素,排名意思是index,排序值是score
127.0.0.1:6379> zremrangebyrank key start stop 127.0.0.1:6379> zrange zset 0 -1 withscores 1) "hhh" 2) "-1" 3) "hi" 4) "2" 5) "two" 6) "2" 7) "four" 8) "3" 9) "three" 10) "3" 127.0.0.1:6379> zremrangebyrank zset 0 2 (integer) 3 127.0.0.1:6379> zrange zset 0 -1 withscores 1) "four" 2) "3" 3) "three" 4) "3"
remrangebyscore
删除集合中排序值在指定区间的元素
127.0.0.1:6379> zrangebyscore key min max [WITHSCORES] [LIMIT offset count] 127.0.0.1:6379> zrange zset 0 -1 withscores 1) "four" 2) "3" 3) "three" 4) "3" 127.0.0.1:6379> zremrangebyscore zset 0 1 (integer) 0 127.0.0.1:6379> zremrangebyscore zset 3 3 (integer) 2 127.0.0.1:6379> zrange zset 0 -1 withscores (empty list or set)
以上是关于zset类型以及其操作的主要内容,如果未能解决你的问题,请参考以下文章