In SpringBoot, I use RedisTemplate to execute the Lua script to implement the interface current limiter. The script is as follows:
local key = KEYS[1]local count = tonumber(ARGV[1])local expiration = tonumber(ARGV[2])if redis.call('EXISTS', key) == 0 then redis.call('SET', key, 1) redis.call('EXPIRE', key, expiration) return 1else local current_count = tonumber(redis.call('GET', key)) if current_count == nil then redis.log(redis.LOG_NOTICE, 'current_count is nil') redis.log(redis.LOG_NOTICE, current_count) return 10 end if current_count >= count then return current_count else redis.call('INCR', key) return current_count + 1 endend
What I don't understand is that it was normal for him to deposit in Redis for the first time, but this error occurred in the second accumulation:attempt to compare nil with number at org.springframework.data.redis.connection.lettuce.lettuceexceptionconverter.convert
But the fetched value should not be a nil, and I also judged whether the current_count is nil or not. Obviously, the program did not enter this judgment, which means it is not nil
But when executing this section of code: if current_count >= count then ,I get an error that tells me that nil cannot be compared with type number? what's the situation? ? ? This surprised me.
Has anyone specified why, please explain, thank you.
no redundant operation.