export interface CacheEntry<T> {
value: T;
expiresAt: number;
}
export class TTLCache<K, V> {
private readonly store = new Map<K, CacheEntry<V>>();
constructor(private readonly ttlMs: number) {}
get(key: K): V | undefined {
const entry = this.store.get(key);
if (!entry) {
return undefined;
}
if (Date.now() > entry.expiresAt) {
this.store.delete(key);
return undefined;
}
return entry.value;
}
set(key: K, value: V): void {
this.store.set(key, {
value,
expiresAt: Date.now() + this.ttlMs,
});
}
clear(key?: K): void {
if (typeof key === 'undefined') {
this.store.clear();
} else {
this.store.delete(key);
}
}
}