A simple in-memory cache for Node.js.
This codebase is cloned from Tinycache. Thanks to Andy Burke.
npm install cacherdb --save
var CacherDB = require( 'cacherdb' );
var cache = new CacherDB({
letDuplicate: false, //let to insert same values
autoSave: true, //save automatically to json file
file: 'data.json' //file name or location + file name
});
// 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', 'disapear', 100 ); // Time in ms
console.log( 'Houdini will now ' + cache.get( 'houdini' ) );
setTimeout( function() {
console.log( 'Houdini is ' + cache.get( 'houdini' ) );
}, 200 );
// don't want to allocate separate caches?
// there's also a default shared cache:
var sharedCache = CacherDB.shared;
sharedCache.put( 'foo', 'bar' );
// or you could grab it in a one-liner
var theSharedCache = require( 'cacherdb' ).shared;
theSharedCache.get( 'bloop' );
Stores a value to the cache. If time (in ms) is specified, the value will be automatically removed (via setTimeout).
Retreives a value for a given key, or if no key is passed, will return the internal cache object.
Deletes a key, returns a boolean indicating if the key existed and was deleted.
Deletes all keys.
The current number of entries in the cache.
The approximate size in bytes of the cache (including all objects stored and cache overhead)
This is a rough estimate, using the js-sizeof library.
The number of cache hits.
The number of cache misses.
Whole data from the library (includes cache memory and storage statistics).
- Fork the project.
- Make your feature addition or bug fix.
- Send me a pull request.