fix cache logic
This commit is contained in:
parent
2bd6c0c0f7
commit
92c3b8fc88
1 changed files with 26 additions and 25 deletions
|
@ -49,7 +49,13 @@ function csmod.allowIp(ip)
|
|||
return nil, "Configuration is bad, cannot run properly"
|
||||
end
|
||||
local resp = runtime.cache:get(ip)
|
||||
if resp == nil then -- not in cache
|
||||
|
||||
if resp ~= nil then -- we have it in cache
|
||||
runtime.logger:debug("'" .. ip .. "' is in cache")
|
||||
return resp, nil
|
||||
end
|
||||
|
||||
-- not in cache
|
||||
local link = runtime.conf["API_URL"] .. "/v1/decisions?ip=" .. ip
|
||||
local resp = {}
|
||||
if link:find("https://") == 1 then
|
||||
|
@ -78,6 +84,7 @@ function csmod.allowIp(ip)
|
|||
sink = ltn12.sink.table(resp)
|
||||
}
|
||||
end
|
||||
|
||||
resp = table.concat(resp)
|
||||
if code~=200 then
|
||||
print(code)
|
||||
|
@ -86,18 +93,12 @@ function csmod.allowIp(ip)
|
|||
end
|
||||
if resp == "null" then -- no result from API, no decision for this IP
|
||||
-- set ip in cache and DON'T block it
|
||||
runtime.cache:set(ip, false,runtime.conf["CACHE_EXPIRATION"])
|
||||
runtime.cache:set(ip, true,runtime.conf["CACHE_EXPIRATION"])
|
||||
return true, nil
|
||||
end
|
||||
|
||||
-- set ip in cache and block it
|
||||
runtime.cache:set(ip, true,runtime.conf["CACHE_EXPIRATION"])
|
||||
runtime.cache:set(ip, false,runtime.conf["CACHE_EXPIRATION"])
|
||||
return false, nil
|
||||
|
||||
end
|
||||
runtime.logger:debug("'" .. ip .. "' is in cache")
|
||||
return resp == true, nil
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue