【发布时间】:2020-04-20 16:59:16
【问题描述】:
抱歉标题含糊。希望下面的例子能解释我的困惑。
尝试在 Redis 中评估以下四个 Lua 脚本中的每一个。我的问题是为什么如果 HMGET 返回 nil 时将值设置为 0 的 ternary operation 在第四个脚本中不起作用。
local bulk = {nil,nil,nil}
return bulk[1] -- nil (good)
-- Proof that ternary operator works
local bulk = {nil,nil,nil}
local rate = (bulk[1] == nil and 0 or bulk[1])
return rate -- 0 (good)
-- Proof that first element in HMGET results is nil
local bulk = redis.call('hmget', 'k1', 'f1', 'f2')
return bulk[1] -- nil (good)
-- Why does ternary op fail on HMGET results?
local bulk = redis.call('hmget', 'k1', 'f1', 'f2')
local rate = (bulk[1] == nil and 0 or bulk[1])
return rate -- nil (bad, want 0)
【问题讨论】: