interface ICache<K, V> {
get(key: K): V | undefined;
set(key: K, value: V): void;
}
class Cache<K, V> implements ICache<K, V> {
private cache: Map<K, { value: V; expiration: number }> = new Map();
constructor(private ttl: number) {}
get(key: K): V | undefined {
const entry = this.cache.get(key);
if (entry && Date.now() < entry.expiration) {
return entry.value;
}
this.cache.delete(key);
return undefined;
}
set(key: K, value: V): void {
const expiration = Date.now() + this.ttl;
this.cache.set(key, { value, expiration });
}
}