【问题标题】:Calling Redis zunionstore from Lua with variable KEYS使用变量 KEYS 从 Lua 调用 Redis zunionstore
【发布时间】:2014-06-04 02:25:29
【问题描述】:

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

提前致谢!

【问题讨论】:

  • 您是否尝试过使用实际参数调用脚本?在您的示例中,您没有传递任何内容,因此您实际上是在没有键的情况下调用 zunionstore。
  • 嘿 - 我不知道运行时的密钥,所以我不确定如何将它们传递给 LUA 脚本(如果这就是你要问的......)跨度>
  • 我的意思是——你确定“重量”里面有什么吗?
  • 是的,已验证。不过还是谢谢。
  • 当然 - 没有实际测试(现在没有我的 lappy 和我)我冒险猜测将 args 传递给 zunionstore 需要使用表格而不是作为连接字符串来完成。

标签: scripting lua redis in-memory-database set-theory


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 2017-01-15
    • 2021-12-01
    相关资源
    最近更新 更多