Options
All
  • Public
  • Public/Protected
  • All
Menu

Class LRUCache<Key, Value>

Fixed size cache that evicts its entries in least-recently-used order when it overflows. Modeled after standard JavaScript Map otherwise.

Type parameters

  • Key

  • Value

Hierarchy

  • LRUCache

Index

Constructors

constructor

  • new LRUCache(cacheCapacity: number, sizeFunction?: (v: Value) => number): LRUCache
  • Creates a new instance of LRUCache.

    The optional [[sizeFunction]] can be used to fine tune the memory consumption of all cached elements, thus [[cacheCapacity]] means then memory used (in MBs). Otherwise, if [[sizeFunction]] is not specified, the [[cacheCapacity]] accounts for the maximum number of elements stored.

    Parameters

    • cacheCapacity: number

      Number used to configure the maximum cache size, may express number of entries or memory consumed in megabytes depending on [[sizeFunction]].

    • Default value sizeFunction: (v: Value) => number = () => 1

      A function determining the size per element.

        • (v: Value): number
        • Parameters

          • v: Value

          Returns number

    Returns LRUCache

Properties

Optional canEvict

canEvict: undefined | ((key: Key, value: Value) => boolean)

Optional callback that is called on every item that should be evicted from the cache to determine if it can be removed, or should be locked in the cache.

It returns true if the item can be removed from cache, false otherwise. Locking items in the cache should be a temporary measure, since if the cache is filled with non-evictable items only, it may grow beyond its capacity.

Note: This callback is not called when an item is explicitly deleted from the map via delete or clear.

Optional evictionCallback

evictionCallback: undefined | ((key: Key, value: Value) => void)

Optional callback that is called on every item that is evicted from the cache.

Note: This callback is not called when an item is explicitly deleted from the map via delete or clear.

Accessors

capacity

  • get capacity(): number
  • Returns the maximum capacity of the cache, i.e. the maximum number of elements this cache can contain or the total amount of memory that may be consumed by cache if element size function was specified in cache c-tor.

    Returns number

    The capacity of the cache.

map

  • get map(): Map<Key, Entry<Key, Value>>
  • deprecated
    • DO NOT USE. Will be removed in future versions.

    Returns the internal map object that keeps the key-value pairs and their order.

    Returns Map<Key, Entry<Key, Value>>

    The internal map object.

newest

  • get newest(): Entry<Key, Value> | null

oldest

  • get oldest(): Entry<Key, Value> | null
  • Returns the oldest entry in the cache.

    Note: Does not promote the oldest item as most recently used item.

    Returns Entry<Key, Value> | null

    Oldest entry in the cache.

size

  • get size(): number

Methods

clear

  • clear(): void

delete

  • delete(key: Key): boolean
  • Explicitly removes a key-value pair from the cache.

    Note: This is an explicit removal, thus, the eviction callback will not be called.

    Parameters

    • key: Key

      The key of the key-value pair to delete.

    Returns boolean

    true if the key-value pair existed and was deleted, false otherwise.

Protected evict

  • evict(): void

evictAll

  • evictAll(): void

Protected evictOldest

  • evictOldest(): Entry<Key, Value> | undefined

evictSelected

  • evictSelected(selector: (value: Value, key: Key) => boolean, thisArg?: any): void
  • Evict selected elements from the cache using [[selector]] function.

    Parameters

    • selector: (value: Value, key: Key) => boolean

      The function for selecting elements for eviction.

        • (value: Value, key: Key): boolean
        • Parameters

          • value: Value
          • key: Key

          Returns boolean

    • Optional thisArg: any

      Optional this object reference.

    Returns void

forEach

  • forEach(callbackfn: (value: Value, key: Key, map: LRUCache<Key, Value>) => void, thisArg?: any): void
  • Iterates over all items from the most recently used item to the least recently used one.

    Note: Results are undefined if the entire cache is modified during iteration. You may although modify the current element in [[callbackfn]] function.

    Parameters

    • callbackfn: (value: Value, key: Key, map: LRUCache<Key, Value>) => void

      The callback to call for each item.

        • (value: Value, key: Key, map: LRUCache<Key, Value>): void
        • Parameters

          • value: Value
          • key: Key
          • map: LRUCache<Key, Value>

          Returns void

    • Optional thisArg: any

      Optional this argument for the callback.

    Returns void

get

  • get(key: Key): Value | undefined
  • Looks up key in the cache and returns the associated value.

    Parameters

    • key: Key

      The key to look up.

    Returns Value | undefined

    The associated value, or undefined if the key-value pair is not in the cache.

has

  • has(key: Key): boolean
  • Test if a key/value pair is in the cache.

    Parameters

    • key: Key

      The key to look up.

    Returns boolean

    true if the key-value pair is in the cache, false otherwise.

set

  • set(key: Key, value: Value): void
  • Inserts or updates a key/value pair in the cache.

    If the key already existed in the cache, it will be updated and promoted to the most recently used item.

    If the key didn't exist in the cache, it will be inserted as most recently used item. An eviction of the least recently used item takes place if the cache exceeded its capacity.

    Parameters

    • key: Key

      The key for the key-value pair to insert or update.

    • value: Value

      The value for the key-value pair to insert or update.

    Returns void

setCapacity

  • setCapacity(newCapacity: number): void
  • Resets the capacity of this cache. If newCapacity is smaller than the current cache size, all items will be evicted until the cache shrinks to newCapacity.

    Parameters

    • newCapacity: number

      The new capacity of this cache.

    Returns void

setCapacityAndMeasure

  • setCapacityAndMeasure(newCapacity: number, sizeMeasure?: (v: Value) => number): void
  • Resets the cache capacity and function used to measure the element size.

    Parameters

    • newCapacity: number

      The new capacity masured in units returned from [[sizeMeasure]] funtion.

    • Default value sizeMeasure: (v: Value) => number = () => 1

      Function that defines the size of element, if you want to measure number of elements only always return 1 from this function (default), you may also specify own function that measures entries by memory consumed, nubmer of sub-elements, etc.

        • (v: Value): number
        • Parameters

          • v: Value

          Returns number

    Returns void

shrinkToCapacity

  • shrinkToCapacity(): void
  • Updates the size of all elements in this cache. If their aggregated size is larger than the capacity, items will be evicted until the cache shrinks to fit the capacity.

    Returns void

Generated using TypeDoc