Ashwin
Ashwin

Reputation: 13517

Why does redis return boolean for HGET in lua script?

This is my eval command

eval "local b = redis.call('hget', 'foo', 'baz'); if (type(b) == 'boolean') then return 'boolean' else return 'not a boolean' end" 0 "hello"

This returns boolean. But if I return the value of b I get (nil). What is happening here?

Upvotes: 2

Views: 1516

Answers (1)

Piglet
Piglet

Reputation: 28940

From 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

There are some conversion rules between Redis an Lua values https://cndoc.github.io/redis-doc-cn/cn/commands/eval.html

Redis nil is converted to Lua false. So b is false. Hence type(b) is boolean so you return a string 'boolean'.

If you return b you return false. Lua false is converted to Redis nil.

Upvotes: 2

Related Questions