Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't persist cache on reload #4854

Merged
merged 8 commits into from
Nov 8, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions frontend/src/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@
expiration: number;
};


Check failure on line 7 in frontend/src/utils/cache.ts

View workflow job for this annotation

GitHub Actions / Lint frontend

Delete `⏎`
class Cache {
private prefix = "app_cache_";

private defaultTTL = 5 * 60 * 1000; // 5 minutes
private cacheMemory = {};

Check failure on line 12 in frontend/src/utils/cache.ts

View workflow job for this annotation

GitHub Actions / Lint frontend

Expected blank line between class members

/**
* Generate a unique key with prefix for local storage
* @param key The key to be stored in local storage
* Generate a unique key with prefix
* @param key The key to be stored in memory
* @returns The unique key with prefix
*/
private getKey(key: CacheKey): string {
return `${this.prefix}${key}`;
}

/**
* Retrieve the cached data from local storage
* @param key The key to be retrieved from local storage
* @returns The data stored in local storage
* Retrieve the cached data from memory
* @param key The key to be retrieved from memory
* @returns The data stored in memory
*/
public get<T>(key: CacheKey): T | null {
const cachedEntry = localStorage.getItem(this.getKey(key));
const cachedEntry = this.cacheMemory[this.getKey(key)];
if (cachedEntry) {
const { data, expiration } = JSON.parse(cachedEntry) as CacheEntry<T>;
if (Date.now() < expiration) return data;
Expand All @@ -35,34 +37,34 @@
}

/**
* Store the data in local storage with expiration
* @param key The key to be stored in local storage
* @param data The data to be stored in local storage
* Store the data in memory with expiration
* @param key The key to be stored in memory
* @param data The data to be stored in memory
* @param ttl The time to live for the data in milliseconds
* @returns void
*/
public set<T>(key: CacheKey, data: T, ttl = this.defaultTTL): void {
const expiration = Date.now() + ttl;
const entry: CacheEntry<T> = { data, expiration };
localStorage.setItem(this.getKey(key), JSON.stringify(entry));
this.cacheMemory[this.getKey(key)] = JSON.stringify(entry);
}

/**
* Remove the data from local storage
* @param key The key to be removed from local storage
* Remove the data from memory
* @param key The key to be removed from memory
* @returns void
*/
public delete(key: CacheKey): void {
localStorage.removeItem(this.getKey(key));
delete this.cacheMemory[this.getKey(key)];
}

/**
* Clear all data with the app prefix from local storage
* Clear all data with the app prefix from memory
* @returns void
*/
public clearAll(): void {
Object.keys(localStorage).forEach((key) => {
if (key.startsWith(this.prefix)) localStorage.removeItem(key);
Object.keys(this.cacheMemory).forEach((key) => {
if (key.startsWith(this.prefix)) delete this.cacheMemory[key];
});
}
}
Expand Down
Loading