javascript - 为 Redis 运行 Lua 脚本的 Node 。如何将排序集从 Lua 脚本返回到 Node?

标签 javascript node.js lua redis

这是文件test.lua中的Lua脚本代码:

local ips_key = 'production:ips'
local ids = redis.call('ZRANGE', ips_key, 0, '+inf', 'WITHSCORES')
local result = {}
for i, name in ipairs(ids) do
    table.insert(result, name)
end
return js.global:Array(table.unpack(result))

我正在使用 ioredis,它是一个用于评估此 lua 脚本的 redis npm。返回值未定义。我究竟做错了什么?谢谢!

最佳答案

上面的脚本有几个问题。

L#2:ZRANGE不接受+inf(或-inf)作为参数,输入应该是有序集合中的索引——将其替换为-1检索整个范围的元素。

L#7:Redis 的 Lua 中没有 js 库,所以它不会工作(而且也不需要)

L#7:table.unpack 是 Lua 5.3,而 Redis 是 5.1 - 在需要时使用 unpack

L#7:不需要解包你返回的结果(如果你这样做,你只会得到数组中的第一个元素,就是这样)

L#1 我知道这是一个测试,但始终使用 KEYS 输入表将您的 key 传递给脚本 - 永远不要在其中硬编码/生成 key 名称。

总而言之,当使用单个键作为输入调用时,这应该“有效”:

local ips_key = KEYS[1]
local ids = redis.call('ZRANGE', ips_key, 0, -1, 'WITHSCORES')
local result = {}
for i, name in ipairs(ids) do
    table.insert(result, name)
end
return result

作为最后的说明,该代码当前“有效”但什么也不做 - 它将调用的结果复制到 ZRANGE 并返回它们。换句话说,回复与仅在不使用 Lua 的情况下定期调用 ZRANGE 是一样的。

关于javascript - 为 Redis 运行 Lua 脚本的 Node 。如何将排序集从 Lua 脚本返回到 Node?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43673148/

相关文章:

javascript - 折线图系列的对象数组? (+ 表示交叉过滤器解决方案)

javascript - RegExp 忽略 <code> 和 <pre> 标签之间的所有内容

javascript - Sequelize : Add attribute column to query with where condition?

node.js - Sequelize : Error creating a post and associating it with the current user

java - 92khz 到 192khz 音频还有什么可以做的吗?

lua - 如何使我的 Gideros 游戏适应所有屏幕尺寸?

javascript - EXTJS 3.X - 防止 TreePanel 自动加载

javascript - 将 ParticlesJ 集成到 View 组件中

javascript - 使用 multer 将音频文件上传到 express js 服务器

lua - 如何为 Windows 10 安装 luarocks