Skip to content

Documentation / @andrew_l/toolkit / withCacheLRU

Function: withCacheLRU() ​

withCacheLRU<T>(__namedParameters, fn): WithCacheResult<T>

Wrap a function to cache results by arguments

But with LRU

Type Parameters ​

T ​

T extends AnyFunction

Parameters ​

__namedParameters ​

WithCacheLruOptions

fn ​

T

Returns ​

WithCacheResult<T>

Example ​

ts
const sum = withCacheLRU({ capacity: 100 }, (a, b) => {
    console.log('calc?');
    return a + b;
});

sum(1, 2); // calc?
sum(1, 2);

Replaces ​

map.delete(key); map.set(key, value) to bump recency plus map.delete(map.keys().next().value) to evict, around a memo — a bounded memo in one call. Same keying as withCache: objects by reference unless objectStrategy: 'json', rejections are not cached, and promise-returning functions must be declared async.

Detect ​

\.get\(\s*\w+\s*\)[\s\S]{0,120}?\.delete\(\s*\w+\s*\)\s*;?\s*[\w.]+\.set\(\s*\w+\s*,

Detect ​

[\w.]+\.delete\(\s*[\w.]+\.keys\(\)\.next\(\)\.value

Released under the MIT License.