From e8b6cd1588ff144668771598261df64f76162355 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Sun, 19 Nov 2023 19:33:12 -0500 Subject: [PATCH] clear expired and invalid cache --- playlet-web/src/lib/Api/InvidiousApi.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/playlet-web/src/lib/Api/InvidiousApi.ts b/playlet-web/src/lib/Api/InvidiousApi.ts index 797624d8..d3bddbe7 100644 --- a/playlet-web/src/lib/Api/InvidiousApi.ts +++ b/playlet-web/src/lib/Api/InvidiousApi.ts @@ -284,12 +284,14 @@ export class InvidiousApi { try { const cacheData = JSON.parse(cache); if (cacheData.timestamp + cacheSeconds * 1000 < Date.now()) { + this.deleteCache(url); return null; } console.log(`Cache hit for ${url}`); return cacheData.data; } catch (error) { console.error(error); + this.deleteCache(url); return null; } } @@ -305,6 +307,11 @@ export class InvidiousApi { localStorage.setItem(cacheKey, JSON.stringify(cacheData)); } + private deleteCache(url: string) { + const cacheKey = this.getCacheKey(url); + localStorage.removeItem(cacheKey); + } + private getCacheKey(url: string) { return `v1:${url}`; }