interface Entry { value: V expires: number } export class TtlStore | null> { readonly #entries = new Map>() readonly #ttl: number readonly #miss_ttl: number readonly #max: number constructor(ttl: number, max: number, miss_ttl: number = ttl) { this.#ttl = ttl this.#miss_ttl = miss_ttl this.#max = max } get(key: K): V | undefined { const entry = this.#entries.get(key) if (!entry) return undefined if (Date.now() > entry.expires) { this.#entries.delete(key) return undefined } return entry.value } set(key: K, value: V): void { const ttl = value === null ? this.#miss_ttl : this.#ttl this.#entries.delete(key) this.#entries.set(key, { value, expires: Date.now() + ttl }) while (this.#entries.size > this.#max) { const oldest = this.#entries.keys().next() if (oldest.done) break this.#entries.delete(oldest.value) } } del(key: K): void { this.#entries.delete(key) } clear(): void { this.#entries.clear() } }