Skip to content

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.

T extends DefinedValue

The type of items. Anything except undefined.

new LruCache<T>(iterable, options): LruCache<T>

Defined in: packages/data/src/cache/lru-cache.ts:47

Iterable<T, any, any> | undefined

LruCacheOptions

LruCache<T>

readonly capacity: 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.

IReplacingSet.capacity


readonly overflowPolicy: 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.

IReplacingSet.overflowPolicy

[iterator](): Iterator<T>

Defined in: packages/data/src/cache/lru-cache.ts:57

Most recently used first.

Iterator<T>

IReplacingSet.[iterator]


add(item): readonly T[]

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.

T

readonly T[]

The item evicted to make room, in a one-element array, or an empty array if nothing was evicted.

IReplacingSet.add


addAll(items): readonly T[]

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.

Iterable<T>

readonly T[]

IReplacingSet.addAll


clear(): void

Defined in: packages/data/src/cache/lru-cache.ts:65

Removes all elements from the collection.

void

IReplacingSet.clear


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.

number

IReplacingSet.count


delete(item): boolean

Defined in: packages/data/src/cache/lru-cache.ts:102

O(1).

T

boolean

IReplacingSet.delete


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.

Iterable<T>

ISet<T>

IReplacingSet.difference


has(item): boolean

Defined in: packages/data/src/cache/lru-cache.ts:107

O(1). Does not count as a use — see touch.

T

boolean

IReplacingSet.has


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.

Iterable<T>

ISet<T>

IReplacingSet.intersection


isDisjointFrom(other): boolean

Defined in: packages/data/src/cache/lru-cache.ts:168

O(m).

Iterable<T>

boolean

IReplacingSet.isDisjointFrom


isSubsetOf(other): boolean

Defined in: packages/data/src/cache/lru-cache.ts:152

O(n·m).

Iterable<T>

boolean

IReplacingSet.isSubsetOf


isSupersetOf(other): boolean

Defined in: packages/data/src/cache/lru-cache.ts:157

O(m).

Iterable<T>

boolean

IReplacingSet.isSupersetOf


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.

T | undefined

The item, or undefined when the cache is empty.


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.

Iterable<T>

ISet<T>

IReplacingSet.symmetricDifference


touch(item): boolean

Defined in: packages/data/src/cache/lru-cache.ts:117

Marks a held item as the most recently used. O(1).

T

boolean

true if the item was held, false if it was not (in which case nothing changed — use add to insert it).


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.

Iterable<T>

ISet<T>

IReplacingSet.union