Files
telecom-platform/apps/web-dashboard/src/lib/cache.ts
T
nutcas3 16bf257b90 feat: Add client-side caching layer to web dashboard with configurable TTL and pattern-based invalidation
- Add CACHE_TTL environment variable with 30000ms default to API configuration
- Add DataCache class with Map-based storage, timestamp tracking, and TTL expiration
- Add cache methods for set, get, invalidate, invalidatePattern, clear, and size operations
- Add getCachedData helper function with fetcher callback and TTL override support
- Add apiFetch wrapper with optional cache parameter for GET
2026-04-25 05:55:07 +03:00

89 lines
1.7 KiB
TypeScript

interface CacheEntry<T> {
data: T;
timestamp: number;
ttl: number;
}
class DataCache {
private cache: Map<string, CacheEntry<any>>;
private defaultTTL: number;
constructor(defaultTTL: number = 60000) {
this.cache = new Map();
this.defaultTTL = defaultTTL;
}
set<T>(key: string, data: T, ttl?: number): void {
const entry: CacheEntry<T> = {
data,
timestamp: Date.now(),
ttl: ttl || this.defaultTTL,
};
this.cache.set(key, entry);
}
get<T>(key: string): T | null {
const entry = this.cache.get(key);
if (!entry) return null;
const now = Date.now();
if (now - entry.timestamp > entry.ttl) {
this.cache.delete(key);
return null;
}
return entry.data as T;
}
invalidate(key: string): void {
this.cache.delete(key);
}
invalidatePattern(pattern: string): void {
const regex = new RegExp(pattern);
for (const key of this.cache.keys()) {
if (regex.test(key)) {
this.cache.delete(key);
}
}
}
clear(): void {
this.cache.clear();
}
size(): number {
return this.cache.size;
}
}
export const cache = new DataCache();
export function getCachedData<T>(
key: string,
fetcher: () => Promise<T>,
ttl?: number
): Promise<T> {
const cached = cache.get<T>(key);
if (cached !== null) {
return Promise.resolve(cached);
}
return fetcher().then((data) => {
cache.set(key, data, ttl);
return data;
});
}
export function invalidateCache(key: string): void {
cache.invalidate(key);
}
export function invalidateCachePattern(pattern: string): void {
cache.invalidatePattern(pattern);
}
export function clearCache(): void {
cache.clear();
}