LruCache
Defined in: packages/data/src/cache/lru-cache.ts:36
A fixed-capacity set that evicts the least recently used item to make room.
add never throws and never blocks: it answers with whatever it had to
displace, so a caller that needs to know what fell out can see it without
tracking the size itself. has deliberately does not count as a use —
only add and touch do — so a membership test cannot reshuffle the
eviction order behind the caller’s back.
Time complexity: O(1) for add, has, touch and delete. The inherited
set algebra is O(n·m), as on EnhancedSet, and returns an unbounded
EnhancedSet rather than another cache — a capacity-bounded union would
silently drop elements it was asked to combine.
Items are compared by SameValueZero, native Map semantics. An arbitrary
equality cannot be hashed, and a cache exists to be fast; use EnhancedSet
when the equality matters more than the speed.
Type Parameters
Section titled “Type Parameters”T extends DefinedValue
The type of items. Anything except undefined.
Implements
Section titled “Implements”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new LruCache<
T>(iterable,options):LruCache<T>
Defined in: packages/data/src/cache/lru-cache.ts:47
Parameters
Section titled “Parameters”iterable
Section titled “iterable”Iterable<T, any, any> | undefined
options
Section titled “options”Returns
Section titled “Returns”LruCache<T>
Properties
Section titled “Properties”capacity
Section titled “capacity”
readonlycapacity:number
Defined in: packages/data/src/cache/lru-cache.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-cache.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<T>
Defined in: packages/data/src/cache/lru-cache.ts:57
Most recently used first.
Returns
Section titled “Returns”Iterator<T>
Implementation of
Section titled “Implementation of”add(
item): readonlyT[]
Defined in: packages/data/src/cache/lru-cache.ts:76
O(1). Counts as a use, so re-adding a held item makes it the most recently used rather than doing nothing.
Parameters
Section titled “Parameters”T
Returns
Section titled “Returns”readonly T[]
The item evicted to make room, in a one-element array, or an empty array if nothing was evicted.
Implementation of
Section titled “Implementation of”addAll()
Section titled “addAll()”addAll(
items): readonlyT[]
Defined in: packages/data/src/cache/lru-cache.ts:87
O(n) in the number of items. Every eviction along the way is reported, in the order it happened — adding more items than the capacity evicts the earlier ones of the same batch.
Parameters
Section titled “Parameters”Iterable<T>
Returns
Section titled “Returns”readonly T[]
Implementation of
Section titled “Implementation of”clear()
Section titled “clear()”clear():
void
Defined in: packages/data/src/cache/lru-cache.ts:65
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-cache.ts:61
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(
item):boolean
Defined in: packages/data/src/cache/lru-cache.ts:102
O(1).
Parameters
Section titled “Parameters”T
Returns
Section titled “Returns”boolean
Implementation of
Section titled “Implementation of”difference()
Section titled “difference()”difference(
other):ISet<T>
Defined in: packages/data/src/cache/lru-cache.ts:142
O(n·m). Returns an unbounded set: see the note on this class.
Parameters
Section titled “Parameters”Iterable<T>
Returns
Section titled “Returns”ISet<T>
Implementation of
Section titled “Implementation of”has(
item):boolean
Defined in: packages/data/src/cache/lru-cache.ts:107
O(1). Does not count as a use — see touch.
Parameters
Section titled “Parameters”T
Returns
Section titled “Returns”boolean
Implementation of
Section titled “Implementation of”intersection()
Section titled “intersection()”intersection(
other):ISet<T>
Defined in: packages/data/src/cache/lru-cache.ts:137
O(n·m). Returns an unbounded set: see the note on this class.
Parameters
Section titled “Parameters”Iterable<T>
Returns
Section titled “Returns”ISet<T>
Implementation of
Section titled “Implementation of”isDisjointFrom()
Section titled “isDisjointFrom()”isDisjointFrom(
other):boolean
Defined in: packages/data/src/cache/lru-cache.ts:168
O(m).
Parameters
Section titled “Parameters”Iterable<T>
Returns
Section titled “Returns”boolean
Implementation of
Section titled “Implementation of”isSubsetOf()
Section titled “isSubsetOf()”isSubsetOf(
other):boolean
Defined in: packages/data/src/cache/lru-cache.ts:152
O(n·m).
Parameters
Section titled “Parameters”Iterable<T>
Returns
Section titled “Returns”boolean
Implementation of
Section titled “Implementation of”isSupersetOf()
Section titled “isSupersetOf()”isSupersetOf(
other):boolean
Defined in: packages/data/src/cache/lru-cache.ts:157
O(m).
Parameters
Section titled “Parameters”Iterable<T>
Returns
Section titled “Returns”boolean
Implementation of
Section titled “Implementation of”lru():
T|undefined
Defined in: packages/data/src/cache/lru-cache.ts:127
The least recently used item — the one add would evict next. O(1). Does
not count as a use.
Returns
Section titled “Returns”T | undefined
The item, or undefined when the cache is empty.
symmetricDifference()
Section titled “symmetricDifference()”symmetricDifference(
other):ISet<T>
Defined in: packages/data/src/cache/lru-cache.ts:147
O(n·m). Returns an unbounded set: see the note on this class.
Parameters
Section titled “Parameters”Iterable<T>
Returns
Section titled “Returns”ISet<T>
Implementation of
Section titled “Implementation of”IReplacingSet.symmetricDifference
touch()
Section titled “touch()”touch(
item):boolean
Defined in: packages/data/src/cache/lru-cache.ts:117
Marks a held item as the most recently used. O(1).
Parameters
Section titled “Parameters”T
Returns
Section titled “Returns”boolean
true if the item was held, false if it was not (in which case
nothing changed — use add to insert it).
union()
Section titled “union()”union(
other):ISet<T>
Defined in: packages/data/src/cache/lru-cache.ts:132
O(n·m). Returns an unbounded set: see the note on this class.
Parameters
Section titled “Parameters”Iterable<T>
Returns
Section titled “Returns”ISet<T>