lru

A simple O(1) LRU cache

lru

A simple LRU cache supporting O(1) set, get and eviction of old keys

$ npm install lru
var LRU = require('lru');
 
var cache = new LRU(2),
    evicted
 
cache.on('evict',function(data) { evicted = data });
 
cache.set('foo', 'bar');
cache.get('foo'); //=> bar 
 
cache.set('foo2', 'bar2');
cache.get('foo2'); //=> bar2 
 
cache.set('foo3', 'bar3'); // => evicted = { key: 'foo', value: 'bar' } 
cache.get('foo3');         // => 'bar3' 
cache.remove('foo2')       // => {key: 'foo2', value: 'bar2'} 
cache.remove('foo4')       // => undefined 
cache.length               // => 1 

Create a new LRU cache that stores length elements before evicting the least recently used.

Returns: the newly created LRU cache

The number of keys currently in the cache.

Set the value of the key and mark the key as most recently used.

Returns: value

Set the value of the key and mark the key as most recently used.

Query the value of the key and mark the key as most recently used.

Returns: value of key if found; undefined otherwise.

Remove the key/value pair from the cache.

Returns: an associative array containing {key: <key>, value: <value>}

Respond to events. Currently only the evict event is implemented. When a key is evicted, the callback is executed with an associative array containing the evicted key: {key: key, value: value}.

A big thanks to Dusty Leary who finished the library.

MIT