class TtlCache

A map whose entries expire ttlMs after they are written, holding maxEntries at most.

Examples

Example 1

const playlists = new TtlCache<YouTubePlaylist>(6 * 3_600_000, 200); const hit = playlists.get(listId) ?? playlists.set(listId, await fetch());

Constructors

TtlCache(
ttlMs: number,
maxEntries: number
)

Type Parameters

Methods

clear(): void

Empties the cache, for the tests' reset between cases.

delete(
key: string,
value?: V
): void

Removes key, or only when value is still what it holds, so a late failure can't remove the entry that replaced its own.

get(key: string): V | undefined

Returns the value under key unless it is missing or expired.

set(
key: string,
value: V
): V

Stores value under key from now on, and returns it.

Usage

import { TtlCache } from "api/lib/ttl-cache.ts";