日期:2014-05-16 浏览次数:20420 次
redis支持有序集合,即sorted set。sorted set在set的基础上,增加了排序属性,是set的升级版。这里简要谈谈sorted set的常用函数:
1)insert
a) zadd
语法:zadd key score member [[score member] [score member] ...]
解释:增加一个或多个member【根据score排序】到有序集key中,如果member已经存在,只更新score。返回增加member个数,不包含已经存在的member
[root@xsf001 ~]# redis-cli redis 127.0.0.1:6379> ZADD score 230 zhangsan (integer) 1 redis 127.0.0.1:6379> zadd score 240 lisi 250 wangwu (integer) 2 redis 127.0.0.1:6379> zadd score 100 liuli redis 127.0.0.1:6379> zadd score 249 wangwu (integer) 0
2)select
a)zrange
语法:zrange key start stop [withscores]
解释:返回有续集key中指定范围【通过索引 start stop】的member[及其score];
redis 127.0.0.1:6379> zrange score 0 -1 #所有元素 1) "liuli" 2) "zhangsan" 3) "lisi" 4) "wangwu" redis 127.0.0.1:6379> zrange score 0 -1 withscores #所有member及score 1) "liuli" 2) "100" 3) "zhangsan" 4) "230" 5) "lisi" 6) "240" 7) "wangwu" 8) "249" redis 127.0.0.1:6379> zrange score 0 2 withscores #前三个元素 1) "liuli" 2) "100" 3) "zhangsan" 4) "230" 5) "lisi" 6) "240" redis 127.0.0.1:6379> zrange score 0 -2 #第一个元素到 1) "liuli" 2) "zhangsan" 3) "lisi"
b)zcount
语法:zcount key min max
解释:统计key中score值介于min 和max之间的member个数
redis 127.0.0.1:6379> zrange score 0 -1 withscores 1) "liuli" 2) "100" 3) "zhangsan" 4) "230" 5) "lisi" 6) "240" 7) "wangwu" 8) "249" redis 127.0.0.1:6379> zcount score 230 240 # count(score >=230 && score <=240) (integer) 2c) zscore
语法:zscore key member
解释:返回有续集key中member的score
redis 127.0.0.1:6379> zscore score liuli "100"
语法:zrevrange key start stop [withscores]
解释:返回有续集key中指定范围[通过索引start stop]的member[及score],返回member根据score按降序排列
redis 127.0.0.1:6379> zrevrange score 0 -1 withscores 1) "wangwu" 2) "249" 3) "lisi" 4) "240" 5) "zhangsan" 6) "230" 7) "liuli" 8) "100" redis 127.0.0.1:6379> zrevrange score 1 -2 1) "lisi" 2) "zhangsan"
e)zrangebyscore
语法:zrangebyscore key min max [withscores] [limit offset count]
解释:返回有续集key中,score大于等于min并且小于等于max的member。返回结果按照score递增的次序排列。可选withscores决定返回结果集中仅仅返回member还是返回member和score;可选参数limit 指定返回结果的数量区间。