serialized lru cache
A cache object that deletes the least-recently-used items, with serialization support.
This is a fork of the wonderful @isaacs node-lru-cache with several improvements
- Serialization support
- Bug fixes
- Bower support
Install
Get it from npm
npm install serialized-lru-cache
var LRUCache = ;var cache = ;
or from bower
bower install serialized-lru-cache
Usage:
var LRU = options = max: 500 { return nlength * 2 + keylength } { n } maxAge: 1000 * 60 * 60 cache = otherCache = // sets just the max size cachecache // "value" cache // 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.
You can save the current cache and load its entries in another cache instantiated with other options
var LRU = fs = cache = otherCache = // sets just the max size cachefs;otherCache;
Options
max
The maximum size of the cache, checked by applying the length function to all values in the cache. Not setting this is kind of silly, since that's the whole purpose of this lib, but it defaults toInfinity
.maxAge
Maximum age in ms. Items are not pro-actively pruned out as they age, but if you try to get an item that is too old, it'll drop it and return undefined instead of giving it to you.length
Function that is used to calculate the length of stored items. If you're storing strings or buffers, then you probably want to do something likefunction(v, k){return v.length}
. The default isfunction(n){return 1}
, which is fine if you want to storen
like-sized things.dispose
Function that is called on items when they are dropped from the cache. This can be handy if you want to close file descriptors or do other cleanup tasks when items are no longer accessible. Called withkey, value
. It's called before actually removing the item from the internal cache, so if you want to immediately put it back in, you'll have to do that in anextTick
orsetTimeout
callback or it won't do anything.stale
By default, if you set amaxAge
, it'll only actually pull stale items out of the cache when youget(key)
. (That is, it's not pre-emptively doing asetTimeout
or anything.) If you setstale:true
, it'll return the stale value before deleting it. If you don't set this, then it'll returnundefined
when you try to get a stale entry, as if it had already been deleted.
API
-
set(key, value)
-
get(key) => value
Both of these will update the "recently used"-ness of the key. They do what you think.
-
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 recent-ness. (Ie, more recently 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.
-
toJSON()
Return an array of the cache entries ready for serialization.
-
load(cacheEntriesArray)
Loads another cache entries array, obtained with
sourceCache.toJSON()
, into the cache. The destination cache is reset before loading the new entries