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"
|
return nil, "Configuration is bad, cannot run properly"
|
||||||
end
|
end
|
||||||
local resp = runtime.cache:get(ip)
|
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 link = runtime.conf["API_URL"] .. "/v1/decisions?ip=" .. ip
|
||||||
local resp = {}
|
local resp = {}
|
||||||
if link:find("https://") == 1 then
|
if link:find("https://") == 1 then
|
||||||
|
@ -78,6 +84,7 @@ function csmod.allowIp(ip)
|
||||||
sink = ltn12.sink.table(resp)
|
sink = ltn12.sink.table(resp)
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
resp = table.concat(resp)
|
resp = table.concat(resp)
|
||||||
if code~=200 then
|
if code~=200 then
|
||||||
print(code)
|
print(code)
|
||||||
|
@ -86,18 +93,12 @@ function csmod.allowIp(ip)
|
||||||
end
|
end
|
||||||
if resp == "null" then -- no result from API, no decision for this IP
|
if resp == "null" then -- no result from API, no decision for this IP
|
||||||
-- set ip in cache and DON'T block it
|
-- 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
|
return true, nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- set ip in cache and block it
|
-- 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
|
return false, nil
|
||||||
|
|
||||||
end
|
|
||||||
runtime.logger:debug("'" .. ip .. "' is in cache")
|
|
||||||
return resp == true, nil
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue