From 3967615abb4b00d38f28c7ae4f4e78bbb30fbee9 Mon Sep 17 00:00:00 2001 From: Nordine Bittich Date: Sat, 20 Jan 2024 20:15:40 +0100 Subject: [PATCH] better caching --- index.mjs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/index.mjs b/index.mjs index 83ee7e4..f736122 100644 --- a/index.mjs +++ b/index.mjs @@ -81,8 +81,9 @@ const aw = (cb) => { }; // CACHE -const CACHE = apicache.middleware; -app.use(CACHE("5 minutes")); +const cache = apicache.middleware; +const onlyStatus200 = (_req, res) => res.statusCode === 200; +const cacheSuccesses = cache("5 minutes", onlyStatus200); // ROUTES router.get( @@ -191,7 +192,8 @@ router.post( ); router.get( "/gallery", - aw(async (req, res, _next) => { + cacheSuccesses, + aw(async (req, res) => { const page = parseInt(req.query.page) || undefined; const pageSize = parseInt(req.query.pageSize) || undefined; const ip = @@ -208,7 +210,8 @@ router.get( // ROUTES router.get( "/blog/posts/:postId/:slug", - aw(async (req, res, _next) => { + cacheSuccesses, + aw(async (req, res) => { const id = req.params.postId; const slug = req.params.slug; const ip = @@ -224,7 +227,8 @@ router.get( ); router.get( "/blog", - aw(async (req, res, _next) => { + cacheSuccesses, + aw(async (req, res) => { const page = parseInt(req.query.page) || undefined; const pageSize = parseInt(req.query.pageSize) || undefined; const ip = @@ -241,7 +245,8 @@ router.get( router.get( "/", - aw(async (req, res, _next) => { + cacheSuccesses, + aw(async (req, res) => { const ip = req.headers["x-forwarded-for"]?.split(",")[0] || req.socket.remoteAddress ||