使用Lua从Redis调用带有变量KEYS的zunionstore。

我有一个 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"...) 命令呢?

提前致谢!

点赞
用户237696
用户237696

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

for i=0,#array,1 do
    local tmp = {'zunionstore', key, #array[i], unpack(array[i])}
    redis.call(unpack(tmp))
end
2014-06-10 18:28:23