问题
I have a lua script that needs to call a zunionstore on a variable number of keys. I'm trying to execute the following code:
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)
The important lines being:
cmd = cmd .. args[i] .. ":weight "
Which builds the list of keys, and the actual call:
redis.call("zunionstore", "p2_test_set", count, cmd)
However, when executing I get the following error:
redis-cli EVAL "$(cat p2.lua)" 0
(error) ERR Error running script (call to f_6dc6501103ea64a02798af1cc9132f8337cdcad4): @user_script:9: ERR syntax error
So, how do I pass a variable number of keys, computed within the lua script, to the redis.call("zunionstore"...) command?
Thanks in advance!
回答1:
The way I solved this is:
for i=0,#array,1 do
local tmp = {'zunionstore', key, #array[i], unpack(array[i])}
redis.call(unpack(tmp))
end
来源:https://stackoverflow.com/questions/24027995/calling-redis-zunionstore-from-lua-with-variable-keys