使用变量KEYS从Lua调用Redis zunionstore [英] Calling Redis zunionstore from Lua with variable KEYS

查看:218
本文介绍了使用变量KEYS从Lua调用Redis zunionstore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

重要的几行是:

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

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

So, how do I pass a variable number of keys, computed within the lua script, to the redis.call("zunionstore"...) command?

提前谢谢!

推荐答案

我解决的方法是:

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

这篇关于使用变量KEYS从Lua调用Redis zunionstore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