Skip to content

Function: createPatternCache()

API / @xmachines/play-router / index / createPatternCache

function createPatternCache<V>(maxSize?): PatternCache<V>;

Defined in: packages/play-pattern/src/cache.ts:85

Builds a cache that forgets its least recently used entries.

The algorithm

Two maps, and no list. recent takes every write. When a write fills recent, it becomes old and a new recent opens, which drops the previous old whole. A read of a key of old moves the entry into recent, and THAT is what makes the eviction an LRU one rather than a FIFO one: an entry that every lookup reads moves forward, and an entry that nothing reads falls out with the map that holds it.

The cache therefore holds between maxSize and 2 * maxSize entries, and it does no bookkeeping for a read that hits recent.

Type Parameters

Type Parameter
V

Parameters

ParameterTypeDefault valueDescription
maxSizenumber500The number of entries that recent holds. It is 1 at the least, and the default is 500. A value that is not a finite number reads as the default, so a cache with no eviction at all cannot happen by accident.

Returns

PatternCache<V>

A cache. It is not shared: each call builds one.

Example

const cache = createPatternCache<ParsedPattern>(50);
parsePattern("/profile/:userId", { patternCache: cache });