Caching library supporting timeouts, events and external data sources

Caching library for JavaScript and Node.js

Caching library with support for timeouts, events and external data sources.

var cache = require('js-cache');
cache.set('lorem', 'ipsum', 60000);

var myCache = new cache();
myCache.set('lorem', 'dolor', 60000);
<script src="bundle/cache.js"></script> 
    cache.set('lorem', 'ipsum', 60000);

It is possible to call the cache object directly:

cache.set('lorem', 'ipsum');

or use it as a constructor to create a separate storage:

var c2 = new cache();
c2.set('lorem', 'dolor');

Cache data or update and existing record.

key Unique key identifying the cache
value Cached value
ttl Time to live in milliseconds (optional)

Get cached value. Returns cached value (or undefined) if no callback was provided. Always returns undefined if callback argument is present.

key Key identifying the cache
callback Return value in callback if record exists in memory or on external resource (optional)

Delete cached data. Returns true if the record existed, false if not.

key Key identifying the cache

Clear all cached data. Returns number of cleared records.

Returns number of cached records.

Returns internal object with cached records.

It is possible to forward api requests to external handlers. They can be used for logging, storing in persistent database etc. The cache library will function as a temporary, in-memory layer.

var c = new cache({
    set: setHandler,
    get: getHandler,
    del: delHandler,
    clear: clearHandler

All handlers are optional.

setHandler is called with key, value, ttl on cache.set().

getHandler is called with key, callback on cache.get() when a callback is provided and the key is not present in the cache. The getHandler is required to execute callback.

delHandler is called with key on cache.del(). It is not called when a cached record times out.

clearHandler is called without arguments on cache.clear().

The cache library uses the Backbone event framework. Please refer to their documentation for a detailed overview.

Emitted when setting a value to a new record.

cache.on('set', function(key, value, ttl){});
cache.on('set:lorem', function(value, ttl){});
cache.set('lorem', 'ipsum', 123);

Emitted when updating the value or ttl of a known record.

cache.set('lorem', 'ipsum');
cache.on('update', function(key, value, ttl){});
cache.on('update:lorem', function(value, ttl){});
cache.set('lorem', 'ipsum', 123);

Emitted when deleting a record or when a cached record times out.

cache.set('lorem', 'ipsum');
cache.on('del', function(key){});
cache.on('del:lorem', function(){});

Emitted when the cache gets cleared.

cache.on('clear', function(size){});

The library is published to NPM and can be installed with the following command:

$ npm install js-cache

The scripts have to be bundled in order to use them in a browser. One of the tools you can use is browserify.

$ sudo npm install browserify -g
$ make browserify

This will build the script in bundle/cache.js

Navigate to this module's repository and make sure you have the development modules installed:

$ npm install

Run the tests:

$ npm test