From 3df4159026470377334057fe874d1ad3febe49d0 Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Sun, 21 Mar 2021 12:13:51 +0100 Subject: [PATCH] Add customizable cache for web instance (#193) --- source/app/web/instance.mjs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/source/app/web/instance.mjs b/source/app/web/instance.mjs index b73830f6..96650674 100644 --- a/source/app/web/instance.mjs +++ b/source/app/web/instance.mjs @@ -70,7 +70,11 @@ } //Cache headers middleware middlewares.push((req, res, next) => { - res.header("Cache-Control", cached ? `public, max-age=${Math.round(cached/1000)}` : "no-store, no-cache") + const maxage = Math.round(Number(req.query.cache)) + if ((cached)||(maxage > 0)) + res.header("Cache-Control", `public, max-age=${Math.round((maxage > 0 ? maxage : cached)/1000)}`) + else + res.header("Cache-Control", "no-store, no-cache") next() }) @@ -195,8 +199,10 @@ convert:["jpeg", "png", "json"].includes(q["config.output"]) ? q["config.output"] : null, }, {Plugins, Templates}) //Cache - if ((!debug)&&(cached)) - cache.put(login, {rendered, mime}, cached) + if ((!debug)&&(cached)) { + const maxage = Math.round(Number(req.query.cache)) + cache.put(login, {rendered, mime}, maxage > 0 ? maxage : cached) + } //Send response res.header("Content-Type", mime) return res.send(rendered)