Class LruCache<K, V>Experimental

Least-recently-used cache.

UNSTABLE: New API, yet to be vetted.

Least-recently-used cache

Automatically removes entries above the max size based on when they were last accessed with get, set, or has.

import { LruCache } from "@std/cache";
import { assert, assertEquals } from "@std/assert";

const MAX_SIZE = 3;
const cache = new LruCache<string, number>(MAX_SIZE);

cache.set("a", 1);
cache.set("b", 2);
cache.set("c", 3);
cache.set("d", 4);

// most recent values are stored up to `MAX_SIZE`
assertEquals(cache.get("b"), 2);
assertEquals(cache.get("c"), 3);
assertEquals(cache.get("d"), 4);

// less recent values are removed
assert(!cache.has("a"));

Type Parameters

  • K

    The type of the cache keys.

  • V

    The type of the cache values.

Implements
Hierarchy
  • Map<K, V>
    • LruCache

Constructors

  • Experimental

    Constructs a new LruCache.

    Type Parameters

    • K
    • V

    Parameters

    • maxSize: number

      The maximum number of entries to store in the cache.

    Returns LruCache<K, V>

Properties

[toStringTag]: string
maxSize: number

The maximum number of entries to store in the cache.

import { LruCache } from "@std/cache";
import { assertEquals } from "@std/assert";

const cache = new LruCache<string, number>(100);
assertEquals(cache.maxSize, 100);
size: number

the number of elements in the Map.

[species]: MapConstructor

Methods

  • Experimental

    Returns an iterable of entries in the map.

    Returns MapIterator<[K, V]>

  • Experimental

    Returns void

  • Experimental

    Parameters

    • key: K

    Returns boolean

    true if an element in the Map existed and has been removed, or false if the element does not exist.

  • Experimental

    Returns an iterable of key, value pairs for every entry in the map.

    Returns MapIterator<[K, V]>

  • Experimental

    Executes a provided function once per each key/value pair in the Map, in insertion order.

    Parameters

    • callbackfn: ((value: V, key: K, map: Map<K, V>) => void)
        • (value, key, map): void
        • Parameters

          • value: V
          • key: K
          • map: Map<K, V>

          Returns void

    • OptionalthisArg: any

    Returns void

  • Experimental

    Gets the element with the specified key.

    Parameters

    • key: K

      The key to get the value for.

    Returns undefined | V

    The value associated with the specified key, or undefined if the key is not present in the cache.

    import { LruCache } from "@std/cache";
    import { assertEquals } from "@std/assert";

    const cache = new LruCache<string, number>(100);

    cache.set("a", 1);
    assertEquals(cache.get("a"), 1);
  • Experimental

    Checks whether an element with the specified key exists or not.

    Parameters

    • key: K

      The key to check.

    Returns boolean

    true if the cache contains the specified key, otherwise false.

    import { LruCache } from "@std/cache";
    import { assert } from "@std/assert";

    const cache = new LruCache<string, number>(100);

    cache.set("a", 1);
    assert(cache.has("a"));
  • Experimental

    Returns an iterable of keys in the map

    Returns MapIterator<K>

  • Experimental

    Sets the specified key to the specified value.

    Parameters

    • key: K

      The key to set the value for.

    • value: V

      The value to set.

    Returns this

    this for chaining.

    import { LruCache } from "@std/cache";

    const cache = new LruCache<string, number>(100);
    cache.set("a", 1);
  • Experimental

    Returns an iterable of values in the map

    Returns MapIterator<V>

  • Experimental

    Groups members of an iterable according to the return value of the passed callback.

    Type Parameters

    • K
    • T

    Parameters

    • items: Iterable<T, any, any>

      An iterable.

    • keySelector: ((item: T, index: number) => K)

      A callback which will be invoked for each item in items.

        • (item, index): K
        • Parameters

          • item: T
          • index: number

          Returns K

    Returns Map<K, T[]>