Function LRUCache

Creates a Least Recently Used (LRU) cache.

The returned object has the following methods:

  • delete: Deletes an item from the cache.
  • get: Returns the value of an item in the cache.
  • has: Returns whether an item is in the cache.
  • set: Sets an item in the cache.
  • refresh: Deletes all items from the cache.
const cache = LRUCache<string>({ max: 2, ttl: 1000 });
cache.set('a', 'A');
cache.set('b', 'B');
cache.set('c', 'C');
cache.get('a'); // undefined
cache.get('b'); // 'B'
cache.refresh();
cache.get('b'); // undefined
cache.set('d', 'D');
cache.get('d'); // 'D'
setTimeout(() => cache.get('d'), 1000); // undefined
  • Type Parameters

    • T

      The type of the cached items.

    Parameters

    • options: CacheOptions<T> = {}

      The cache options.

    Returns {
        delete: (key: unknown) => void;
        get: (key: unknown) => undefined | T;
        has: (key: unknown) => boolean;
        refresh: () => void;
        set: (key: unknown, value: T, ttlOverride?: number) => void;
    }

    The cache object.