function collectionCache
collectionCache<T>(
fetchAll: () => Promise<T[]>,
ttlMs: number,
now: () => number,
): CollectionCache<T>

Create an in-memory collection cache with TTL. Loads all items via fetchAll on first access or after invalidation/expiry, then serves from memory until the TTL expires or invalidate() is called. Takes the clock as an argument: pass nowMs, or a fake clock in a test.

Uses a generation counter to prevent a race condition where a concurrent fetchAll() that started before an invalidation could overwrite the cache with stale data.

Type Parameters

Parameters

fetchAll: () => Promise<T[]>
ttlMs: number
now: () => number

Return Type

Usage

import { collectionCache } from "docs/utilities.ts";