首页 > 代码库 > Redis有序集合数据类型操作命令
Redis有序集合数据类型操作命令
sorted sets类型及操作
sorted set是set的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset(有序集合)会自动重新按新的值调整顺序。可以理解为有两列的mysql表,一列存value,一列存顺序。操作中key理解为zset的名字。
zset的方法:
zadd:向名称为key的zset中添加元素member,score用于排序。如果该元素存在,则更新其顺序
127.0.0.1:6379> zadd myzset 1 one (integer) 1 (0.76s) 127.0.0.1:6379> zadd myzset 2 two (integer) 1 127.0.0.1:6379> zadd myzset 3 two (integer) 0 127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "3" 127.0.0.1:6379> zrange myzset 0 -1 1) "one" 2) "two"
zrange:对有序集合中的元素按照score值进行升序排序
127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "3" 127.0.0.1:6379> zrange myzset 0 -1 1) "one" 2) "two"
zrem:删除名为key的zset中元素member
127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "3" 127.0.0.1:6379> zrem myzset two (integer) 1 127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1"
zincrby:如果在名称为key的zset中已经存在元素member,则该元素的score增加increment,否则向该集合中添加该元素,其score的值为increment
127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 127.0.0.1:6379> zincrby myzset 2 one "3" 127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "3" 127.0.0.1:6379> zincrby myzset 2 two "2" 127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "two" 2) "2" 3) "one" 4) "3"
如果增加的幅度值为负数,则表示自减
127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "two" 2) "2" 3) "one" 4) "3" 127.0.0.1:6379> zincrby myzset -2 one "1" 127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2"
zrank:返回名称为key的zset中member元素的排名(按score从小到大排序),即下标(或者说是索引)
127.0.0.1:6379>zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 127.0.0.1:6379> zrank myzset one (integer) 0 127.0.0.1:6379> zrank myzset two (integer) 1
在myzset中one的score为1,two的score为2,而索引是从0开始的,所以one的索引值为0,two的索引值为1.
注意:索引值和score值不相等,索引值比score值小1
zrevrank:返回名称为key的zset中member元素的排名(按score从大到小排序)即小标
127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" 7) "four" 8) "4" 9) "five" 10) "5" 127.0.0.1:6379> zrevrank myzset five (integer) 0 127.0.0.1:6379> zrevrank myzset four (integer) 1 127.0.0.1:6379> zrevrank myzset three (integer) 2
zrevrank和zrank相反,zrevrank先将有序集合中的元素按照score进行降序排序,然后再获取元素的小标索引值
zrevrange:与zrange相反,对有序集合中的元素按照score值进行降序排序
127.0.0.1:6379> zrevrange myzset 0 -1 withscores
1) "five" 2) "5" 3) "four" 4) "4" 5) "three" 6) "3" 7) "two" 8) "2" 9) "one" 10) "1"
zrangebyscore:返回集合中score在给定区间的元素
127.0.0.1:6379> zrevrange myzset 0 -1 withscores 1) "five" 2) "5" 3) "four" 4) "4" 5) "three" 6) "3" 7) "two" 8) "2" 9) "one" 10) "1" 127.0.0.1:6379> zrangebyscore myzset 2 4 withscores 1) "two" 2) "2" 3) "three" 4) "3" 5) "four" 6) "4"
zcount:返回集合中score在给定区间的元素的数量
127.0.0.1:6379> zcount myzset 2 4 (integer) 3
zcard:返回集合中所有元素的个数
127.0.0.1:6379> zcard myzset (integer) 5 127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" 7) "four" 8) "4" 9) "five" 10) "5"
zremrangebyrank:删除集合在给定下标区间的所有元素
127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" 7) "four" 8) "4" 9) "five" 10) "5" 127.0.0.1:6379> zremrangebyrank myzset 1 3 (integer) 3 127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "five" 4) "5"
注意:这个是byrank,即按照所以来删除,不是按照score来删除
zremrangebyscore:删除集合在给定score区间的元素
127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "five" 4) "5" 127.0.0.1:6379> zremrangebyscore myzset 0 1 (integer) 1 127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "five" 2) "5"
本文出自 “zengestudy” 博客,请务必保留此出处http://zengestudy.blog.51cto.com/1702365/1860018
Redis有序集合数据类型操作命令