Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Redis 得分最高的ZADD成员_Redis_Add_Sortedset - Fatal编程技术网

Redis 得分最高的ZADD成员

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

有没有可能(或者有另一种惯用的方式)用现有的最高分数加上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 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键,字段