2.3.15 • Published 10 days ago

@thi.ng/cache v2.3.15

Weekly downloads
713
License
Apache-2.0
Repository
github
Last release
10 days ago

cache

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

In-memory cache implementations with different eviction strategies.

Available strategies

  • LRU: Least Recently Used
  • TLRU: Time-aware Least Recently Used
  • MRU: Most Recently Used

Features

  • ES6 Map-like API (with minor differences)
  • Supports any types for both keys & values
  • Customizable cache limits (no. of items / actual size)
  • Customizable key equality checks (@thi.ng/equiv by default)
  • Optional item release callbacks (to clean up resources when value is expunged)

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

  • @thi.ng/associative - Alternative Map and Set implementations with customizable equality semantics & supporting operations

Installation

yarn add @thi.ng/cache

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/cache"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const cache = await import("@thi.ng/cache");

Package sizes (gzipped, pre-treeshake): ESM: 1.03 KB

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Filterable commit log UI w/ minimal server to provide commit historyDemoSource

API

Generated API docs

Common config options

All caches support at least the following options (all optional):

interface CacheOpts<K, V> {
    /**
     * Key size calculation
     */
    ksize: (k: K) => number;
    /**
     * Value size calculation
     */
    vsize: (v: V) => number;
    /**
     * Eviction callback to clean up resources
     */
    release: (k: K, v: V) => void;
    /**
     * Factory for ES6 Map compatible instance
     * to index cache entries
     */
    map: () => Map<K, any>;
    /**
     * Max number of items in cache (default: ∞)
     */
    maxlen: number;
    /**
     * Max cache size (computed via `ksize` & `vsize`) (default: ∞)
     */
    maxsize: number;
}

LRU

Removes least recently used items if a new item is added, but would not satisfy cache limit. Every time a cached item is accessed, it's recency is updated.

import * as cache from "@thi.ng/cache";

// caches can be configured with maxLen, maxSize and sizing functions (see below)
const lru = new cache.LRUCache<string, number>(null, { maxlen: 3 });
lru.set("foo", 23);
lru.set("bar", 42);
lru.set("baz", 66);

lru.has("foo");
// true
// retrieving a value from the cache updates its timestamp
lru.get("foo");
// 23

// caches are fully iterable
// largely intended for inspection, does not update recency
// btw. "foo" appears last since most recently accessed
[...lru]
// [ { k: 'bar', v: 42, s: 0 },
//   { k: 'baz', v: 66, s: 0 },
//   { k: 'foo', v: 23, s: 0 } ]
[...lru.keys()]
// [ 'bar', 'baz', 'foo' ]
[...lru.values()]
// [ 42, 66, 23 ]

// remove from cache
lru.delete("foo");
// true

// caches have a getSet() method to obtain & store a new value
// if its key is not known. this process is asynchronous
lru.getSet("boo", () => Promise.resolve(999)).then(console.log);
// 999

// the given retrieval fn is only called if there's a cache miss
// (not the case here). `getSet()` always returns a promise
lru.getSet("boo", () => Promise.resolve(123)).then(console.log);
// 999

// caches can be limited by size instead of (or in addition to)
// number of items. the meaning of `size` is user-defined.
// sizing fns can be provided for both keys & values (both default to 0)
// here we multiply value size by 8 since JS numbers are doubles by default
// we also provide a release hook for demo purposes

// the first arg is an iterable of KV pairs to store (just as for Map)
lru = new cache.LRUCache<string, number[]>(
    [ ["a", [1.0, 2.0]], ["b", [3.0, 4.0, 5.0]] ],
    {
        maxsize: 32,
        ksize: (k) => k.length,
        vsize: (v) => v.length * 8,
        release: (k, v) => console.log("release", k, v)
    }
);
// release a [1, 2] ("a" is evicted due to maxsize constraint)
lru.size
// 25

[...lru.keys()]
// [ 'b' ]

TLRU

