@stephen-shopopop/cache
    Preparing search index...

    Class LRUCache<K, V>

    A Least Recently Used (LRU) cache implementation. Keeps track of how recently entries were accessed and removes the least recently used entry when cache exceeds maxSize.

    const cache = new LRUCache<string, number>({ maxSize: 3 });
    cache.set('a', 1);
    cache.get('a'); // Returns 1

    Illustration of the design:

      entry             entry             entry             entry
      ______            ______            ______            ______
     | head |.newer => |      |.newer => |      |.newer => | tail |
     |  A   |          |  B   |          |  C   |          |  D   |
     |______| <= older.|______| <= older.|______| <= older.|______|
    

    removed <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- <-- added

    Architecture Diagram: LRUCache

    ┌───────────────────────────────────────────────┐ │ LRUCache │ ├───────────────────────────────────────────────┤ │ #cache: Map<K, V> │ │ #maxSize: number │ └───────────────────────────────────────────────┘ │ ▼ ┌───────────────────────────────┐ │ Map<K, V> │ └───────────────────────────────┘

    • Most recently used entries are at the end (tail), least recently used at the start (head).
    • When maxSize is reached, the oldest (head) entry is removed on insert.
    • Accessing an entry moves it to the most recently used position.
    • All data is stored in memory only.

    Type Parameters

    • K

      The type of the keys stored in the cache

    • V

      The type of the values stored in the cache

    Index

    Constructors

    Accessors

    Methods

    Constructors

    • Creates a new instance of the LRU Cache.

      Type Parameters

      • K
      • V

      Parameters

      • options: Readonly<LRUCacheOptions>

        Configuration options for the LRU Cache

        • maxSize

          The maximum number of items the cache can hold. Must be a non-negative integer.

      Returns LRUCache<K, V>

      When maxSize is not a non-negative integer

    Accessors

    • get size(): number

      Gets the number of items currently stored in the cache.

      Returns number

      The total number of cached items.

    Methods

    • Removes all key-value entries from the cache.

      Returns void

      void

    • Removes the specified key and its associated value from the cache.

      Parameters

      • key: K

        The key to remove from the cache

      Returns boolean

      True if the element was successfully removed, false if the key was not found

    • Returns an iterator that contains an array of [key, value] pairs for every cache entry. The iterator is iterating over entries in order of the least recently used items first.

      Returns IterableIterator<[K, V]>

      An iterator containing tuples of key-value pairs

    • Retrieves the value associated with the specified key from the cache. If the key exists, it will be moved to the most recently used position.

      Parameters

      • key: K

        The key to look up in the cache

      Returns undefined | V

      The value associated with the key if it exists, undefined otherwise

    • Checks if a key exists in the cache

      Parameters

      • key: K

        The key to check for existence

      Returns boolean

      true if the key exists in the cache, false otherwise

    • Sets a key-value pair in the cache. If the key already exists, it reorders it to maintain LRU order. If the cache exceeds maximum size, it removes the least recently used item.

      Parameters

      • key: K

        The key to set in the cache

      • value: V

        The value to associate with the key

      Returns void

    • Removes and returns the oldest entry (first inserted) from the cache

      Returns undefined | [K, V]

      A tuple containing the key and value of the removed entry, or undefined if the cache is empty

      const cache = new CacheLRU<string, number>();
      cache.set('a', 1);
      cache.set('b', 2);
      const [key, value] = cache.shift(); // Returns ['a', 1]