Dejavu
Dejavu

Reputation: 1427

Redis Lua script redis.call('get', 'nonexisting') return value

It seems the following Redis Lua script returns false rather than nil, which is contradicting to what the document says:

> 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

the first eval failed at the condition r==nil, the second eval seems to prove that the return value is false

It seems using not r is the safest option I have at hand, but the documentation here says that the GET command would return nil

Is this something everyone else has observed and relying upon as a fact that the safest Redis Lua script checking for commands returning nil is to use not r?

Upvotes: 5

Views: 5638

Answers (1)

Dejavu
Dejavu

Reputation: 1427

Ok, dug deeper in the documentation, Redis nil does convert to false in Lua scripts:

Redis to Lua conversion table.

  • Redis integer reply -> Lua number
  • Redis bulk reply -> Lua string
  • Redis multi bulk reply -> Lua table (may have other Redis data types nested)
  • Redis status reply -> Lua table with a single ok field containing the status
  • Redis error reply -> Lua table with a single err field containing the error
  • Redis Nil bulk reply and Nil multi bulk reply -> Lua false boolean type

Upvotes: 11

Related Questions