Tiny naive LRU cache. 250 bytes gzipped.
npm i lur --save
It uses an array of tuples instead of a linked list or other structures like
Map
. Basically, it's a much simpler implemenation, without a few of the common
methods like peek
.
import lru from "lur";
const cache = lru(3); // max entities
cache.set("a", "1");
cache.set("b", "2");
cache.set("c", "3");
cache.get("a");
cache.set("d", "4");
cache.keys; // => [ 'd', 'a', 'c' ]
cache.values; // => [ '4', '1', '3' ]
cache.length; // => 3
cache.has("a"); // => true
cache.delete("a");
cache.values; // => [ '4', '3' ]
cache.clear();
cache.values; // => []
Of course, keys and values aren't limited to strings, though keep in mind
that lur
performs strict shallow equality checks:
const key = { a: 1 };
const value = { b: 2 };
cache.set(key, value);
cache.get(key); // => { b: 2 }
Although not strictly compatible with the concept of LRU, lur
also provides
methods of serialization and providing initial values:
const cache = lur(3, {
a: '1',
b: '2',
c: '3'
})
cache.keys // => ['a', 'b', 'c']
cache.serialize() // => { a: 1, b: 2, c: 3 }
Keep in mind, when using serialize
, non-string keys will be dropped.
MIT License © Eric Bailey