lua使用redis命令和';setex&x27;在openresty中显示为永久密钥
使用redis命令设置过期时间在我使用lua脚本在openresty中执行此操作时显示为一个永久键。 lua脚本如下所示:lua使用redis命令和';setex&x27;在openresty中显示为永久密钥,redis,lua,openresty,Redis,Lua,Openresty,使用redis命令设置过期时间在我使用lua脚本在openresty中执行此操作时显示为一个永久键。 lua脚本如下所示: local function ip_frequency(ip,red) local limit_num=50 local key = "limit:frequency:"..ip local resp, err = red:get(key) if resp==nil then local ok,err
local function ip_frequency(ip,red)
local limit_num=50
local key = "limit:frequency:"..ip
local resp, err = red:get(key)
if resp==nil then
local ok,err=red:setex(key,2,1)
if not ok then
return false;
end
end
if type(resp) == "string" then
if tonumber(resp) > limit_num then
return false
end
end
ok, err = red:incr(key)
if not ok then
return false
end
return true
end
当openresty程序运行一段时间后,redis中会出现一些永久键。从这个函数可以看出,我没有为永久时间设置键,但它只是发生了。为什么,请帮我回答这个问题。谢谢大家!软件版本如下:
- openresty:1.17.8.2
- redis:6.0+
- centos:8.0+
redis:get
,暂停,redis:setex
]或[redis:get
,暂停,redis:incr
]。在暂停期间可能会发生很多事情,如果只有1ms,则会发生事件,例如清除“死”键
这就是您的代码可能发生的情况:
localresp,err=red:get(key)
- 您得到的有效键值小于
limit\u num
ok,err=red:incr(键)
- Redis检查密钥是否有效,并在到达ttl时将其删除
- Redis检查并没有这样的键,所以创建value=0的键并增加键值
red:setex(键,2,tonumber(res)+1)
istead ofred:incr(键)