有序集合存储着成员和分值之间的映射,并且提供了分值处理命令,以及根据分值大小有序的获取或扫描成员和分值的命令
(常用命令)
ZADD : ZADD key-name score member [score member ...] ------------将带有给定分值的成员添加到有序集合里
ZREM : ZREM key-name member [member ...] --------------------------从有序集合里移除给定的成员,并返回被移除成员的数量
ZCARD : ZCARD key-name ------------------------------------------------返回有序集合包含的成员数量
ZINCRBY : ZINCRBY key-name increment member --------------------将member成员的分值加上increment
ZCOUNT : ZCOUNT key-name min max ---------------------------------返回分值介于min和max之间的成员数量
ZRANK : ZRANK key-name member ------------------------------------返回成员member在有序集合中的排名
ZSCORE : ZSCORE key-name member --------------------------------返回成员member的分值
ZRANGE : ZRANGE key-name start stop [WITHSCORE] ------------返回有序集合中排名介于start和stop之间的成员,如果给定了可选的WITHSCORE选项,那么命令会将成员的分值也一并返回
>>> conn.zadd('zset-key','a',3,'b',2,'c',1) 3 >>> conn.zcard('zset-key') 5 >>> conn.delete('zset-key') 1 >>> conn.zadd('zset-key','a',3,'b',2,'c',1) 3 >>> conn.zcard('zset-key') 3 >>> conn.zincrby('zset-key','c',3) 4.0 >>> conn.zscore('zset-key','b') 2.0 >>> conn.zrank('zset-key','c') 2 >>> conn.zcount('zset-key',0,3) 2L >>> conn.zrem('zset-key','c') 1 >>> conn.zrange('zset-key',0,-1,withscores=True) [('b', 2.0), ('a', 3.0)]
(有序集合在一定范围内获取数据和删除数据命令,以及并集和交集命令)
ZREVRNK : ZREVRANK key-name member --------------返回有序集合里成员member的排名,成员按照分值从大到小排列
ZREVRANGE : ZREVRANGE key-name start stop [WHISTSCORES] -----------返回有序集合给定排名范围内的成员,成员按照分值从大到小排列
ZRABGEBYSCORE : ZRABGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] --------返回有序集合中,分值介于min和max之间的所有成员
ZREVRANGEBYSCORE : ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] -----获取有序集合中分值介于min和max之间的所有成员,并按照分值从大到小的顺序返回它们
ZREMRANGEBYRANK : ZREMRANGEBYRANK key-name start stop ---------------------移除有序集合中排名介于start和end之间的所有成员
ZREMRANGEBYSCORE : ZREMRANGEBYSCORE key-name min max -------------------移除有序集合中分值介于min和max之间的所有成员
ZINTERSTORE : ZINTERSTORE dest-key key-count key [key...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]--------------对给定的有序集合执行类似于集合的交集运算
ZUNIONSTORE : ZUNIONSTORE dest-key key-count key [key...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]--------------对给定的有序集合执行类似于集合的并集运算
>>> import redis >>> conn = redis.Redis(host='192.168.146.129',port=6379,db=0) >>> conn.zadd('zset-1','a',1,'b',2,'c',3) 3 >>> conn.zadd('zset-2','b',4,'c',1,'d',0) 3 >>> conn.zinterstore('zset-i',['zset-1','zst-2']) 0L >>> conn.zinterstore('zset-i',['zset-1','zset-2']) 2L >>> conn.zrange('zset-i',0,-1,withscores=True) [('c', 4.0), ('b', 6.0)] >>> conn.zunionstore('zset-u',['zset-1','zset-2'],aggregate='min') 4L