1
votes

I have following Redis LUA script:

eval "if redis.call('ZSCORE', KEYS[1], ARGV[1]) == nil then return 'true' else return 'false' end" 1 stats test

In return I always get 'false', although that following command returns nil:

zscore stats test

Result:

(nil)

Expected result of the first command would be 'true'. I've changed key and argument of the first command to random values and I always get 'false'. Can anyone help me with that? I'm using Redis 2.8.17.

1

1 Answers

4
votes

Please refer to the documentation regarding type conversion, specifically this line:

  • Redis Nil bulk reply and Nil multi bulk reply -> Lua false boolean type

That means that for your script to "work" as expected you'll need to change it as follows:

eval "if redis.call('ZSCORE', KEYS[1], ARGV[1]) == false then return 'true' else return 'false' end" 1 stats test

Note that:

  1. Redis' protocol does not have a Boolean type so you're basically returning the strings 'true' or 'false'.
  2. The logic is counter intuitive (if false then true) ;)

BTW, although Lua doesn't sport the ternary operator, it can be simulated with the following equivalent:

eval "return redis.call('ZSCORE', KEYS[1], ARGV[1]) and 'false' or 'true'" 1 stats test