Calling Redis zunionstore from Lua with variable KEYS

℡╲_俬逩灬. 提交于 2020-01-06 02:34:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!