Time-aware LRU cache. Extends LRU strategy with TTL (time-to-live) values associated with each entry. has() will only return true and get() only returns a cached value if its TTL hasn't yet expired. When adding a new value to the cache, first removes expired entries and if there's still not sufficient space removes entries in LRU order. set() takes an optional entry specific ttl arg. If not given, uses the cache instance's default (provided via ctor option arg). If no instance TTL is given, TTL defaults to 1 hour.

// same opts as LRUCache, but here with custom TTL period (in ms)
tlru = new cache.TLRUCache(null, { ttl: 10000 });

// with item specific TTL (500ms)
tlru.set("foo", 42, 500)

MRU

Similar to LRU, but removes most recently accessed items first. Wikipedia

// same opts as LRUCache
mru = new cache.MRUCache();

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-cache,
  title = "@thi.ng/cache",
  author = "Karsten Schmidt",
  note = "https://thi.ng/cache",
  year = 2018
}

License

© 2018 - 2021 Karsten Schmidt // Apache Software License 2.0

2.3.15

10 days ago

2.3.14

28 days ago

2.3.13

1 month ago

2.3.12

2 months ago

2.3.11

2 months ago

2.3.0

5 months ago

2.3.2

4 months ago

2.3.1

4 months ago

2.3.4

4 months ago

2.3.3

4 months ago

2.3.6

4 months ago

2.3.5

4 months ago

2.3.8

3 months ago

2.3.7

3 months ago

2.3.9

3 months ago

2.2.17

6 months ago

2.2.18

5 months ago

2.2.15

6 months ago

2.2.16

6 months ago

2.2.19

5 months ago

2.2.20

5 months ago

2.2.21

5 months ago

2.3.10

3 months ago

2.2.14

8 months ago

2.2.13

8 months ago

2.2.12

8 months ago

2.2.11

8 months ago

2.2.10

8 months ago

2.2.9

9 months ago

2.2.8

9 months ago

2.2.7

9 months ago

2.2.6

9 months ago

2.2.5

9 months ago

2.2.4

9 months ago

2.2.3

9 months ago

2.2.1

10 months ago

2.2.2

9 months ago

2.1.106

10 months ago

2.2.0

10 months ago

2.1.105

10 months ago

2.1.104

10 months ago

2.1.103

10 months ago

2.1.102

10 months ago

2.1.99

10 months ago

2.1.101

10 months ago

2.1.100

10 months ago

2.1.98

10 months ago

2.1.96

10 months ago

2.1.95

11 months ago

2.1.94

11 months ago

2.1.92

11 months ago

2.1.93

11 months ago

2.1.91

11 months ago

2.1.89

11 months ago

2.1.88

11 months ago

2.1.87

12 months ago

2.1.86

12 months ago

2.1.85

1 year ago

2.1.83

1 year ago

2.1.84

1 year ago

2.1.82

1 year ago

2.1.81

1 year ago

2.1.80

1 year ago

2.1.58

1 year ago

2.1.59

1 year ago

2.1.56

1 year ago

2.1.57

1 year ago

2.1.54

1 year ago

2.1.55

1 year ago

2.1.52

1 year ago

2.1.51

1 year ago

2.1.69

1 year ago

2.1.67

1 year ago

2.1.68

1 year ago

2.1.65

1 year ago

2.1.66

1 year ago

2.1.63

1 year ago

2.1.64

1 year ago

2.1.61

1 year ago

2.1.62

1 year ago

2.1.60

1 year ago

2.1.78

1 year ago

2.1.79

1 year ago

2.1.77

1 year ago

2.1.74

1 year ago

2.1.75

1 year ago

2.1.72

1 year ago

2.1.73

1 year ago

2.1.70

1 year ago

2.1.71

1 year ago

2.1.50

2 years ago

2.1.49

2 years ago

2.1.47

2 years ago

2.1.48

2 years ago

2.1.45

2 years ago

2.1.46

2 years ago

2.1.43

2 years ago

2.1.44

2 years ago

2.1.41

2 years ago

2.1.42

