A simple in-memory cache for node.js
npm install memory-cache --save
var cache = require('memory-cache');
// now just use the cache
cache.put('foo', 'bar');
console.log(cache.get('foo'));
// that wasn't too interesting, here's the good part
cache.put('houdini', 'disappear', 100, function(key, value) {
console.log(key + ' did ' + value);
}); // Time in ms
console.log('Houdini will now ' + cache.get('houdini'));
setTimeout(function() {
console.log('Houdini is ' + cache.get('houdini'));
}, 200);
// create new cache instance
var newCache = new cache.Cache();
newCache.put('foo', 'newbaz');
setTimeout(function() {
console.log('foo in old cache is ' + cache.get('foo'));
console.log('foo in new cache is ' + newCache.get('foo'));
}, 200);
which should print
bar
Houdini will now disappear
houdini did disappear
Houdini is null
foo in old cache is baz
foo in new cache is newbaz
setTimeout
)function(key, value) {}
)null
== size()
unless a setTimeout
removal went wrongexport
into the cacheimport
will remain in the cacheskipDuplicates
is true
options
:
skipDuplicates
: If true
, any duplicate keys will be ignored when importing them. Defaults to false
.require('cache')
would return the default instance of Cacherequire('cache').Cache
is the actual class