Skip to content

Documentation / @ripl/utilities / LRUCache

Class: LRUCache<TKey, TValue> ​

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

A fixed-capacity Map that evicts the least recently used entry once it is full.

Recency is tracked by Map insertion order: a read re-inserts its key so the oldest key is always the map's first. Iteration (keys, values, entries, forEach, spread) therefore yields entries least recently used first, and neither iterating nor LRUCache.has affects recency — only LRUCache.get and LRUCache.set do.

Extends ​

  • Map<TKey, TValue>

Type Parameters ​

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

Constructors ​

Constructor ​

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

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

Parameters ​

ParameterType
limitnumber

Returns ​

LRUCache<TKey, TValue>

Overrides ​

Map<TKey, TValue>.constructor

Properties ​

PropertyModifierTypeInherited fromDefined in
[toStringTag]readonlystringLRUCache.[toStringTag]node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts:135
sizereadonlynumberLRUCache.sizenode_modules/typescript/lib/lib.es2015.collection.d.ts:46
[species]readonlyMapConstructorMap.[species]node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts:317

Accessors ​

maxSize ​

Get Signature ​

get maxSize(): number

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

Maximum number of entries retained before a write evicts the least recently used entry.

Returns ​

number

Methods ​

[iterator]() ​

[iterator](): MapIterator<[TKey, TValue]>

Defined in: node_modules/typescript/lib/lib.es2015.iterable.d.ts:141

Returns an iterable of entries in the map.

Returns ​

MapIterator<[TKey, TValue]>

Inherited from ​

Map.[iterator]


clear() ​

clear(): void

Defined in: node_modules/typescript/lib/lib.es2015.collection.d.ts:21

Removes all elements from the Map.

Returns ​

void

Inherited from ​

Map.clear


delete() ​

delete(key): boolean

Defined in: node_modules/typescript/lib/lib.es2015.collection.d.ts:25

Parameters ​

ParameterType
keyTKey

Returns ​

boolean

true if an element in the Map existed and has been removed, or false if the element does not exist.

Inherited from ​

Map.delete


entries() ​

entries(): MapIterator<[TKey, TValue]>

Defined in: node_modules/typescript/lib/lib.es2015.iterable.d.ts:146

Returns an iterable of key, value pairs for every entry in the map.

Returns ​

MapIterator<[TKey, TValue]>

Inherited from ​

Map.entries


forEach() ​

forEach(callbackfn, thisArg?): void

Defined in: node_modules/typescript/lib/lib.es2015.collection.d.ts:29

Executes a provided function once per each key/value pair in the Map, in insertion order.

Parameters ​

ParameterType
callbackfn(value, key, map) => void
thisArg?any

Returns ​

void

Inherited from ​

Map.forEach


get() ​

get(key): TValue | undefined

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

Reads an entry, marking it as the most recently used, or undefined when absent.

Parameters ​

ParameterType
keyTKey

Returns ​

TValue | undefined

Overrides ​

Map.get


getOrInsert() ​

getOrInsert(key, defaultValue): TValue

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

Reads an entry as the most recently used, inserting defaultValue first when absent.

Parameters ​

ParameterType
keyTKey
defaultValueTValue

Returns ​

TValue

Overrides ​

Map.getOrInsert


getOrInsertComputed() ​

getOrInsertComputed(key, callback): TValue

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

Reads an entry as the most recently used, inserting the result of callback first when absent.

Parameters ​

ParameterType
keyTKey
callback(key) => TValue

Returns ​

TValue

Overrides ​

Map.getOrInsertComputed


has() ​

has(key): boolean

Defined in: node_modules/typescript/lib/lib.es2015.collection.d.ts:38

Parameters ​

ParameterType
keyTKey

Returns ​

boolean

boolean indicating whether an element with the specified key exists or not.

Inherited from ​

Map.has


keys() ​

keys(): MapIterator<TKey>

Defined in: node_modules/typescript/lib/lib.es2015.iterable.d.ts:151

Returns an iterable of keys in the map

Returns ​

MapIterator<TKey>

Inherited from ​

Map.keys


set() ​

set(key, value): this

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

Writes an entry as the most recently used, evicting the least recently used entry when at capacity.

Parameters ​

ParameterType
keyTKey
valueTValue

Returns ​

this

Overrides ​

Map.set


values() ​

values(): MapIterator<TValue>

Defined in: node_modules/typescript/lib/lib.es2015.iterable.d.ts:156

Returns an iterable of values in the map

Returns ​

MapIterator<TValue>

Inherited from ​

Map.values


groupBy() ​

static groupBy<K, T>(items, keySelector): Map<K, T[]>

Defined in: node_modules/typescript/lib/lib.es2024.collection.d.ts:23

Groups members of an iterable according to the return value of the passed callback.

Type Parameters ​

Type Parameter
K
T

Parameters ​

ParameterTypeDescription
itemsIterable<T>An iterable.
keySelector(item, index) => KA callback which will be invoked for each item in items.

Returns ​

Map<K, T[]>

Inherited from ​

Map.groupBy