Class Cache<K, T>

Map of data which tracks whether it is a complete collection & offers optional expiry of cached values

Type Parameters

  • K extends string | number | symbol
  • T

Indexable

  • [key: string | number | symbol]: T | any

    Support index lookups

Constructors

  • Create new cache

    Type Parameters

    • K extends string | number | symbol
    • T

    Parameters

    • Optionalkey: keyof T

      Default property to use as primary key

    • options: CacheOptions = {}

    Returns Cache<K, T>

Properties

complete: boolean = false

Whether cache is complete

key?: keyof T

Default property to use as primary key

options: CacheOptions = {}
values: T[] = ...

Get all cached items

Array of items

Methods

  • Add a new item to the cache. Like set, but finds key automatically

    Parameters

    • value: T

      Item to add to cache

    • ttl: any = ...

      Override default expiry

    Returns this

  • Add several rows to the cache

    Parameters

    • rows: T[]

      Several items that will be cached using the default key

    • complete: boolean = true

      Mark cache as complete & reliable, defaults to true

    Returns this

  • Get all cached items

    Returns T[]

    Array of items

  • Remove all keys from cache

    Returns void

  • Delete an item from the cache

    Parameters

    • key: K

      Item's primary key

    Returns void

  • Return cache as an array of key-value pairs

    Returns [K, T][]

    Key-value pairs array

  • Get item from the cache

    Parameters

    • key: K

      Key to lookup

    Returns T

    Cached item

  • Get a list of cached keys

    Returns K[]

    Array of keys

  • Get map of cached items

    Returns Record<K, T>

  • Add an item to the cache manually specifying the key

    Parameters

    • key: K

      Key item will be cached under

    • value: T

      Item to cache

    • ttl: undefined | number = ...

      Override default expiry in seconds

    Returns this