interface ExpiringMap { get(key: K): V | undefined set(key: K, value: V): void } export function expiringMap(duration: number): ExpiringMap { const map = new Map() return { get(key: K) { const result = map.get(key) if (!result) return undefined if (result.expiration <= new Date()) { map.delete(key) return undefined } return result.value }, set(key: K, value: V) { const expiration = new Date() expiration.setMilliseconds(expiration.getMilliseconds() + duration) map.set(key, { value, expiration, }) }, } }