delete client side caching every 20 min
This commit is contained in:
parent
961fd08a54
commit
b6d81dd97e
@ -1,26 +1,49 @@
|
|||||||
import { writable } from 'svelte/store';
|
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) => {
|
export const setCache = (key, data, name) => {
|
||||||
|
const timestamp = Date.now();
|
||||||
clientSideCache.update(cache => {
|
clientSideCache.update(cache => {
|
||||||
return {
|
return {
|
||||||
...cache,
|
...cache,
|
||||||
[key]: {
|
[key]: {
|
||||||
...cache[key],
|
...cache[key],
|
||||||
[name]: data
|
[name]: { data, timestamp }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Function to get cache data for a specific key
|
||||||
export const getCache = (key, name) => {
|
export const getCache = (key, name) => {
|
||||||
let cacheData;
|
let cacheData;
|
||||||
clientSideCache.subscribe(cache => {
|
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;
|
return cacheData;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Function to clear the entire cache
|
||||||
|
export const clearCache = () => {
|
||||||
|
clientSideCache.set({});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
export const showCookieConsent = writable(<boolean>(false));
|
export const showCookieConsent = writable(<boolean>(false));
|
||||||
|
|
||||||
@ -42,7 +65,6 @@ export const isCrosshairMoveActive = writable(<boolean>(true));
|
|||||||
|
|
||||||
export const twitchStatus = writable(<boolean>(false));
|
export const twitchStatus = writable(<boolean>(false));
|
||||||
|
|
||||||
export const clientSideCache = writable({});
|
|
||||||
export const screenWidth = writable(<Number> (0));
|
export const screenWidth = writable(<Number> (0));
|
||||||
|
|
||||||
export const globalForm = writable(<Array<any>> []);
|
export const globalForm = writable(<Array<any>> []);
|
||||||
|
|||||||
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
import { beforeNavigate, afterNavigate } from '$app/navigation';
|
import { beforeNavigate, afterNavigate } from '$app/navigation';
|
||||||
import { onMount } from 'svelte';
|
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 { Button } from "$lib/components/shadcn/button/index.ts";
|
||||||
import * as Card from "$lib/components/shadcn/card/index.ts";
|
import * as Card from "$lib/components/shadcn/card/index.ts";
|
||||||
@ -187,6 +187,14 @@ onMount(async () => {
|
|||||||
if($showCookieConsent === true) {
|
if($showCookieConsent === true) {
|
||||||
Cookie = (await import("$lib/components/Cookie.svelte")).default;
|
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);
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user