Small cache abstraction that auto GCs unref'ed objects after a max size has been reached.
npm install refcache
const Refcache = require('refcache')
const cache = new Refcache({
maxSize: 1000, // set the max cache of unreffed objects
open (key, opts) {
// return the thing you wanna cache
},
close (thing) {
// close the thing you opened
}
})
const checkout = cache.checkout(Buffer.from('some-key'), {
...someOptions
})
// use checkout.value ...
// then let the cache know you are done with it
checkout.checkin()
Make a new cache instance. Options include:
{
maxSize, // how many unreffed objects to cache
open(key, opts), // make a new instance to cache
close(instance) // close a cached instance
}
Checkout a value from the cache. If not present it is auto opened. Every checkout you do is reference counted, and only cached values with no references are closed after the max size is reached.
If you do not want to ref count this particular checkout pass { weak: true }
to the options. All options are forwarded to open.
Check if the cache has a specific key loaded.
Call this when you are done with the value. You may only call this once.
Whether or not this entry has been closed.
The value returned from open.
Force remove this value from the cache.
Bumps this value in the internal LRU.
MIT