A purely synchronous lightweight cache module with constant-time set
, get
and del
methods that works like a dictionary map.
Cache supports ttl for expired value removal. Expired keyValue pairs are only pruned on get
or del
. No tasks are scheduled in the node event loop.
Built-in support for null
value and integer
keys.
If you want stale key-removal scheduler, multi-key support, or event handling then look at node-cache.
If you do not need nor want to "pay" for those features then liteCache is for you.
LiteCache has no external dependencies. Feel free to comment and contribute!
npm install litecache
Or just require the lite_cache.js
file to get the superclass
var LiteCache = require( "litecache" );
var cache = new LiteCache();
cache.set( key, val, [ ttl ] )
Sets a key
to value
. Optionally provide ttl
(in seconds).
obj = { aNumber: 42 };
cache.set( "aKey", obj, 10);
cache.set( 123, 123);
cache.set('null', null);
cache.get( key )
Gets the stored value from cache.
Returns undefined
if not found or expired. If expired, the keyValue will be removed from the cache before returning.
cache.del( key )
Delete a key. Returns the value for the given key if exist.
cache.keys()
Returns an array of all existing keys.
cache.getStats()
Returns the statistics.
cache.getStats();
/*
{
keys: 0, // key count
hits: 0, // hit count
misses: 0, // miss count
}
*/
cache.flush()
Flush all data. Reset internal counters.