cache-money-flow

LRU cache with locking

npm install cache-money-flow
1 downloads in the last week
1 downloads in the last month

Cache Flow Money

cache-flow-money is a simple LRU cache for node.

It supports both synchronous and asynchrous population for the cache, and it supports locking items so they cannot be evicted.

Installation

npm install cache-flow-money

Usage

var LRUCache = require('cache-flow-money');

var maxItems = 10;
var options = {
  populate: function(key) { return key + key; },
  async: false
};
var cache = new LRUCache(maxItems, options);

cache.get('foo') === 'foo-foo' // populated automagically

You can also use async populate methods.

var options = {
  populate: function(key, cb) {
    cb(key + "-" + key);
  },
  async: true
};

var cache = new LRUCache(maxItems, options);

cache.get('foo', function(v) {
  v === 'foo-foo'; // populated
});

Performance

Evicting items (which happens on inserts when cache is full), is O(n), with n being the number of items in the cache.

Everything else (retrieving, locking, unlocking, setting), is O(1).

Locking

You can lock items that are in use to prevent them from being evicted.

var cache = new LRUCache(5);

cache.set('foo', 'bar');
cache.set('baz', 'bam');
cache.lock('foo');
for (var i = 0; i < 10; ++i) {
  cache.set('foo' + i, i);
}

cache.get('foo'); // 'bar'
cache.get('baz'); // undefined

License

Cache-flow-money is under an MIT Style (jslint) license.

See the LICENSE file for details.

npm loves you