cache-money-flow

0.0.1 • Public • Published

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.

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i cache-money-flow

      Weekly Downloads

      1

      Version

      0.0.1

      License

      none

      Last publish

      Collaborators

      • regality