lite-lru
v0.0.1
Published
A simple LRU cache module that is optimized for frequent serialization, only supports increasing integer keys and uses binary search to locate the entries
Downloads
6
Readme
lite-lru
A simple LRU cache module that is optimized for frequent serialization, only supports number keys and uses binary search to locate the entries
Installation
$ npm install --save lite-lru
Usage
const LiteLRU = require('lite-lru');
const size = 5;
const lru = new LiteLRU(size);
const key = lru.add('any value');
// Later you can get back the value:
lru.get(key); // > 'any value'
lru.set(key, 'some other value');
lru.delete(key);
lru.get(key); // > undefined
Note that get
, set
and delete
methods use binary search
There's a toJSON
method defined which basically returns the internal
array. You can pass that array to LiteLRU constructor:
new LiteLRU(50, JSON.parse(JSON.stringify(new LiteLRU(50))))
You can set callback for when a value is added, so that you can save the cache somewhere:
lru.onAdd = () => {
localStorage.cache = JSON.stringify(lru);
}
License
MIT © Olindata BV