@stephen-shopopop/cache
    Preparing search index...

    Class MemoryCacheStore<K, Metadata>

    A memory-based cache store implementation using LRU (Least Recently Used) cache strategy.

    This cache store implementation provides the following features:

    • Maximum entry size limit (default: 5MB)
    • Maximum total cache size limit (default: 100MB)
    • Maximum number of entries limit (default: 1024)
    • LRU eviction policy
    • Support for string and Buffer values
    • Associated metadata for each cache entry
    const cache = new MemoryCacheStore<string, { timestamp: number }>({
    maxCount: 100,
    maxSize: 1024 * 1024, // 1MB
    maxEntrySize: 64 * 1024 // 64KB
    });

    Type Parameters

    • K

      The type of keys used in the cache

    • Metadata extends object = Record<PropertyKey, unknown>

      The type of metadata associated with cached values, extends object

    Index

    Constructors

    Accessors

    Methods

    Constructors

    • Creates a new instance of MemoryCacheStore.

      Type Parameters

      • K
      • Metadata extends object = Record<PropertyKey, unknown>

      Parameters

      • options: Readonly<MemoryCacheStoreOptions>

        Configuration options for the memory cache store

        • maxCount

          Maximum number of entries allowed in the cache. Must be a non-negative integer.

        • maxEntrySize

          Maximum size of a single entry in bytes. Must be a non-negative integer.

        • maxSize

          Maximum total size of all entries in bytes. Must be a non-negative integer.

      Returns MemoryCacheStore<K, Metadata>

      If any of the numeric options are not non-negative integers

    Accessors

    • get byteSize(): number

      Returns the current byte size of the cache

      Returns number

      The size of the cache in bytes

    • get size(): number

      Gets the total number of entries stored in the memory cache.

      Returns number

      The number of key-value pairs currently stored in the cache.

    Methods

    • Clears all data from the cache store and resets the size counter to zero. This operation removes all key-value pairs from the internal Map storage.

      Returns void

    • Deletes an entry from the cache by its key.

      Parameters

      • key: K

        The key of the cache entry to delete

      Returns boolean

      True if an element was removed successfully, false if the key was not found

      cache.delete("myKey"); // returns true if key exists and was deleted
      
    • Retrieves the value associated with the specified key from the cache.

      Parameters

      • key: K

        The key to lookup in the cache

      Returns undefined | Value<Metadata>

      The cached value and metadata if found, undefined otherwise

    • Checks if a value exists for the specified key in the cache

      Parameters

      • key: K

        The key to check in the cache

      Returns boolean

      True if the key exists in the cache, false otherwise

    • Sets a key-value pair in the memory cache with associated metadata.

      Parameters

      • key: K

        The key to store the value under

      • value: string | Buffer<ArrayBufferLike>

        The value to store, must be a string or Buffer

      • metadata: Metadata = ...

        Associated metadata for the cached entry, must be a object

      Returns void

      If value is not a string or Buffer

      If entry size exceeds configured maxEntrySize