HMGET与lua-resty-redis中的数组

HMGET with an array in lua-resty-redis

我要替换此命令:

1
red:hmget('item', 'item:1', 'item:2')

类似:

1
2
local test = {'item:1', 'item:2'}
red:hmget('item', test)

但是,尝试此操作时出现错误(string expected, got table)。如何在Lua for Redis中格式化此格式?


根据您使用的Lua版本,您将要使用unpack

  • Lua 5.1 red:hmget('item', unpack(test))
  • Lua 5.2 red:hmget('item', table.unpack(test))

unpack是一个将数组样式表分解为函数的函数,就好像您像使用一组参数一样使用它。它有点类似于您可能会在其他语言中找到的splat运算符。

1
2
> =unpack{'item:1', 'item:2'}
item:1  item:2