delete client side caching every 20 min

This commit is contained in:
MuslemRahimi 2024-08-14 00:45:41 +02:00
parent 961fd08a54
commit b6d81dd97e
2 changed files with 35 additions and 5 deletions

View File

@ -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(<boolean>(false));
@ -42,7 +65,6 @@ export const isCrosshairMoveActive = writable(<boolean>(true));
export const twitchStatus = writable(<boolean>(false));
export const clientSideCache = writable({});
export const screenWidth = writable(<Number> (0));
export const globalForm = writable(<Array<any>> []);

View File

@ -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);
})