lur
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i lur

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

20.1 kB

Total Files

12

Last publish

Collaborators

  • estrattonbailey