Skip to main content

Class: LruCache<K, V>

dg.LruCache

Least recently used cache. Inspired by https://github.com/Yomguithereal/mnemonist/blob/master/lru-cache.js

Type parameters

NameType
Kany
Vany

Constructors

constructor

new LruCache<K, V>(capacity?)

Type parameters

NameType
Kany
Vany

Parameters

NameTypeDefault value
capacitynumber100

Defined in

src/utils.ts:472

Properties

K

K: any[]

Defined in

src/utils.ts:460

src/utils.ts:468


V

V: any[]

Defined in

src/utils.ts:460

src/utils.ts:467


backward

Private backward: Uint16Array

Defined in

src/utils.ts:466


capacity

Private capacity: number

Defined in

src/utils.ts:461


forward

Private forward: Uint16Array

Defined in

src/utils.ts:465


Private head: number

Defined in

src/utils.ts:470


items

Private items: Object

Defined in

src/utils.ts:463


onItemEvicted

onItemEvicted: null | Function

Defined in

src/utils.ts:462


size

Private size: number

Defined in

src/utils.ts:469


tail

Private tail: number

Defined in

src/utils.ts:464

Methods

get

get(key): undefined | V

Gets the value attached to the given key, and makes it the most recently used item.

Parameters

NameTypeDescription
keyanyKey.

Returns

undefined | V

Defined in

src/utils.ts:573


getOrCreate

getOrCreate(key, createFromKey): V

Returns the value with the specified key, if it already exists in the cache, or creates a new one by calling the provided function.

Parameters

NameTypeDescription
keyKKey.
createFromKey(key: K) => VFunction to create a new item.

Returns

V

Defined in

src/utils.ts:593


has

has(key): boolean

Checks whether the key exists in the cache.

Parameters

NameTypeDescription
keyanyKey.

Returns

boolean

Defined in

src/utils.ts:518


set

set(key, value): void

Sets the value for the given key in the cache.

Parameters

NameTypeDescription
keyanyKey.
valueanyValue.

Returns

void

Defined in

src/utils.ts:528


splayOnTop

splayOnTop(pointer): LruCache<K, V>

Splays a value on top.

Parameters

NameTypeDescription
pointernumberPointer of the value to splay on top.

Returns

LruCache<K, V>

Defined in

src/utils.ts:490