Skip to content

Documentation / @andrew_l/toolkit / LruCache

Class: LruCache<TKey, TValue>

A simple implementation of a Least Recently Used (LRU) cache. This cache stores key-value pairs and ensures that the most recently accessed items are kept in the cache, while the least recently used items are evicted when the cache reaches its capacity.

The cache is implemented as a doubly linked list where the head represents the most recently accessed item, and the tail represents the least recently accessed item. When the cache is full, the least recently used item (tail) is removed to make space for new items.

Type Parameters

TKey = any

TValue = any

Constructors

new LruCache()

new LruCache<TKey, TValue>(capacity): LruCache<TKey, TValue>

Parameters

capacity: number

Returns

LruCache<TKey, TValue>

Properties

size

size: number

Methods

[iterator]()

[iterator](): object

Returns

object

[iterator]()
Returns

{ [Symbol.iterator](): ...; next(): { done: boolean; value: undefined; } | { done: boolean; value: (TKey | TValue | undefined)[]; }; }

next()
Returns

object | object


clear()

clear(): void

Method used to clear the structure.

Returns

void


delete()

delete(key): void

Parameters

key: TKey

Returns

void


entries()

entries(): object

Method used to create an iterator over the cache's entries from most recently used to least recently used.

Returns

object

[iterator]()
Returns

{ [Symbol.iterator](): ...; next(): { done: boolean; value: undefined; } | { done: boolean; value: (TKey | TValue | undefined)[]; }; }

next()
Returns

object | object


get()

get(key): undefined | TValue

Parameters

key: TKey

Returns

undefined | TValue


has()

has(key): boolean

Parameters

key: TKey

Returns

boolean


keys()

keys(): IterableIterator<TKey, any, any>

Method used to create an iterator over the cache's keys from most recently used to least recently used.

Returns

IterableIterator<TKey, any, any>


peek()

peek(key): undefined | TValue

Method used to get the value attached to the given key. Does not modify the ordering of the underlying linked list.

Parameters

key: TKey

Returns

undefined | TValue


set()

set(key, value): this

Parameters

key: TKey

value: TValue

Returns

this


splayOnTop()

splayOnTop(pointer): this

Method used to splay a value on top.

Parameters

pointer: number

Pointer of the value to splay on top.

Returns

this


values()

values(): IterableIterator<TValue, any, any>

Method used to create an iterator over the cache's values from most recently used to least recently used.

Returns

IterableIterator<TValue, any, any>