LruCache<K, V>
Least recently used cache. Inspired by https://github.com/Yomguithereal/mnemonist/blob/master/lru-cache.js
Type parameters
Type parameter | Value |
---|---|
K | any |
V | any |
Constructors
new LruCache()
new LruCache<
K
,V
>(capacity
):LruCache
<K
,V
>
Parameters
Parameter | Type | Default value |
---|---|---|
capacity | number | 100 |
Returns
LruCache
<K
, V
>
Source
Properties
Property | Modifier | Type |
---|---|---|
K | public | any [] |
V | public | any [] |
backward | private | Uint16Array |
capacity | private | number |
forward | private | Uint16Array |
head | private | number |
items | private | object |
onItemEvicted | public | null | Function |
size | private | number |
tail | private | number |
Methods
get()
get(
key
):undefined
|V
Gets the value attached to the given key, and makes it the most recently used item.
Parameters
Parameter | Type | Description |
---|---|---|
key | any | Key. |
Returns
undefined
| V
Source
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
Parameter | Type | Description |
---|---|---|
key | K | Key. |
createFromKey | (key ) => V | Function to create a new item. |
Returns
V
Source
has()
has(
key
):boolean
Checks whether the key exists in the cache.
Parameters
Parameter | Type | Description |
---|---|---|
key | any | Key. |
Returns
boolean
Source
set()
set(
key
,value
):void
Sets the value for the given key in the cache.
Parameters
Parameter | Type | Description |
---|---|---|
key | any | Key. |
value | any | Value. |
Returns
void
Source
splayOnTop()
splayOnTop(
pointer
):LruCache
<K
,V
>
Splays a value on top.
Parameters
Parameter | Type | Description |
---|---|---|
pointer | number | Pointer of the value to splay on top. |
Returns
LruCache
<K
, V
>