Redis Lua 脚本 redis.call('get', 'nonexisting') 返回 value

似乎以下 Redis Lua 脚本返回 false 而不是 nil,这与文档的描述相矛盾:

> eval "local r = redis.call('get', 'none'); if r==nil then return 42 end" 0
(nil)
> eval "local r = redis.call('get', 'none'); if r==false then return 42 end" 0
(integer) 42
> eval "local r = redis.call('get', 'none'); if not r then return 42 end" 0
(integer) 42

第一个 eval 在条件 r==nil 上失败了,第二个 eval 似乎证明了返回值是 false

似乎使用 not r 是我手头上最安全的选择,但是这个文档 here 中说 GET 命令会返回 nil

这是否是每个人都观察到并依赖于的一个事实,即检查返回 nil 的最安全的 Redis Lua 脚本使用 not r?

点赞
用户1559050
用户1559050

好的,深入研究了 文档,Redis 的 nil 在 Lua 脚本中确实可以转换为 false

Redis 到 Lua 的转换表格。

  • Redis 整数回复 -> Lua 数字
  • Redis 字符串回复 -> Lua 字符串
  • Redis 多字符串回复 -> Lua 表格(可能包含其他 Redis 数据类型嵌套)
  • Redis 状态回复 -> Lua 表格,包含一个 ok 字段,其值为状态
  • Redis 错误回复 -> Lua 表格,包含一个 err 字段,其值为错误
  • Redis 的 Nil 字符串回复和 Nil 多字符串回复 -> Lua 的 false 布尔类型
2017-02-08 22:14:49