Class LRUCache<TKey, TValue>

A key value cache that implements the LRU policy.

Typeparam

TKey The type of the keys in the cache. Defaults to string.

Typeparam

TValue The type of the values in the cache. Defaults to any.

See

https://en.wikipedia.org/wiki/Cache_replacement_policies#Least_recently_used_(LRU)

Type Parameters

  • TKey = string

  • TValue = any

Hierarchy

  • LRUCache

Implements

Constructors

  • Creates a new instance of the LRUCache.

    Example

    // No options.
    const cache = new LRUCache();

    // With options.
    const cache = new LRUCache({
    entryExpirationTimeInMS: 10000
    });

    Type Parameters

    • TKey = string

    • TValue = any

    Parameters

    • Optional options: LRUCacheOptions<TKey, TValue>

      Additional configuration options for the LRUCache.

    Returns LRUCache<TKey, TValue>

Accessors

  • get maxSize(): number
  • set maxSize(value: number): void
  • Gets or sets the maxSize of the cache. This will evict the least recently used entries if needed to reach new maxSize.

    Example

    const cache = new LRUCache({ maxSize: 10 });

    cache.set('testKey', 'testValue');

    // Will be 10
    const maxSize = cache.maxSize;

    // Set new maxSize to 5. If there are more than 5 items in the cache, the least recently used entries will be removed until cache size is 5.
    cache.maxSize = 5;

    Returns number

  • Parameters

    • value: number

    Returns void

  • Returns the most recently used (newest) entry in the cache. This will not mark the entry as recently used. If the newest node is expired, it will be removed.

    Returns

    The most recently used (newest) entry in the cache.

    Example

    const cache = new LRUCache({ maxSize: 10 });

    cache.set('testKey', 'testValue');

    const newest = cache.newest;

    // Will log testValue
    console.log(newest.value);

    // Will log testKey
    console.log(newest.key);

    Returns null | LRUCacheEntry<TKey, TValue>

  • Returns the least recently used (oldest) entry in the cache. This will not mark the entry as recently used. If the oldest node is expired, it will be removed.

    Returns

    The least recently used (oldest) entry in the cache.

    Example

    const cache = new LRUCache({ maxSize: 10 });

    cache.set('testKey', 'testValue');

    const oldest = cache.oldest;

    // Will log testValue
    console.log(oldest.value);

    // Will log testKey
    console.log(oldest.key);

    Returns null | LRUCacheEntry<TKey, TValue>

  • get remainingSize(): number
  • Returns the number of entries that can still be added to the LRUCache without evicting existing entries.

    Returns

    The number of entries that can still be added without evicting existing entries.

    Example

    const cache = new LRUCache({ maxSize: 10 });

    cache.set('testKey', 'testValue');

    const remainingSize = cache.remainingSize;

    // Will log 9 due to 9 spots remaining before reaching maxSize of 10.
    console.log(remainingSize);

    Returns number

  • get size(): number
  • Returns the number of entries in the LRUCache object. If the cache has entryExpirationTimeInMS set, expired entries will be removed before the size is returned.

    Returns

    The number of entries in the cache.

    Example

    const cache = new LRUCache();

    cache.set('testKey', 'testValue');

    const size = cache.size;

    // Will log 1
    console.log(size);

    Returns number

