From 5b379a70bee04ffaa9d5f5651ae21589f05328c1 Mon Sep 17 00:00:00 2001 From: Nulo Date: Thu, 7 Dec 2023 21:05:41 -0300 Subject: [PATCH] frontend: cachear respuestas gzip --- frontend/src/lib/dump.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/frontend/src/lib/dump.ts b/frontend/src/lib/dump.ts index 69caeae..539a545 100644 --- a/frontend/src/lib/dump.ts +++ b/frontend/src/lib/dump.ts @@ -26,9 +26,22 @@ async function fetchGzipped(url: string): Promise { const resD = new Response(decompressedStream); return resD; } +let cachedGzippedJson = new Map(); + async function loadGzippedJson(url: string): Promise { + const cachedEntry = cachedGzippedJson.get(url); + if (cachedEntry) { + if (+cachedEntry.date + 10 * 60 * 1000 > +new Date()) { + return cachedEntry.data; + } else { + cachedGzippedJson.delete(url); + } + } + const res = await fetchGzipped(url); - return await res.json(); + const json = await res.json(); + cachedGzippedJson.set(url, { date: new Date(), data: json }); + return json; } const endpoint = "http://localhost:8081";