Class Cache<TKeys>

A cache that can be used to store data for the lifetime of the this object.

It uses LRU caching, so it will automatically remove the least recently used items when it reaches its limits. And also has expiries, so it will automatically replace items that have not been used for a while.

It's a small wrapper over LRUCache npm package.

Type Parameters

  • TKeys extends string

Constructors

Properties

Methods

Constructors

Properties

lruCache: LRUCache<string, {}, unknown> = ...

Type declaration

    Methods

    • Invalidate a cache item, so that it will be fetched again the next time it is requested.

      Parameters

      • key: TKeys

        The key to used to identify the cached data.

      Returns boolean

      Whether the cache item was invalidated.

    • Invalidate all cache items. This will cause all cache items to be fetched again the next time they are requested.

      Returns void

    • Wrap an async function with a cache.

      If the cache is empty, the async function will be run and the result will be stored in the cache. If the cache is not empty, the async function will not be run and the result will be returned from the cache.

      All the cached items are stored by reference, so if you modify the returned cached data, it will also modify the cached data internally. Nesting this function will also forward those references up the chain, so doing a cache wrap inside a cache wrap is a great way to share data between functions and avoid unnecessary object creation and garbage collection.

      Type Parameters

      • T

      Parameters

      • key: TKeys

        The key to used to identify the cached data.

      • operation: (() => Promise<T>)

        The async function to run if the cache is empty.

          • (): Promise<T>
          • Returns Promise<T>

      • Optional ttl: number

        The time to live of the cached data in milliseconds.

      Returns Promise<T>

      The result of the async function.

    Generated using TypeDoc