Methods

  • [iterator](): Generator<LRUCacheEntry<TKey, TValue>, any, unknown>
  • Creates a Generator which can be used with for ... of ... to iterate over the cache entries. Iterates in order from most recently accessed entry to least recently. Expired entries will be skipped (and removed). No entry will be marked as accessed.

    Returns

    A Generator for the cache entries.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    for (const entry of cache) {
    const { key, value } = entry;
    // do something with the entry
    }

    Returns Generator<LRUCacheEntry<TKey, TValue>, any, unknown>

  • clear(): void
  • Removes all entries in the cache.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    // Clear cache.
    cache.clear();

    Returns void

  • delete(key: TKey): boolean
  • Deletes the entry for the passed in key.

    Returns

    True if an element in the LRUCache object existed and has been removed, or false if the element does not exist.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    // Will be true
    const wasDeleted = cache.delete('testKey');

    // Will be false
    const wasDeleted2 = cache.delete('keyNotInCache');

    Parameters

    • key: TKey

      The key of the entry to delete

    Returns boolean

  • Creates a Generator which can be used with for ... of ... to iterate over the cache entries. Iterates in order from most recently accessed entry to least recently. Expired entries will be skipped (and removed). No entry will be marked as accessed.

    Returns

    A Generator for the cache entries.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    for (const entry of cache.entries()) {
    const { key, value } = entry;
    // do something with the entry
    }

    Returns Generator<LRUCacheEntry<TKey, TValue>, any, unknown>

  • Searches the cache for an entry matching the passed in condition. Expired entries will be skipped (and removed). If multiply entries in the cache match the condition, the most recently used entry will be returned. If an entry is returned, this marks the returned entry as the most recently used entry.

    Returns

    The first cache entry to match the condition. Null if none match.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    // item will be { key: 'testKey', value: 'testValue }
    const item = cache.find(entry => {
    const { key, value } = entry;

    if (key === 'testKey' || value === 'something') {
    return true;
    }

    return false;
    });

    // item2 will be null
    const item2 = cache.find(entry => entry.key === 'notInCache');

    Parameters

    • condition: ((entry: LRUCacheEntry<TKey, TValue>) => boolean)

      The condition to apply to each entry in the

    Returns null | LRUCacheEntry<TKey, TValue>

  • forEach(callback: ((value: TValue, key: TKey, index: number) => void)): void
  • Iterates over and applies the callback function to each entry in the cache. Iterates in order from most recently accessed entry to least recently. Expired entries will be skipped (and removed). No entry will be marked as recently used.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    cache.forEach((key, value, index) => {
    // do something with key, value, and/or index
    });

    Parameters

    • callback: ((value: TValue, key: TKey, index: number) => void)

      the callback function to apply to the entry

        • (value: TValue, key: TKey, index: number): void
        • Parameters

          • value: TValue
          • key: TKey
          • index: number

          Returns void

    Returns void

  • get(key: TKey): null | TValue
  • Returns the value associated to the key, or null if there is none or if the entry is expired. If an entry is returned, this marks the returned entry as the most recently used entry.

    Returns

    The cached value or null.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    // Will be 'testValue'. Entry will now be most recently used.
    const item1 = cache.get('testKey');

    // Will be null
    const item2 = cache.get('keyNotInCache');

    Parameters

    • key: TKey

      The key of the entry to get.

    Returns null | TValue

  • has(key: TKey): boolean
  • Returns a boolean asserting whether a value has been associated to the key in the LRUCache object or not. This does not mark the entry as recently used. If the cache has a key but the entry is expired, it will be removed and false will be returned.

    Returns

    true if the cache contains the supplied key. False if not.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    // Will be true
    const wasDeleted = cache.has('testKey');

    // Will be false
    const wasDeleted2 = cache.has('keyNotInCache');

    Parameters

    • key: TKey

      The key of the entry to check if exists

    Returns boolean

  • keys(): Generator<TKey, any, unknown>
  • Creates a Generator which can be used with for ... of ... to iterate over the cache keys. Iterates in order from most recently accessed entry to least recently. Expired entries will be skipped (and removed). No entry will be marked as accessed.

    Returns

    A Generator for the cache keys.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    for (const key of cache.keys()) {
    // do something with the key
    }

    Returns Generator<TKey, any, unknown>

  • peek(key: TKey): null | TValue
  • Returns the value associated to the key, or null if there is none or if the entry is expired. If an entry is returned, this will not mark the entry as most recently accessed. Useful if a value is needed but the order of the cache should not be changed.

    Returns

    The cached value or null.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    // Will be 'testValue'
    const item1 = cache.peek('testKey');

    // Will be null
    const item2 = cache.peek('keyNotInCache');

    Parameters

    • key: TKey

      The key of the entry to get.

    Returns null | TValue

  • Sets the value for the key in the LRUCache object. Returns the LRUCache object. This marks the newly added entry as the most recently used entry. If adding the new entry makes the cache size go above maxSize, this will evict the least recently used entries until size is equal to maxSize.

    Returns

    The LRUCache instance.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    // Set the key key2 to value value2. Pass in optional options.
    cache.set('key2', 'value2', { entryExpirationTimeInMS: 10 });

    Parameters

    • key: TKey

      The key of the entry.

    • value: TValue

      The value to set for the key.

    • Optional entryOptions: LRUCacheSetEntryOptions<TKey, TValue>

      Additional configuration options for the cache entry.

    Returns LRUCache<TKey, TValue>

  • values(): Generator<TValue, any, unknown>
  • Creates a Generator which can be used with for ... of ... to iterate over the cache values. Iterates in order from most recently accessed entry to least recently. Expired entries will be skipped (and removed). No entry will be marked as accessed.

    Returns

    A Generator for the cache values.

    Example

    const cache = new LRUCache();

    // Set the key testKey to value testValue
    cache.set('testKey', 'testValue');

    for (const value of cache.values()) {
    // do something with the value
    }

    Returns Generator<TValue, any, unknown>

Generated using TypeDoc