A cache object that deletes the least-frequently-used items.
ISC License
A cache object that deletes the least-frequently-used items. A direct port of lru-cache but using this algorithm.
npm install node-lfu-cache --save
var LFU = require('node-lfu-cache');
var options = {
max: 500,
length: function (n, key) {
return n * 2 + key.length;
},
dispose: function (key, n) {
n.close();
},
maxAge: 1000 * 60 * 60
};
var cache = LFU(options);
var otherCache = LFU(50); // sets just the max size
cache.set('key', 'value');
cache.get('key'); // "value"
// non-string keys ARE fully supported
var someObject = {};
cache.set(someObject, 'a value');
cache.set('[object Object]', 'a different value');
assert.equal(cache.get(someObject), 'a value');
cache.reset(); // empty the cache
If you put more stuff in it, then items will fall out.
If you try to put an oversized thing in it, then it'll fall out right away.
max
The maximum size of the cache, checked by applying the lengthInfinity
.maxAge
Maximum age in ms. Items are not pro-actively pruned outlength
Function that is used to calculate the length of storedfunction(n, key){return n.length}
. The default isfunction(){return 1}
, which is fine if you want to store max
dispose
Function that is called on items when they are droppedkey, value
. It's called beforenextTick
or setTimeout
callback or it won't do anything.stale
By default, if you set a maxAge
, it'll only actually pullget(key)
. (That is, it'ssetTimeout
or anything.) If you setstale:true
, it'll return the stale value before deleting it. Ifundefined
when you try tonoDisposeOnSet
By default, if you set a dispose()
method, thenset()
operation overwrites an existingdispose()
will only be called when aset(key, value, maxAge)
get(key) => value
Both of these will update the "recently used"-ness of the key.
They do what you think. maxAge
is optional and overrides the
cache maxAge
option if provided.
If the key is not found, get()
will return undefined
.
The key and val can be any value.
peek(key)
Returns the key value (or undefined
if not found) without
updating the "recently used"-ness of the key.
(If you find yourself using this a lot, you might be using the wrong sort of data structure, but there are some use cases where it's handy.)
del(key)
Deletes a key out of the cache.
reset()
Clear the cache entirely, throwing away all values.
has(key)
Check if a key is in the cache, without updating the recent-ness or deleting it for being stale.
forEach(function(value,key,cache), [thisp])
Just like Array.prototype.forEach
. Iterates over all the keys
in the cache, in order of frequent-ness. (Ie, more frequently used
items are iterated over first.)
rforEach(function(value,key,cache), [thisp])
The same as cache.forEach(...)
but items are iterated over in
reverse order. (ie, least frequently used items are iterated over
first.)
keys()
Return an array of the keys in the cache.
values()
Return an array of the values in the cache.
length
Return total length of objects in cache taking into account
length
options function.
itemCount
Return total quantity of objects currently in cache. Note, that
stale
(see options) items are returned as part of this item
count.
dump()
Return an array of the cache entries ready for serialization and usage with 'destinationCache.load(arr)`.
load(cacheEntriesArray)
Loads another cache entries array, obtained with sourceCache.dump()
,
into the cache. The destination cache is reset before loading new entries
prune()
Manually iterates over the entire cache proactively pruning old entries
pop()
Removes and returns the least frequently used item, i.e. the one that would be deleted next.