flat-cache

  • Version 6.1.7
  • Published
  • 52.5 kB
  • 3 dependencies
  • MIT license

Install

npm i flat-cache
yarn add flat-cache
pnpm add flat-cache

Overview

A simple key/value storage using files to persist the data

Index

Functions

function clearAll

clearAll: (cacheDirectory?: string) => void;
  • Clear the cache directory clearAll

    Parameter cacheDir

    directory for the cache entry

function clearCacheById

clearCacheById: (cacheId: string, cacheDirectory?: string) => void;
  • Clear the cache identified by the given Id clearCacheById

    Parameter cacheId

    the id of the cache

    Parameter cacheDirectory

    directory for the cache entry

function create

create: (options?: FlatCacheOptions) => FlatCache;
  • Load a cache identified by the given Id. If the element does not exists, then initialize an empty cache storage.

    create

    Parameter docId

    the id of the cache, would also be used as the name of the file cache

    Parameter cacheDirectory

    directory for the cache entry

    Parameter options

    options for the cache

    Returns

    {cache} cache instance

function createFromFile

createFromFile: (filePath: string, options?: FlatCacheOptions) => FlatCache;
  • Load a cache from the provided file createFromFile

    Parameter filePath

    the path to the file containing the info for the cache

    Parameter options

    options for the cache

    Returns

    {cache} cache instance

Classes

class FlatCache

class FlatCache extends Hookified {}

    constructor

    constructor(options?: FlatCacheOptions);

      property cache

      readonly cache: CacheableMemory;
      • The cache object cache {CacheableMemory}

      property cacheDir

      cacheDir: string;
      • The cache directory cacheDir {String} '.cache'

      property cacheDirPath

      readonly cacheDirPath: string;
      • Returns the path to the cache directory cacheDirPath

        Returns

        {String}

      property cacheFilePath

      readonly cacheFilePath: string;
      • Returns the path to the file where the cache is persisted cacheFilePath

        Returns

        {String}

      property cacheId

      cacheId: string;
      • The cache id cacheId {String} 'cache1'

      property changesSinceLastSave

      readonly changesSinceLastSave: boolean;
      • The flag to indicate if there are changes since the last save changesSinceLastSave {Boolean} false

      property items

      readonly items: { key: string; value: any; expires?: number }[];
      • Returns an array with all the items in the cache { key, value, ttl } items

        Returns

        {Array}

      property persistInterval

      persistInterval: number;
      • The interval to persist the cache to disk. 0 means no timed persistence persistInterval {Number} 0

      method all

      all: () => Record<string, any>;
      • Returns the entire persisted object all

        Returns

        {*}

      method clear

      clear: () => void;
      • Clear the cache and save the state to disk clear

      method delete

      delete: (key: string) => void;
      • Remove a given key from the cache delete

        Parameter key

        the key to remove from the object

      method destroy

      destroy: (includeCacheDirectory?: boolean) => void;
      • Destroy the cache. This will remove the directory, file, and memory cache destroy

        Parameter includeCacheDir

        if true, the cache directory will be removed {undefined}

      method get

      get: <T>(key: string) => T;
      • Return the value of the provided key get

        Parameter key

        the name of the key to retrieve

        Returns

        {*} at T the value from the key

      method getKey

      getKey: <T>(key: string) => T;
      • (Legacy) Return the value of the provided key. This method will be deprecated in the future getKey

        Parameter key

        the name of the key to retrieve

        Returns

        {*} at T the value from the key

      method keys

      keys: () => string[];
      • Returns an array with all the keys in the cache keys

        Returns

        {Array}

      method load

      load: (cacheId?: string, cacheDir?: string) => void;
      • Load a cache identified by the given Id. If the element does not exists, then initialize an empty cache storage. If specified cacheDir will be used as the directory to persist the data to. If omitted then the cache module directory .cacheDir will be used instead

        load

        Parameter cacheId

        the id of the cache, would also be used as the name of the file cache

        Parameter cacheDir

        directory for the cache entry

      method loadFile

      loadFile: (pathToFile: string) => void;
      • Load the cache from the provided file loadFile

        Parameter pathToFile

        the path to the file containing the info for the cache

      method removeCacheFile

      removeCacheFile: () => boolean;
      • Remove the file where the cache is persisted removeCacheFile {Boolean} true or false if the file was successfully deleted

      method removeKey

      removeKey: (key: string) => void;
      • (Legacy) Remove a given key from the cache. This method will be deprecated in the future removeKey

        Parameter key

        the key to remove from the object

      method save

      save: (force?: boolean) => void;
      • Save the state of the cache identified by the docId to disk as a JSON structure save

      method set

      set: (key: string, value: any, ttl?: number | string) => void;
      • Sets a key to a given value set

        Parameter key

        the key to set

        Parameter value

        the value of the key. Could be any object that can be serialized with JSON.stringify

        Parameter ttl

        the time to live in milliseconds

      method setKey

      setKey: (key: string, value: any, ttl?: number | string) => void;
      • (Legacy) set key method. This method will be deprecated in the future setKey

        Parameter key

        the key to set

        Parameter value

        the value of the key. Could be any object that can be serialized with JSON.stringify

      method startAutoPersist

      startAutoPersist: () => void;
      • Start the auto persist interval startAutoPersist

      method stopAutoPersist

      stopAutoPersist: () => void;
      • Stop the auto persist interval stopAutoPersist

      class FlatCacheDefault

      class FlatCacheDefault {}

        property clearAll

        static clearAll: (cacheDirectory?: string) => void;

          property clearCacheById

          static clearCacheById: (cacheId: string, cacheDirectory?: string) => void;

            property create

            static create: (options?: FlatCacheOptions) => FlatCache;

              property createFromFile

              static createFromFile: (
              filePath: string,
              options?: FlatCacheOptions
              ) => FlatCache;

                Enums

                enum FlatCacheEvents

                enum FlatCacheEvents {
                SAVE = 'save',
                LOAD = 'load',
                DELETE = 'delete',
                CLEAR = 'clear',
                DESTROY = 'destroy',
                ERROR = 'error',
                EXPIRED = 'expired',
                }

                  member CLEAR

                  CLEAR = 'clear'

                    member DELETE

                    DELETE = 'delete'

                      member DESTROY

                      DESTROY = 'destroy'

                        member ERROR

                        ERROR = 'error'

                          member EXPIRED

                          EXPIRED = 'expired'

                            member LOAD

                            LOAD = 'load'

                              member SAVE

                              SAVE = 'save'

                                Type Aliases

                                type FlatCacheOptions

                                type FlatCacheOptions = {
                                ttl?: number | string;
                                useClone?: boolean;
                                lruSize?: number;
                                expirationInterval?: number;
                                persistInterval?: number;
                                cacheDir?: string;
                                cacheId?: string;
                                deserialize?: (data: string) => any;
                                serialize?: (data: any) => string;
                                };

                                  Package Files (1)

                                  Dependencies (3)

                                  Dev Dependencies (7)

                                  Peer Dependencies (0)

                                  No peer dependencies.

                                  Badge

                                  To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                  You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/flat-cache.

                                  • Markdown
                                    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/flat-cache)
                                  • HTML
                                    <a href="https://www.jsdocs.io/package/flat-cache"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>