ZADD
ZADD key [NX|XX] [CH] [INCR]score member [score member …]
将元素及对应分值添加到一个有序集合中
NX:不更新已经存在的key,只增加新元素
XX:只更新已经存在的key,不增加新元素
CH:abbr:changed.不指定时只返回新增的元素个数,指定时返回新增的和更新的元素个数之和
INCR:参考zincrby
1 | //通过第二个参数区分是zadd还是zincrby |
1 | /* This generic command implements both ZADD and ZINCRBY. */ |
ZINCRBY
ZINCRBY key increment member
如果key存在,就给相应member的score增加increment
否则直接给key设置分数为increment
1 |
|
ZCARD
ZCARD key
返回有序集合的元素个数
1 | void zcardCommand(client *c) { |
1 | unsigned int zsetLength(const robj *zobj) { |
ZCOUNT
ZCOUNT key min max
返回key中score值在min和max之间的元素个数
其中min和max可以加(,如 zcount key (5 (10
加左括号表示不包含。不加表示包含
1 | void zcountCommand(client *c) { |
ZRANGEBYSCORE
ZRANGEBYSCORE key min max [WITHSCORES][LIMIT offset count]
获取有序结合中分值位于 min和max之间的所有元素
withscores:将member 和 score一起返回
limit offset count:从偏移offset开始获取count个元素
min和max可以为 -inf,+inf,分别表示负无穷和正无穷
1 |
|
1 | /* This command implements ZRANGEBYSCORE, ZREVRANGEBYSCORE. */ |
ZRANK
ZRANK key member
返回有序集合中元素member的rank
以0为起始rank,元素分数从低到高
zrevrank,元素分数从高到低
1 | void zrankGenericCommand(client *c, int reverse) { |
1 | long zsetRank(robj *zobj, sds ele, int reverse) { |
ZREM
ZREM key member [member …]
从有序集合中删除相应的member
1 | void zremCommand(client *c) { |
1 | /* Delete the element 'ele' from the sorted set, returning 1 if the element |