cache.ts•728 B
interface CacheEntry<T> {
data: T;
timestamp: number;
}
export class SimpleCache {
private cache: Map<string, CacheEntry<any>> = new Map();
private ttl: number;
constructor(ttlMs: number = 60000) {
this.ttl = ttlMs;
}
get<T>(key: string): T | null {
const entry = this.cache.get(key);
if (!entry) return null;
if (Date.now() - entry.timestamp > this.ttl) {
this.cache.delete(key);
return null;
}
return entry.data as T;
}
set<T>(key: string, data: T): void {
this.cache.set(key, {
data,
timestamp: Date.now()
});
}
clear(): void {
this.cache.clear();
}
}
export const apiCache = new SimpleCache(60000); // Default 1 min cache