A Browser cache object that deletes the least-recently-used items. and use localforage as storage.
The code is forked and adapted from node-lru-cache
warning version
0.1.*
has an async api and use mozilla localforage to store cache objects if you want a sync version that use localstorage use version0.0.x
var LRU = require("lru-cache")
, options = {ns: 'cache-1', max: 500, maxAge: 1000 * 60 * 60 }
, cache = LRU(options)
, otherCache = LRU(50) // sets just the max size
// load cache from storage
cache.load().then(function({
// cache is loaded
})
// ...
// get an oject from the cache
cache.get('key-1').then(function(v) {
// do something with value
})
// ...
// save an oject into the cache
cache.set('key-2', { foo: 'bar' }).then(function() {
// cache is saved
})
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, an item size is the length of its key and the stringify length of its value. if no size is specified the cache will keep adding items until the max size of the localstorage is reached.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.
update the "recently used"-ness of the key, and returns a promise of the key value.
Type: String
Type: Promise
save value
and update the "recently used"-ness of the key
Type: String
Type: Object
Type: Promise
Returns a promise of the key value without updating the "recently used"-ness of the key.
Type: String
Type: Promise
Deletes a key out of the cache.
Type: String
Clear the cache entirely, throwing away all values.
Type: Promise
a Promise that resolve when the change have been saved to storage.
Check if a key is in the cache, without updating the recent-ness or deleting it for being stale.
Type: String
Type: Boolean
Return an array of the keys in the cache.
Type: Array