1

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

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"...) 命令?

提前致谢!

4

1 回答 1

0

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

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