A simple in-memory cache for node.js
npm install memory-cache --save
varcache=require('memory-cache');// now just use the cachecache.put('foo','bar');console.log(cache.get('foo'));// that wasn't too interesting, here's the good partcache.put('houdini','disappear',100,function(key,value){console.log(key+' did '+value);});// Time in msconsole.log('Houdini will now '+cache.get('houdini'));setTimeout(function(){console.log('Houdini is '+cache.get('houdini'));},200);which should print
bar
Houdini will now disappear
houdini did disappear
Houdini is null
- 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) - timeoutCallback is optional function fired after entry has expired with key and value passed (
function(key, value) {}) - 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 asetTimeoutremoval 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
- Fork the project.
- Make your feature addition or bug fix.
- Send me a pull request.