Class ApiReponseCache<V>

Persistent API response cache based on level-db.

Type Parameters

  • V

Hierarchy

  • ApiReponseCache

Constructors

Properties

db: Level<string, string>

Level database instance

events: EventEmitter = ...

Event emitter for cache events

maxAgeMs: number

Max age of cached data in milliseconds. Defaults to 0 (no max age).

optionsDefaults: {
    dirpath: string;
    maxAgeMs: number;
    name: string;
} = ...

Default options for creating new instances

Type declaration

  • dirpath: string
  • maxAgeMs: number
  • name: string

Accessors

Methods

  • Delete a given value for a given hash key if it exists.

    Parameters

    • hash: string

      The hash key.

    Returns Promise<void>

    Remarks

    No error is thrown if no value exists for the given hash.

    Emits

    error - if deletion fails.

    Emits

    delete - if deletion succeeds.

  • Deletes a value from the cache if it is expired.

    Parameters

    • hash: string

      The hash key.

    • serialized: string

      The serialized value.

    Returns Promise<void>

    Emits

    expired - if the value is expired.

  • Iterate over all [key, value] pairs in the cache.

    Returns AsyncIterableIterator<[string, V]>

    Remarks

    This data entries are expired, they are deleted and not yielded.

    Emits

    error - if iteration encounters an error.

  • Get a value for a given hash key.

    Parameters

    • hash: string

      The hash key.

    Returns Promise<V>

    Emits

    error - if the value does not exist for the give hash.

    Emits

    get - if the value exists for the given hash.

  • Get a value for a given hash key if it exists. If the does not exist, returns a value from the api by invoking the provided function and then stores that value in the cache.

    Parameters

    • hash: string

      The hash key.

    • apiRequest: (() => V | Promise<V>)

      function that returns a new value for a given key if it doesn't exist in the cache.

        • (): V | Promise<V>
        • Returns V | Promise<V>

    Returns Promise<V>

    Emits

    hit - if the value exists in the cache.

    Emits

    miss - if the value does not exist in the cache.

  • Get a value for a given hash key or undefined if it does not exist or an error occurs.

    Parameters

    • hash: string

      The hash key.

    Returns Promise<undefined | V>

    Emits

    get - if the value exists for the given hash.

  • Shorthand for try/catch block with error-handling. Wrap a function call in a try catch block and emit an error event if an error occurs.

    Type Parameters

    • T

    Parameters

    • fn: (() => T | Promise<T>)

      The function to wrap.

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

    Returns T | Promise<T>

    The return value of the provided function.

    Emits

    error - if the provided function throws an error.

  • Parse the timestamp part of a raw serialized value string from the database.

    Parameters

    • serialized: string

      The serialized value.

    Returns number

  • Set a given value for a given hash key.

    Parameters

    • hash: string

      The hash key.

    • value: V

      The value to store.

    Returns Promise<V>

    Emits

    put - if insertion succeeds.

    Emits

    error - if insertion fails.

Generated using TypeDoc