sorted set类型
sorted sets类型以及其操作
zset是set的一格升级版本,它在set的基础上增加了一格顺序属性,这一属性在添加元素的同时可以指定,每次指定后,zset会自动重新按照新的值调整顺序。可以理解为有两列的mysql表,一列存储value,一列存储顺序。操作中key理解成zset的名字。
zadd
向名称为key的zset中添加元素member,score用于排序,如果该元素存在,则更新其顺序
127.0.0.1:> zadd key [NX|XX] [CH] [INCR] score member [score member ...]
127.0.0.1:> zadd zset one
(integer)
127.0.0.1:> zadd zset - two
(integer)
zrange
遍历集合的元素
127.0.0.1:6379> zrange key start stop [WITHSCORES] //withscores代表的是否显示顺序号 start和stop代表所在的位置的索引。可以这样理解:将集合元素依照顺序值升序排序再输出,start和stop限制遍历的限制范围
127.0.0.1:> zadd zset - hhh
(integer)
127.0.0.1:> zrange zset
) "hhh"。
127.0.0.1:> zrange zset - withscore
) "two"
) "-1"
) "one"
) ""
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:> zrange zset - withscores
) "one"
) ""
) "two"
) ""
) "three"
) ""
127.0.0.1:> zrem zset one
(integer)
127.0.0.1:> zrange zset -
) "two"
) "three"
zincrby
如果在名称为key的zset中已经存在元素member,则该元素的score增加incremenr否则向该集合中添加该元素,其score的值为increment
127.0.0.1:> zincrby key increment member
127.0.0.1:> zincrby zset two
""
127.0.0.1:> zincrby zset four
""
127.0.0.1:> zrange zset - withscores
) "four"
) ""
) "three"
) ""
) "two"
) ""
127.0.0.1:>
127.0.0.1:> zincrby zset - two
""
zrank
返回名称为key的zset中member元素的排名(类似索引从0开始)(按score从小到大排序)即下标
127.0.0.1:> zrank key member
127.0.0.1:> zrange zset - withscores
) "two"
) ""
) "four"
) ""
) "three"
) ""
127.0.0.1:> zrank zset two
(integer)
127.0.0.1:> zrank zset four
(integer)
127.0.0.1:> zrank zset three
(integer)
注意得到的是排名而不是顺序值score
zrevrank
rev:代表翻转
返回名称为key的zset中member元素的排名(按score从大到小排序),即下标
127.0.0.1:> zrange zset - withscores
) "hhh"
) "-1"
) "hi"
) ""
) "two"
) ""
) "four"
) ""
) "three"
) ""
127.0.0.1:> zrank zset hhh
(integer)
127.0.0.1:> zrevrank zset hhh
(integer)
zrevrange
按照顺序值降序排序然后显示指定下标内的值
127.0.0.1:> zrevrange key start stop [WITHSCORES]
127.0.0.1:> zrevrange zset - withscores
) "three"
) ""
) "four"
) ""
) "two"
) ""
) "hi"
) ""
) "hhh"
) "-1"
127.0.0.1:> zrange zset - withscores
) "hhh"
) "-1"
) "hi"
) ""
) "two"
) ""
) "four"
) ""
) "three"
) ""
zrangebyscore
就是利用score升序排序,而是返回集合中score给定区间内的元素。范围内的元素一句score升序排序。start和stop代表的不再是index而是score
127.0.0.1:> zrangebyscore key min max [WITHSCORES] [LIMIT offset count]
127.0.0.1:> zrange zset - withscores
) "hhh"
) "-1"
) "hi"
) ""
) "two"
) ""
) "four"
) ""
) "three"
) ""
127.0.0.1:> zrangebyscore zset - withscores
) "hhh"
) "-1"
) "hi"
) ""
) "two"
) ""
zcount
返回集合中score指定区间中元素的数量
127.0.0.1:> zcount key min max
127.0.0.1:> zcount zset -
(integer)
zcard
返回集合中的集合元素
127.0.0.1:> zcard zset
(integer)
zremrangebyrank
删除集合中排名在指定区间内的元素,排名意思是index,排序值是score
127.0.0.1:> zremrangebyrank key start stop
127.0.0.1:> zrange zset - withscores
) "hhh"
) "-1"
) "hi"
) ""
) "two"
) ""
) "four"
) ""
) "three"
) ""
127.0.0.1:> zremrangebyrank zset
(integer)
127.0.0.1:> zrange zset - withscores
) "four"
) ""
) "three"
) ""
remrangebyscore
删除集合中排序值在指定区间的元素
127.0.0.1:> zrangebyscore key min max [WITHSCORES] [LIMIT offset count]
127.0.0.1:> zrange zset - withscores
) "four"
) ""
) "three"
) ""
127.0.0.1:> zremrangebyscore zset
(integer)
127.0.0.1:> zremrangebyscore zset
(integer)
127.0.0.1:> zrange zset - withscores
(empty list or set)