local key = "rate.limit:" .. KEYS[1]
local limit = tonumber(ARGV[1])
local current = tonumber(redis.call('get', key) or "0")
if current + 1 > limit then
return 0
else
redis.call("INCR", key)
redis.call("expire", key,ARGV[2])
return current + 1
end
上面脚本,在linux,redis集群下执行会报错:
Lua script attempted to access a non local key in a cluster node
修改脚本为:
local key = KEYS[1]
local limit = tonumber(ARGV[1])
local current = tonumber(redis.call('get', key) or "0")
if current + 1 > limit then
return 0
else
redis.call("INCR", key)
redis.call("expire", key,ARGV[2])
return current + 1
end
可正常执行
避免踩坑
在redis-cli命令行中执行lua脚本的时候,要注意 KEY和ARGV的位置
在lock_testLock后面没有空格的话会报错:
(error) CROSSSLOT Keys in request don't hash to the same slot
正确的应该是: