Class LruCache<K, V>

An LRU Cache implementation. An LRU (Least Recently Used) Cache is a Map with a fixed capacity. When the capacity is exceeded, the least recently used element will be removed. An element is considered to be used when it has been retrieved via get, or when it's been set. Iteration does not affect ordering.

Type Parameters

  • K
  • V

Implements

Constructors

  • Type Parameters

    • K
    • V

    Parameters

    • initialCapacity: number

    Returns LruCache<K, V>

Properties

onEvict: null | ((element: V, key: K, list: CacheMap<K, V>) => void) = null

When an element is added when at capacity, the least recently used element will be removed. This callback can be set to be notified with the element being evicted, which may be used for disposal.

onEvicting: null | ((element: V, key: K, list: CacheMap<K, V>) => boolean) = null

An element is about to be removed. Should return true if the element may be evicted, otherwise false. If false is returned for all elements, an IllegalStateError is thrown.

Accessors

  • get [toStringTag](): string
  • Returns string

  • get capacity(): number
  • Returns number

  • set capacity(value): void
  • Parameters

    • value: number

    Returns void

  • get size(): number
  • Returns number

    the number of elements in the Map.

Methods

  • Returns IterableIterator<[K, V]>

  • Clears all elements from the cache.

    Returns void

  • 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.

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

    Returns IterableIterator<[K, V]>

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

    Parameters

    • callback: ((value: V, key: K, map: this) => void)

      A callback to invoke for every element in the cache.

        • (value, key, map): void
        • Parameters

          • value: V
          • key: K
          • map: this

          Returns void

    • OptionalthisArg: any

      Value to use as this when executing callback

    Returns void

  • Returns the mapped value for the given key, and moves the element to the most recently used position.

    Parameters

    • key: K

    Returns undefined | V

    Returns the element associated with the given key, or undefined if no such element exists.

  • Parameters

    • key: K

    Returns boolean

    boolean indicating whether an element with the specified key exists or not.

  • Returns an iterable of keys in the map. This will be ordered from least recently used to most.

    Returns IterableIterator<K>

  • Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated and position moved to most recently used.

    Parameters

    • key: K
    • value: V

    Returns this

  • Returns an iterable of values in the map. This will be ordered from least recently used to most.

    Returns IterableIterator<V>