Options
All
  • Public
  • Public/Protected
  • All
Menu

Class CacheStore

Chowchow's cache store. This is the main class for managing cached data.

Hierarchy

  • CacheStore

Index

Constructors

  • new CacheStore(name: string, getFreshData: Callback, maxAge?: number, fileDir?: string, fileName?: string): CacheStore
  • Construct a new cache store object.

    Parameters

    • name: string

      Machine-readable name of the store.

    • getFreshData: Callback

      Callback that fetches the data to be cached.

    • maxAge: number = 15

      Maximum age of the cache in minutes.

    • fileDir: string = '.cache'

      Directory name the file lives in.

    • fileName: string = name

      Name of the JSON file the cached data lives in.

    Returns CacheStore

Properties

fileDir: string

Directory name the file lives in.

fileName: string

Name of the JSON file the cached data lives in.

getFreshData: Callback

Callback function that fetches the data to be cached.

maxAge: number

Maximum age of the cache in minutes.

name: string

Machine-readable name of the store.

Accessors

  • get filePath(): string
  • Full store file path.

    Returns string

Methods

  • ensureCacheFileExists(): void
  • Create the store directory.

    Returns void

  • Get the data.

    If the cache is not expired yet according to the expiration time, it will return the cache. Otherwise it will fetch new data, save it to the store, and return it.

    This method uses the following strategy:

    1. Check whether pre-existing cache is still valid and, if so, return it.
    2. Cache is not valid or does not yet exist. Get fresh data and validate it.
    3. Newly fetched data is valid. Write and return fresh data.

    Returns Promise<CachedData>

    Data.

  • getExpirationDate(): Date
  • Calculate the actual expiration date, i.e. now + expiration time.

    Returns Date

  • isCacheExpired(data?: CachedData): Promise<boolean>
  • Check if the cache is expired.

    Parameters

    • Optional data: CachedData

      Cached data to check. If no parameter is passed, this method will read the cache store using readCache.

    Returns Promise<boolean>

    true if the cache is expired. Otherwise false.

  • Read the cache, regardless of the expiration date.

    Returns Promise<CachedData>

    Cached data

  • Write the newly fetched data to the cache store.

    Parameters

    Returns Promise<void>

Generated using TypeDoc