1.0.2 • Published 4 years ago

lur v1.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

lur

Tiny naive LRU cache. 250 bytes gzipped.

Install

npm i lur --save

Naive?

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.

Usage

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.

License

MIT License © Eric Bailey

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.1

4 years ago