I’ve a Redis cluster v7.x with 3 masters each having a slave.
I’ve defined a Lua function as following:
local keyGet = redis.call('GET', timeSlotKey)
local currentValue = 0
if keyGet ~= nil then
currentValue = tonumber(keyGet)
else
currentValue = 0
end
redis.log(redis.LOG_NOTICE, currentValue)
if currentValue < 5 then
redis.call('INCR', timeSlotKey)
redis.call('EXPIRE', timeSlotKey, expiryDuration)
return 1
else
return 0
end
When I call the function from command line it throws an error:
attempt to compare nil with number
at line if currentValue < 5 then
What is the issue here? How to use the return value of GET call from the Redis?
2
Answers
Finally, after trying other things, I added
redis.setresp(3)
inside the lua function and it is working now.
The issue is that currentValue is set to nil in the call to tonumber(keyGet).
From the lua manual for tonumber:
So, even though keyGet is not nil, whatever it is, it is still not convertible into a number.
Add a line to log the value of keyGet after local keyGet = redis.call(…)
redis.log(redis.LOG_NOTICE, keyGet)
Hopefully, that will tell you why the value of keyGet isn’t working with tonumber.