Skip to content

Documentation / @ripl/utilities / createLRUCache

Function: createLRUCache() ​

createLRUCache<TKey, TValue>(limit): LRUCache<TKey, TValue>

Defined in: packages/utilities/src/cache.ts:99

Creates a bounded LRUCache, evicting the single least recently used entry when a write exceeds the limit.

Preferred over an unbounded memo wherever the key space is open-ended (paint strings, resolved geometry), since evicting one entry keeps every other caller's entry warm — wiping the whole cache at a threshold makes the steady state a permanent miss.

Type Parameters ​

Type ParameterDescription
TKeyType of the cache key.
TValueType of the cached value.

Parameters ​

ParameterTypeDescription
limitnumberMaximum number of entries to retain. Values below 1 are clamped to 1.

Returns ​

LRUCache<TKey, TValue>

The cache instance.

Example ​

ts
const cache = createLRUCache<string, number>(2);

cache.set('a', 1);
cache.set('b', 2);
cache.get('a');
cache.set('c', 3); // evicts 'b', the least recently used