Skip to content

leastRecentlyUsedCache<K, V>(options): Cache<K, V>

Defined in: packages/cache/src/least-recently-used-cache.ts:57

Create a Cache that evicts the entry used least recently.

Choose it when a key read recently is likely to be read again soon and the traffic has no long one-pass scans: a scan of keys never read again pushes every older entry out. get and a set of a key already held count as uses; peek and has do not.

get, peek, has, delete, and set run in O(1) average time, plus O(1) per entry set evicts. Iteration is O(n) over every slot allocated since the last clear.

K

V

CacheOptions<K, V>

Cache<K, V>

With INVALID_CAPACITY when capacity is not a positive safe integer (or, with weigh, not a positive finite number), MISSING_CLOCK when timeToLive comes without clock, or INVALID_TIME_TO_LIVE when timeToLive is not a positive finite number.

const cache = leastRecentlyUsedCache<string, number>({ capacity: 2 })
cache.set('a', 1)
cache.set('b', 2)
cache.get('a')
cache.set('c', 3) // [{ key: 'b', value: 2 }]