Skip to main content

Cache

Runtime: any — Node.js ≥ 24 or a modern browser

An in-memory TTL cache keyed by string. createTtlCache takes a freshness window and a load function per call: a value younger than ttlMs is served without calling load, and concurrent calls for one key share a single load.

Set staleOnError for upstreams that fail now and then. When a refresh throws, the last good value is returned (however old) and kept, so the next get tries again. With no good value yet, or without staleOnError, the error is rethrown. Entries are replaced, never evicted by age, so bound the key space yourself or call delete / clear.

ttlMs takes 0 to never cache (every get calls load) and Infinity for entries that never expire. A NaN or negative ttlMs throws a RangeError when the cache is created.

Example​

import { createTtlCache } from '@rtorcato/js-common/cache'

const quotes = createTtlCache<Quote>({ ttlMs: 60_000, staleOnError: true })

// Served from memory for a minute; if the refresh then fails, the old quote is returned.
const quote = await quotes.get('AAPL', () => fetchQuote('AAPL'))

Import​

import { TtlCache, TtlCacheOptions, createTtlCache } from '@rtorcato/js-common/cache'

Exports​

NameSummary
TtlCache—
TtlCacheOptions—
createTtlCacheIn-memory TTL cache with optional stale-on-error.

See also​

  • functions — once, debounce, throttle
  • promises — timeout and retry helpers for the load function