redis - 为什么redis在lua脚本中为HGET返回 bool 值?

标签 redis lua

这是我的评估命令

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/

相关文章:

redis - 如何对一组字符串进行排序并存储结果

python - flask 缓存 : list keys based on a pattern?

nginx - 在nginx中使用Lua将请求传递给FastCGI

string - 字符串中可能存在的字符的模式

c++ - 在 C/C++ 中引用 Lua 函数

redis - 如何在 REDIS 中标记一个键,以便以后我可以删除所有与该标签匹配的键?

Redis HSETNX 没有引发 key 空间通知

redis - Redis 中的默认 TTL 是多少?

Lua:四舍五入然后截断

c++ - 混合从 C 和 C++ 编译的目标文件时出现链接器错误