fix: json encode psl rules to cache between requests

This commit is contained in:
Iain Learmonth 2025-05-04 15:31:24 +01:00
parent ecf5e28086
commit 99f7a5d635

View file

@ -1,3 +1,4 @@
local cjson = require "cjson"
local http = require "resty.http" local http = require "resty.http"
local _M = {} local _M = {}
@ -26,10 +27,11 @@ end
local function load_psl() local function load_psl()
local cache = ngx.shared.jasima_cache local cache = ngx.shared.jasima_cache
local cached = cache:get("psl") local cached = cache:get("psl")
if cached then return cached end if cached then return cjson.decode(cached) end
ngx.log(ngx.DEBUG, "Cache miss on public suffix list")
local rules, err = fetch_psl() local rules, err = fetch_psl()
if rules then if rules then
cache:set("psl", rules, 86400 * 7) -- 1 week cache:set("psl", cjson.encode(rules), 86400 * 7) -- 1 week
return rules return rules
else else
ngx.log(ngx.ERR, err or "Unknown error loading PSL") ngx.log(ngx.ERR, err or "Unknown error loading PSL")