Chok’s Typescript Utilities
    Preparing search index...

    Function storeItem

    • Simple local storage cache with support for auto-expiration. Note that this works in the browser context only because nodejs does not have local storage.

      Create a cache item accessor object with auto-expiration. The value will always be stored as a string by applying JSON.stringify(), and will be returned in the same object type by applying JSON.parse().

      In order to provide proper type-checking, please always specify the T type parameter. E.g. const item = storeItem("name", 10_000);

      Type Parameters

      • T

      Parameters

      • key: string

        The store key in local storage.

      • expires: number | Duration

        Either a number in milliseconds, or a Duration object

      • logError: boolean = true

        Log an error if we found an invalid object in the store. The invalid object is usually a string that cannot be parsed as JSON.

      • OptionaldefaultValue: T

        Specify a default value to use for the object. Defaults to undefined.

      Returns CacheItem<T>