fix cache logic

This commit is contained in:
Thibault bui Koechlin 2020-11-10 21:06:22 +01:00
parent 2bd6c0c0f7
commit 92c3b8fc88

View file

@ -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