From 8fa0cf775f55fdd8377035ef6e04e2bccb726132 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 29 Apr 2024 11:16:40 +0100 Subject: [PATCH] Fix LDB Cache, so when data is missing, nothing is cached Signed-off-by: Fred Boniface --- src/controllers/ldb.controllers.js | 12 +++++++++--- src/controllers/ref.controllers.js | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/controllers/ldb.controllers.js b/src/controllers/ldb.controllers.js index 83f92f9..5349bdd 100644 --- a/src/controllers/ldb.controllers.js +++ b/src/controllers/ldb.controllers.js @@ -9,7 +9,7 @@ async function getTrain(req, res, next) { err.status = 401; throw err; } - setCache(res, "private", 180) + setCache(res, "private", 240) let type = req.params.searchType; let id = req.params.id; try { @@ -45,8 +45,14 @@ async function getStation(req, res, next) { err.status = 401; return next(err); } - setCache(res, "public", 120) - res.json(await ldb.get(id, true)); + const data = await ldb.get(id, true); + // Only cache if data is present + if (data.data) { + setCache(res, "public", 120); + } else { + setCache(res, "no-store", 120); + } + res.json(data); } else { setCache(res, "public", 240) res.json(await ldb.get(id, false)); diff --git a/src/controllers/ref.controllers.js b/src/controllers/ref.controllers.js index 32e8275..98d9e0d 100644 --- a/src/controllers/ref.controllers.js +++ b/src/controllers/ref.controllers.js @@ -18,6 +18,7 @@ async function getReasonCode(req, res, next) { } catch (err) { console.error("ERROR", err.message); err.status = 500; + setCache(res, "no-store", 5) next(err); } }