【发布时间】:2016-08-09 06:16:00
【问题描述】:
我正在使用 Lua 脚本来读取 redis 存储中哈希键的内容。我的代码如下:
local key = KEYS[1]
-- Check if the user account exists
local accnt = redis.call('hgetall', key);
if next(accnt) == nil then return 404 end
return accnt;
当我运行这个程序时,我得到了正确的结果,如下所示。
[2016-04-17 19:27:07.807] [DEBUG] AuthServer - Loading script ./scripts/debit_script.lua to redis...
[ 'id',
'47',
'accType',
'1',
'creditLimit',
'75000',
'creditConsumed',
'null' ]
但是当我尝试从上面的 lua 表中返回单个值(比如 accnt['id'] 或 accnt.id 时,我得到 null。这是新版本的程序,它通过键访问表中的单个条目,但失败了。
local key = KEYS[1]
-- Check if the user account exists
local accnt = redis.call('hgetall', key);
if next(accnt) == nil then return 404 end
return accnt['id'];
我听说 lua 表是键值对的关联数组。因此,我用于访问表中条目的代码似乎是正确的。不是吗?
我在这里做错了什么,如何正确访问单个密钥?
** 编辑 ** 为了找到“accnt”的类型,我修改了我的代码以返回为
return type(accnt)
结果如下:
[2016-04-17 20:42:02.229] [DEBUG] AuthServer - Loading script ./scripts/debit_script.lua to redis...
table
debit result: table
所以,我想知道当我们查询 redis 哈希时,lua 是否返回常规数组或表。感谢任何人指出正确的解决方案。
【问题讨论】:
-
问题是
accnt其实是一个正则数组。 -
我试图返回 accnt 变量的类型。它以表格形式返回。