Files
Perplexica/src/lib/utils/cache.ts
eligrinfeld fde5b5e318 Add project files:
- Add database initialization scripts
- Add configuration files
- Add documentation
- Add public assets
- Add source code structure
- Update README
2025-01-04 17:22:46 -07:00

36 lines
646 B
TypeScript

interface CacheItem<T> {
data: T;
timestamp: number;
}
export class Cache<T> {
private store = new Map<string, CacheItem<T>>();
private ttl: number;
constructor(ttlMinutes: number = 60) {
this.ttl = ttlMinutes * 60 * 1000;
}
set(key: string, value: T): void {
this.store.set(key, {
data: value,
timestamp: Date.now()
});
}
get(key: string): T | null {
const item = this.store.get(key);
if (!item) return null;
if (Date.now() - item.timestamp > this.ttl) {
this.store.delete(key);
return null;
}
return item.data;
}
clear(): void {
this.store.clear();
}
}