Files
profilarr/src/lib/server/cache/cache.ts
Sam Chau 119131bab6 feat: add expandable table component for displaying Radarr library items with detailed views
feat: implement caching mechanism for library data with TTL
feat: enhance Radarr client with methods to fetch movies and quality profiles
feat: update library page to support profile changing and improved UI elements
fix: update navigation icons and improve layout for better user experience
fix: correct cache handling and error management in library loading
2025-12-26 07:41:04 +10:30

68 lines
1.1 KiB
TypeScript

/**
* Simple in-memory cache with TTL
*/
interface CacheEntry<T> {
data: T;
expiresAt: number;
}
class Cache {
private store = new Map<string, CacheEntry<unknown>>();
/**
* Get a cached value
*/
get<T>(key: string): T | undefined {
const entry = this.store.get(key) as CacheEntry<T> | undefined;
if (!entry) return undefined;
if (Date.now() > entry.expiresAt) {
this.store.delete(key);
return undefined;
}
return entry.data;
}
/**
* Set a cached value with TTL in seconds
*/
set<T>(key: string, data: T, ttlSeconds: number): void {
this.store.set(key, {
data,
expiresAt: Date.now() + ttlSeconds * 1000
});
}
/**
* Delete a cached value
*/
delete(key: string): boolean {
return this.store.delete(key);
}
/**
* Delete all cached values matching a prefix
*/
deleteByPrefix(prefix: string): number {
let count = 0;
for (const key of this.store.keys()) {
if (key.startsWith(prefix)) {
this.store.delete(key);
count++;
}
}
return count;
}
/**
* Clear all cached values
*/
clear(): void {
this.store.clear();
}
}
export const cache = new Cache();