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

    Class LRUCacheWithTTL<K, V>

    A Least Recently Used (LRU) cache implementation with Time-To-Live (TTL) support.

    const cache = new CacheLRUWithTTL<string, number>({
    maxSize: 100,
    ttl: 60000
    });
    cache.set("key", 123); // Store value with default TTL
    cache.set("key2", 456, 30000); // Store with custom TTL
    • Entries are automatically removed when they expire based on TTL
    • Least recently used entries are removed when cache reaches maxSize
    • Optional automatic cleanup of expired entries at specified intervals

    When constructor options have invalid types or values:

    • maxSize must be a non-negative integer
    • ttl must be a non-negative integer
    • cleanupInterval must be >= 1000ms
    • stayAlive must be a boolean

    Type Parameters

    • K

      The type of keys stored in the cache

    • V

      The type of values stored in the cache

    Index

    Constructors

    • Creates a new instance of CacheLRUWithTTL

      Type Parameters

      • K
      • V

      Parameters

      • options: Readonly<LRUCacheWithTTLOptions>

        Configuration options for the cache

        • maxSize

          Maximum number of items the cache can hold. Must be a non-negative integer

        • ttl

          Time to live in milliseconds for cache items. Must be a non-negative integer

        • stayAlive

          If true, keeps the cleanup timer running even when process.exit is called

        • cleanupInterval

          Interval in milliseconds between cleanup runs. Must be at least 1000ms

      Returns LRUCacheWithTTL<K, V>

      If any of the options have invalid values

    Accessors

    • get size(): number

      Gets the number of key-value pairs in the cache.

      Returns number

      The total number of entries in the cache.

    Methods

    • Stops the cleanup timer for expired cache entries if one is running. This should be called when shutting down the cache to prevent memory leaks.

      Returns void

    • Removes all entries from the cache. After calling this method, the cache will be empty.

      Returns void

    • Removes the specified key from the cache.

      Parameters

      • key: K

        The key to remove from the cache

      Returns boolean

      Returns true if an element in the cache existed and has been removed, or false if the element does not exist

    • Retrieves a value from the cache by its key

      Parameters

      • key: K

        The key to look up in the cache

      Returns undefined | V

      The value associated with the key if it exists and hasn't expired, undefined otherwise

    • Checks if a key exists in the cache and is not expired

      Parameters

      • key: K

        The key to check in the cache

      Returns boolean

      True if the key exists and has not expired, false otherwise

    • Sets a key-value pair in the cache with an optional time-to-live (TTL).

      Parameters

      • key: K

        The key to store the value under

      • value: V

        The value to be stored

      • Optionalttl: number

        Optional TTL in milliseconds. If not provided, uses the cache's default TTL if set, otherwise the entry won't expire

      Returns void

      void

      cache.set("key", "value", 1000); // Expires after 1 second
      cache.set("key2", "value2"); // Uses default TTL or never expires
    • Removes and returns the first (oldest) item from the cache.

      Returns undefined | [K, V]

      The first key-value pair that was removed, or undefined if the cache was empty.