小编典典

使用变量KEYS从Lua调用Redis zunionstore

redis

我有一个lua脚本,需要在可变数量的键上调用zunionstore。我正在尝试执行以下代码:

local args = redis.call("zrange", "weight", 0, -1, "WITHSCORES")
local r,w
local count = 0
local cmd = ' '
for i=1,#args,2 do
    cmd = cmd .. args[i] .. ":weight " -- building up a list of zsets
    count = count + 1
end
redis.call("zunionstore", "p2_test_set", count, cmd)

重要的几行是:

cmd = cmd .. args[i] .. ":weight "

生成键列表和实际调用:

redis.call("zunionstore", "p2_test_set", count, cmd)

但是,执行时出现以下错误:

redis-cli EVAL "$(cat p2.lua)" 0
(error) ERR Error running script (call to f_6dc6501103ea64a02798af1cc9132f8337cdcad4): @user_script:9: ERR syntax error

那么,如何将在lua脚本中计算出的可变数量的键传递给redis.call(“ zunionstore” …)命令?

提前致谢!


阅读 498

收藏
2020-06-20

共1个答案

小编典典

我解决这个问题的方法是:

for i=0,#array,1 do
    local tmp = {'zunionstore', key, #array[i], unpack(array[i])}
    redis.call(unpack(tmp))
end
2020-06-20