An LRU cache of weak references
npm install weak-lru-cache


npm i weak-lru-cache
`
And import or require it to access the constructor:
`
import { WeakLRUCache } from 'weak-lru-cache';
let myCache = new WeakLRUCache();
myValue.setValue('key', { greeting: 'hello world' });
myValue.getValue('key') -> return the object above as long as it is still cached
`
Or in Deno, import directly from the weakcache deno.land package:
`
import { WeakLRUCache } from 'https://deno.land/x/weakcache/index.js';
...
`
Basic Usage
The WeakLRUCache class extends the native Map class and also includes the following methods, which are the primary intended interactions with the cache:
$3
Gets the value referenced by the given key and returns it. If the value is no longer cached, will return undefined.
$3
Sets or inserts the value into the cache, with the given key. This will create a new cache entry to reference your provided value. This also returns the cache entry.
The key can be any JS value.
If the value is an object, it will be stored in the cache, until it expires (as determined by the LRFU expiration policy), and is garbage collected. If you provide a primitive value (string, number, boolean, null, etc.), this can not be weakly referenced, so the value will still be stored in the LRFU cache, but once it expires, it will immediately be removed, rather than waiting for GC.
The expirationPriority is an optional directive indicating how quickly the cache entry should expire. A higher value will indicate the entry should expire sooner. This can be used to help limit the amount of memory used by cache if you are loading large objects. Using the default cache size, and entries with varied expirationPriority values, the expiration cache will typically hold a sum of about 100,000 of the expirationPriority values. This means that if you wanted to have a cache to stay around or under 100MB, you could provide the size, divided by 1000 (or using >> 10 is much faster), as the expirationPriority:
`
myCache.setValue('key', bigObject, sizeOfObject >> 10);
`
The expirationPriority can also be set to -1 that the value should be pinned in memory and never expire, until the entry has been changed (with a positive expirationPriority).
WeakLRUCache(options) Constructor
The WeakLRUCache constructor supports an optional options object parameter that allows specific configuration and cache tuning. The following properties (all optional) can be defined on the options object:
$3
This indicates the number of entries to allocate in the cache. This defaults to 32,768, and the maximum allowed value is 16,777,216.
$3
By default there is a single shared expiration cache, that is a single instance of LRFUExpirer. However, you can define your own expiration cache or create separate instances of LRFUExpirer. Generally using a (the default) single instance is preferable since it naturally gives higher priority/recency to more heavily used caches, and allows lesser used caches to expire more of their entries.
You can also set this to false to indicate that no LRU/LRFU cache should be used and the cache should rely entirely on weak-references.
$3
This flag can be set to true to save key and cache information so that it can defer the finalization registration. This tends to be slightly faster for caches that are more heavily dominated by lots of entry replacements (multiple setValue calls to same entries before they expire). However, for (what is generally considered more typical) usage more dominated by setting values and getting them until they expire, the default setting will probably be faster.
Using Cache Entries
The WeakLRUCache class extends the native Map class, and consequently, all the standard Map methods are available. As a Map, all the values are cache entries, which are typically WeakRef objects that hold a reference to the cached value, along with retention information. This means that the get(key) differs from getValue(key)` in that it returns the cache entry, which references the value, rather than the value itself.