HGETALL在Lua中的返回类型是什么? 答:返回类型为表。

我正在编写一个 Lua 脚本,以下是我的脚本:

local session = redis.call('HGETALL', accessToken)
if (session == nil) then
  redis.log(redis.LOG_WARNING, 'Session not found : ' .. accessToken)
  return
end

我尝试了多个 if 条件,但我无法确认如何适当地验证 value 是否为 null。另外,我不想在调用 EXIST 命令之前增加额外的开销。

我尝试通过 telnet 调用该命令,redis 的响应是 *0

以下是我尝试过的条件:

if (session == nil) then
if (session == '[]') then
if (session == '{}') then
if (session == '*0') then
if (session == '') then
if (session == '(empty list or set)') then
if (not session) then

这些条件都不起作用。有人有想法吗?

点赞
用户258523
用户258523

一些快速的在线搜索似乎暗示HGETALL(和其他返回键/值对的函数)的结果是一个顺序键/值对的表。所以是{"key1","val1","key2","val2"}

这表明空结果(假定它不是nil)将是一个空表(即session[1] == nil)。

2014-11-04 18:11:37
用户1016142
用户1016142

更一般化的方法,同样适用于具有字符串键的表格是 next(table_name) == nil

你的脚本将是:

local session = redis.call('HGETALL', accessToken)
if next(session) == nil then
    redis.log(redis.LOG_WARNING, 'Session not found : ' .. accessToken)
    return
end

当然,在一般情况下,最好还要检查空表 table_name == nil or next(table_nil)

2015-12-16 12:44:16