Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
lua使用redis命令和';setex&x27;在openresty中显示为永久密钥_Redis_Lua_Openresty - Fatal编程技术网

lua使用redis命令和';setex&x27;在openresty中显示为永久密钥

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

使用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=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+

Openresty连接到redis数据库并使用其功能。在lua或其他语言中使用redis函数不是原子的。对于redis服务器,它意味着:[
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 of
red:incr(键)