2 years ago

2.1.40

2 years ago

2.1.39

2 years ago

2.1.38

2 years ago

2.1.36

2 years ago

2.1.34

2 years ago

2.1.35

2 years ago

2.1.32

2 years ago

2.1.33

2 years ago

2.1.31

2 years ago

2.1.27

2 years ago

2.1.28

2 years ago

2.1.25

2 years ago

2.1.26

2 years ago

2.1.23

2 years ago

2.1.24

2 years ago

2.1.21

2 years ago

2.1.22

2 years ago

2.1.29

2 years ago

2.1.30

2 years ago

2.1.19

2 years ago

2.1.20

2 years ago

2.1.18

2 years ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.14

2 years ago

2.1.15

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.11

2 years ago

2.1.9

3 years ago

2.1.10

3 years ago

2.1.8

3 years ago

2.1.7

3 years ago

2.1.6

3 years ago

2.1.5

3 years ago

2.0.9

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.0

3 years ago

2.0.8

3 years ago

2.0.4

3 years ago

2.0.7

3 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.94

3 years ago

1.0.93

3 years ago

1.0.92

3 years ago

1.0.91

3 years ago

1.0.89

3 years ago

1.0.90

3 years ago

1.0.88

3 years ago

1.0.87

3 years ago

1.0.86

3 years ago

1.0.85

3 years ago

1.0.84

4 years ago

1.0.83

4 years ago

1.0.82

4 years ago

1.0.81

4 years ago

1.0.80

4 years ago

1.0.79

4 years ago

1.0.78

4 years ago

1.0.77

4 years ago

1.0.76

4 years ago

1.0.75

4 years ago

1.0.71

4 years ago

1.0.70

4 years ago

1.0.69

4 years ago

1.0.68

4 years ago

1.0.67

4 years ago

1.0.66

4 years ago

1.0.65

4 years ago

1.0.64

4 years ago

1.0.63

4 years ago

1.0.62

4 years ago

1.0.61

4 years ago

1.0.60

4 years ago

1.0.59

4 years ago

1.0.58

4 years ago

1.0.57

4 years ago

1.0.56

4 years ago

1.0.55

4 years ago

1.0.54

4 years ago

1.0.53

4 years ago

1.0.51

4 years ago

1.0.52

4 years ago

1.0.50

4 years ago

1.0.49

4 years ago

1.0.48

5 years ago

1.0.47

5 years ago

1.0.46

5 years ago

1.0.45

5 years ago

1.0.44

5 years ago

1.0.43

5 years ago

1.0.42

5 years ago

1.0.41

5 years ago

1.0.40

5 years ago

1.0.39

5 years ago

1.0.38

5 years ago

1.0.37

5 years ago

1.0.36

5 years ago

1.0.35

5 years ago

1.0.34

5 years ago

1.0.33

5 years ago

1.0.32

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.2.40

6 years ago

0.2.39

6 years ago

0.2.38

6 years ago

0.2.37

6 years ago

0.2.36

6 years ago

0.2.35

6 years ago

0.2.34

6 years ago

0.2.33

6 years ago

0.2.32

6 years ago

0.2.31

6 years ago

0.2.30

6 years ago

0.2.29

6 years ago

0.2.28

6 years ago

0.2.27

6 years ago

0.2.26

6 years ago

0.2.25

6 years ago

0.2.24

6 years ago

0.2.24-alpha.1

6 years ago

0.2.24-alpha.0

6 years ago

0.2.23

6 years ago

0.2.22

6 years ago

0.2.21

6 years ago

0.2.20

6 years ago

0.2.19

6 years ago

0.2.18

6 years ago

0.2.17

6 years ago

0.2.16

6 years ago

0.2.15

6 years ago

0.2.14

6 years ago

0.2.13

7 years ago

0.2.12

7 years ago

0.2.11

7 years ago

0.2.9

7 years ago

0.2.8

7 years ago

0.2.7

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.0

7 years ago

0.0.1

7 years ago