Redis 得分最高的ZADD成员
有没有可能(或者有另一种惯用的方式)用现有的最高分数加上1的分数来添加一个成员 例如:Redis 得分最高的ZADD成员,redis,add,sortedset,Redis,Add,Sortedset,有没有可能(或者有另一种惯用的方式)用现有的最高分数加上1的分数来添加一个成员 例如: > FLUSHDB > ZADD key 1 one > ZADD key * mem > ZSCORE key mem 1) "2" 您可以使用zrevrange获得最大分数,然后调用zadd将分数设置为最大+1。没有内置的方法可以做到这一点。但是,您可以编写一个用于执行此任务的: local key = KEYS[1] local field = ARGV[1] local sc
> FLUSHDB
> ZADD key 1 one
> ZADD key * mem
> ZSCORE key mem
1) "2"
您可以使用
zrevrange
获得最大分数,然后调用zadd
将分数设置为最大+1
。没有内置的方法可以做到这一点。但是,您可以编写一个用于执行此任务的:
local key = KEYS[1]
local field = ARGV[1]
local score = ARGV[2]
if score then
-- client specifies a score, use it
redis.call('zadd', key, score, field)
else
-- get the largest score in the sorted set
local largest = redis.call('zrevrange', key, 0, 0, 'withscores')
score = largest[2]
if score then
-- update the score
score = score + 1
else
-- the sorted set is empty, set a default score
score = 0
end
redis.call('zadd', key, score, field)
end
试试看:
/src/redis cli——eval t.lua键,字段
您可以使用zrevrange
获得最大分数,然后调用zadd
将分数设置为最大+1
。没有内置的方法可以做到这一点。但是,您可以编写一个用于执行此任务的:
local key = KEYS[1]
local field = ARGV[1]
local score = ARGV[2]
if score then
-- client specifies a score, use it
redis.call('zadd', key, score, field)
else
-- get the largest score in the sorted set
local largest = redis.call('zrevrange', key, 0, 0, 'withscores')
score = largest[2]
if score then
-- update the score
score = score + 1
else
-- the sorted set is empty, set a default score
score = 0
end
redis.call('zadd', key, score, field)
end
试试看:/src/redis cli——eval t.lua键,字段