LruMap
Defined in: packages/data/src/cache/lru-map.ts:36
A fixed-capacity map that evicts the least recently used entry to make room.
The Map-shaped counterpart of LruCache, over the same engine. Prefer
this one: a cache almost always has something to say about each key, and a
LruMap<K, undefined> is the degenerate case LruCache covers. Reach for
LruCache only when the membership itself is the whole answer — a
seen-recently set, a dedupe window.
get counts as a use; has and peek do not, so an inspection cannot
reshuffle the eviction order behind the caller’s back. set never throws and
never blocks: it answers with whatever entry it had to displace. Overwriting
a key already held never evicts.
Time complexity: O(1) for get, set, has, peek and delete. Iteration
is most-recently-used first, and does not count as a use.
Keys are compared by SameValueZero, native Map semantics.
Type Parameters
Section titled “Type Parameters”K
The key type.
V extends DefinedValue
The value type.
Implements
Section titled “Implements”IReplacingMap<K,V>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new LruMap<
K,V>(iterable,options):LruMap<K,V>
Defined in: packages/data/src/cache/lru-map.ts:47
Parameters
Section titled “Parameters”iterable
Section titled “iterable”Iterable<readonly [K, V], any, any> | undefined
options
Section titled “options”Returns
Section titled “Returns”LruMap<K, V>
Properties
Section titled “Properties”capacity
Section titled “capacity”
readonlycapacity:number
Defined in: packages/data/src/cache/lru-map.ts:39
The maximum number of elements the collection can hold.
Note: Infinity is not a legal value here — an unbounded type omits this
mixin.
Implementation of
Section titled “Implementation of”overflowPolicy
Section titled “overflowPolicy”
readonlyoverflowPolicy:ReplacementPolicy="lru"
Defined in: packages/data/src/cache/lru-map.ts:45
Always "lru". A different replacement policy is a different class, the
same way BoundedQueue/LossyQueue are, never a constructor flag.
Implementation of
Section titled “Implementation of”Methods
Section titled “Methods”[iterator]()
Section titled “[iterator]()”[iterator]():
Iterator<readonly [K,V]>
Defined in: packages/data/src/cache/lru-map.ts:62
Most recently used first. Does not count as a use.
Returns
Section titled “Returns”Iterator<readonly [K, V]>
Implementation of
Section titled “Implementation of”clear()
Section titled “clear()”clear():
void
Defined in: packages/data/src/cache/lru-map.ts:70
Removes all elements from the collection.
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”count()
Section titled “count()”count():
number
Defined in: packages/data/src/cache/lru-map.ts:66
The number of elements in the collection.
A method, not a getter: its async counterpart needs a round trip to the backing store, and a member must have the same shape in both.
Returns
Section titled “Returns”number
Implementation of
Section titled “Implementation of”delete()
Section titled “delete()”delete(
key):boolean
Defined in: packages/data/src/cache/lru-map.ts:102
O(1) — no tombstone, no compaction.
Parameters
Section titled “Parameters”K
Returns
Section titled “Returns”boolean
Implementation of
Section titled “Implementation of”entries()
Section titled “entries()”entries():
IterableIterator<readonly [K,V]>
Defined in: packages/data/src/cache/lru-map.ts:117
Most recently used first.
Returns
Section titled “Returns”IterableIterator<readonly [K, V]>
Implementation of
Section titled “Implementation of”get(
key):V|undefined
Defined in: packages/data/src/cache/lru-map.ts:75
O(1). Counts as a use.
Parameters
Section titled “Parameters”K
Returns
Section titled “Returns”V | undefined
Implementation of
Section titled “Implementation of”has(
key):boolean
Defined in: packages/data/src/cache/lru-map.ts:97
O(1). Does not count as a use.
Parameters
Section titled “Parameters”K
Returns
Section titled “Returns”boolean
Implementation of
Section titled “Implementation of”keys()
Section titled “keys()”keys():
IterableIterator<K>
Defined in: packages/data/src/cache/lru-map.ts:107
Most recently used first.
Returns
Section titled “Returns”IterableIterator<K>
Implementation of
Section titled “Implementation of”lru(): readonly [
K,V] |undefined
Defined in: packages/data/src/cache/lru-map.ts:127
The least recently used entry — the one set would evict next. O(1). Does
not count as a use.
Returns
Section titled “Returns”readonly [K, V] | undefined
The entry, or undefined when the map is empty.
peek()
Section titled “peek()”peek(
key):V|undefined
Defined in: packages/data/src/cache/lru-map.ts:80
O(1). Does not count as a use, unlike get.
Parameters
Section titled “Parameters”K
Returns
Section titled “Returns”V | undefined
set(
key,value): readonly readonly [K,V][]
Defined in: packages/data/src/cache/lru-map.ts:90
O(1). Counts as a use.
Parameters
Section titled “Parameters”K
V
Returns
Section titled “Returns”readonly readonly [K, V][]
The entry evicted to make room, in a one-element array, or an empty array if nothing was evicted. Overwriting a held key never evicts.
Implementation of
Section titled “Implementation of”values()
Section titled “values()”values():
IterableIterator<V>
Defined in: packages/data/src/cache/lru-map.ts:112
Most recently used first.
Returns
Section titled “Returns”IterableIterator<V>