From b6d81dd97eef78e775f2bfb0e6dc8c2c1035c286 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Wed, 14 Aug 2024 00:45:41 +0200 Subject: [PATCH] delete client side caching every 20 min --- src/lib/store.ts | 30 ++++++++++++++++++++++++++---- src/routes/+layout.svelte | 10 +++++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/lib/store.ts b/src/lib/store.ts index 6ffa8c5f..f8889b6a 100644 --- a/src/lib/store.ts +++ b/src/lib/store.ts @@ -1,26 +1,49 @@ import { writable } from 'svelte/store'; -// Function to set cache data for a specific tickerID + +// Cache expiration time in milliseconds (5 minutes) +const CACHE_EXPIRATION_TIME = 5 * 60 * 1000; + +export const clientSideCache = writable({}); + +// Function to set cache data for a specific key export const setCache = (key, data, name) => { + const timestamp = Date.now(); clientSideCache.update(cache => { return { ...cache, [key]: { ...cache[key], - [name]: data + [name]: { data, timestamp } } }; }); }; +// Function to get cache data for a specific key export const getCache = (key, name) => { let cacheData; clientSideCache.subscribe(cache => { - cacheData = cache[key]?.[name]; + const entry = cache[key]?.[name]; + if (entry) { + const { data, timestamp } = entry; + // Check if the cache has expired + if (Date.now() - timestamp < CACHE_EXPIRATION_TIME) { + cacheData = data; + } else { + // Cache has expired, so return undefined to fetch new data + cacheData = undefined; + } + } }); return cacheData; }; +// Function to clear the entire cache +export const clearCache = () => { + clientSideCache.set({}); +}; + export const showCookieConsent = writable((false)); @@ -42,7 +65,6 @@ export const isCrosshairMoveActive = writable((true)); export const twitchStatus = writable((false)); -export const clientSideCache = writable({}); export const screenWidth = writable( (0)); export const globalForm = writable(> []); diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index f1aa4835..690b4306 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -19,7 +19,7 @@ import { beforeNavigate, afterNavigate } from '$app/navigation'; import { onMount } from 'svelte'; - import { showCookieConsent, newAvatar, userRegion, screenWidth, stockTicker, etfTicker, loginData, numberOfUnreadNotification, cachedPosts, currentPagePosition, clientSideCache, twitchStatus } from '$lib/store'; + import { clearCache, showCookieConsent, newAvatar, userRegion, screenWidth, stockTicker, etfTicker, loginData, numberOfUnreadNotification, cachedPosts, currentPagePosition, clientSideCache, twitchStatus } from '$lib/store'; import { Button } from "$lib/components/shadcn/button/index.ts"; import * as Card from "$lib/components/shadcn/card/index.ts"; @@ -187,6 +187,14 @@ onMount(async () => { if($showCookieConsent === true) { Cookie = (await import("$lib/components/Cookie.svelte")).default; } + + //Clear all the cache every 20 min + const interval = setInterval(() => { + clearCache(); + }, 20 * 60 * 1000); + + return () => clearInterval(interval); + })