A simple in-memory cache. put(), get() and del()
npm install memory-cacheA simple in-memory cache for node.js
npm install memory-cache --save
``javascript
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
* Simply stores a value
* If time isn't passed in, it is stored forever
* Will actually remove the value in the specified time in ms (via setTimeout)function(key, value) {}
* timeoutCallback is optional function fired after entry has expired with key and value passed ()
* Returns the cached value
* Retrieves a value for a given key
* If value isn't cached, returns null
* Deletes a key, returns a boolean specifying whether or not the key was deleted
* Deletes all keys
* Returns the current number of entries in the cache
* Returns the number of entries taking up space in the cache
* Will usually == size() unless a setTimeout removal went wrong
* Turns on or off debugging
* Returns the number of cache hits (only monitored in debug mode)
* Returns the number of cache misses (only monitored in debug mode)
* Returns all the cache keys
* Returns a JSON string representing all the cache data
* Any timeoutCallbacks will be ignored
* Merges all the data from a previous call to export into the cacheimport
* Any existing entries before an will remain in the cacheskipDuplicates
* Any duplicate keys will be overwritten, unless is trueoptions
* Any entries that would have expired since being exported will expire upon being imported (but their callbacks will not be invoked)
* Available :skipDuplicates
* : If true, any duplicate keys will be ignored when importing them. Defaults to false.
* Returns the new size of the cache
* Cache constructor
* note that require('cache') would return the default instance of Cacherequire('cache').Cache` is the actual class
* while
* Fork the project.
* Make your feature addition or bug fix.
* Send me a pull request.