这是我的评估命令
eval "local b = redis.call('hget', 'foo', 'baz'); if (type(b) == 'boolean') then return 'boolean' else return 'not a boolean' end" 0 "hello"
这将返回 bool 值
。但如果我返回 b
的值,我会得到 (nil)
。这里发生了什么?
最佳答案
来自https://gigacrunch.herokuapp.com/commands/hget
the value associated with field, or nil when field is not present in the hash or key does not exist
Redis 和 Lua 值之间有一些转换规则 https://cndoc.github.io/redis-doc-cn/cn/commands/eval.html
Redis nil
转换为 Lua false
。所以 b
是假的。因此 type(b)
是 bool 值,因此您返回字符串 'boolean'
。
如果返回b
,则返回false
。 Lua false
转换为 Redis nil
。
关于redis - 为什么redis在lua脚本中为HGET返回 bool 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71033029/