intermediate
TTL cache mechanics
Complete the lazy-expiry path of a TTL cache — the write that stamps an expiry and the read that honours it.
interface Entry {
value: string
expiresAt: number
}
export class TTLCache {
#store = new ___<string, Entry>()
#ttlMs: number
constructor(ttlMs: number) {
this.#ttlMs = ttlMs
}
set(key: string, value: string): void {
this.#store.set(key, {
value,
expiresAt: Date.___() + this.#ttlMs,
})
}
get(key: string): string | undefined {
const entry = this.#store.get(key)
if (entry === undefined) return undefined
if (Date.now() > entry._________) {
this.#store.______(key)
return undefined
}
return entry.value
}
}0/4 filled tab moves · Ctrl+↵ submits · Ctrl+Shift+↵ runsstarter code