@vicinae/api
    Preparing search index...

    Class Cache

    Caching abstraction that stores data on disk and supports LRU (least recently used) access. Values can only be stored as plain text strings, so it is up to you to serialize your data in an appropriate way. For instance, you could store json using JSON.stringify and JSON.parse it back. If you need to store binary data, you could encode it in base64.

    Unlike the local storage API, this API exclusively uses the extension's support directory to store its data. No calls to internal Vicinae APIs are required, hence why all methods in this class are synchronous. Another major difference is that cache data is not encrypted, so it's not suitable to store secrets. (local storage currently isn't either, but that will change in the future)

    Index

    Constructors

    Accessors

    • get isEmpty(): boolean

      Returns boolean

      whether the cache is empty.

    • get storageDirectory(): string

      Returns string

      the full path to the directory where the data is stored on disk.

      This is provided for informative purpose only. You should not attempt to mutate anything in this directory by yourself. Also note that the way cache data is serialized on disk can (will) change across versions.

    Methods

    • Clears all stored data. This also notifies registered subscribers (see subscribe) unless the notifySubscribers option is set to false.

      Parameters

      • Optionaloptions: { notifySubscribers: boolean }

      Returns void

    • Parameters

      • key: string

      Returns undefined | string

      the data for the given key, or undefined if there is no data.

      To solely check for existence of a key, use has.

    • Parameters

      • key: string

      Returns boolean

      true if data for the key exists, false otherwise.

      You can use this method to check for entries without affecting the LRU access.

    • Removes the data for the given key. This also notifies registered subscribers (see subscribe).

      Parameters

      • key: string

      Returns boolean

      true if data for the key was removed, false otherwise.

    • Sets the data for the given key. If the data exceeds the configured capacity, the least recently used entries are removed. This also notifies registered subscribers (see subscribe).

      Parameters

      • key: string
      • data: string

      Returns void

      An individual cache entry cannot be bigger than the configured capacity. If this happens, an error will be thrown.