Class LFUCache<K, V>

Class representing an LFU (Least Frequently Used) Cache. Evicts the least frequently used items when the cache reaches its maximum capacity.

const cache = new LFUCache<string, number>(2); // Cache with capacity of 2 items
cache.put('a', 1);
cache.put('b', 2);
console.log(cache.get('a')); // Outputs: 1
cache.put('c', 3); // 'b' is evicted because it is the least frequently used
console.log(cache.get('b')); // Outputs: null (since 'b' was evicted)

Type Parameters

  • K

    The type of keys stored in the cache.

  • V

    The type of values stored in the cache.

Constructors

Methods

Constructors

Methods

  • Retrieves a value from the cache by key and updates the node's frequency.

    Parameters

    • key: K

      The key to retrieve from the cache.

    Returns V

    • The value associated with the key, or null if not found.
  • Adds a key-value pair to the cache. If the cache exceeds its capacity, it evicts the least frequently used item.

    Parameters

    • key: K

      The key to add to the cache.

    • value: V

      The value to associate with the key.

    Returns void