Simple LRU cache in JavaScript
The implementation is inspired by node-lru-cache by Isaac Schlueter. The
motivation of this project is to provide Object.create
fallback in order to
work on IE8.
With npm
$ npm install simple-lru
With Component
$ component install smagch/simple-lru
With Bower
$ bower install simple-lru
Or download tarball.
You can set any value with a string key.
var cache = new SimpleLRU(3);
cache.set('a', 'A');
cache.set('b', {name: 'smagch'});
cache.set('c', 1000);
var a = cache.get('a'); // a = 'A'
var b = cache.get('b'); // b = {name: 'smagch'}
var c = cache.get('c'); // c = 1000
It removes cache items automatically when total length get out of max.
var cache = new SimpleLRU(3);
cache.set('a', 'A');
cache.set('b', 'B');
cache.set('c', 'C');
cache.set('d', 'D');
var a = cache.get('a'); // a = undefined;
var b = cache.get('b'); // b = 'B'
var c = cache.get('c'); // c = 'C'
var d = cache.get('d'); // d = 'D'
var keys = cache.keys(); // keys = ['b', 'c', 'd']
Since it's using LRU cache algorithm, it removes the least recently used item.
var cache = new SimpleLRU(3);
cache.set('a', 'A');
cache.set('b', 'B');
cache.set('c', 'C');
// Calling `get` with 'a', 'a' is the most recently used item.
var a = cache.get('a'); // a = 'A'
var keys = cache.keys(); // keys = ['b', 'c', 'a']
cache.set('d', 'D');
a = cache.get('a'); // a = 'A'
var b = cache.get('b'); // b = undefined
var c = cache.get('c'); // c = 'C'
var d = cache.get('d'); // d = 'D'
Create a new SimpleLRU
instance with given max cache number. max
option
should be a positive number.
Set a new cache with given key and value.
Get a cache by key.
Get a cache without updating recently used order.
Delete a cache by key.
See if it has a cache with given key.
Return total number of cache items.
Clear all stored cache.
Return an Array of existing cache keys in least recently used order. The most recently used cache item will be the last.
Getter|Setter of max
option. Get a max
option if it has no argument. And
change max
option with an argument. It will trim cache when new max
option
is smaller than its length.
MIT