Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Cache<T>

Provides a generic caching mechanism useful for a number of cases

Type parameters

  • T

Hierarchy

  • Cache

Index

Constructors

constructor

  • new Cache(timeToLive: number, maxItems: number): Cache
  • Parameters

    • timeToLive: number

      Time a cached item should live before expiring, in milliseconds

    • maxItems: number

      Amount of items that can be held in the cache maximum.

    Returns Cache

Methods

fetch

  • Fetch a value from the cache, calling the given function to generate the value if it is not present or it is expired. The result of the function is then cached for future calls.

    Use options.missStrategy to control the revalidation behavior. The default value ("fulfill") will wait until the fetch operation completes to return a value in the case where a cache miss occurs, even if an expired cache value is present. The "stale-revalidate" option will instead return the value found in the cache even when expired, executing the fetch operation if the value is expired and saving the resulting value back into the cache for future calls.

    Parameters

    • key: string

      The key to fetch

    • Optional fetcher: CacheFetcher<T>

      A function to define the value of the key if a cache miss occurs

    • Default value options: FetchOptions = {}

      Options for doing the caching.

    Returns Promise<T>

get

  • get(key: string): T
  • Get the value present in the cache for the given key regardless of the cache entry's expiration status.

    Parameters

    • key: string

      The key to get

    Returns T

getEntry

getOldestEntry

insertItem

  • insertItem(key: string, value: T, timeToLive?: number): void
  • Insert the specified item into the cache under the given key, optionally specifying a custom time-to-live. If no time-to-live is specified, the class-wide default is used.

    Parameters

    • key: string

      The key for the cache entry

    • value: T

      The value to cache

    • Optional timeToLive: number

      How long the value should remain valid for before revalidation

    Returns void

Generated using TypeDoc