Library API
    Preparing search index...

    Class WeakCache<K, V>

    Map-like cache holding values weakly via WeakRef and auto-pruning collected entries using Finalizer without memory leaks.

    Keys must be primitive (string | number | symbol) to ensure the key does not create a strong reference cycle with the cached value.

    Type Parameters

    • K extends keyof any = string
    • V extends WeakKey = WeakKey
    Index
    • Type Parameters

      • K extends string | number | symbol = string
      • V extends WeakKey = WeakKey

      Returns WeakCache<K, V>

    • get isWeakCache(): boolean

      Returns true to identify this instance as a WeakCache.

      Returns boolean

    • Clears all entries from the cache and unregisters all active finalizers.

      Returns void

    • Deletes a key from the cache and unregisters its finalizer.

      Parameters

      • key: K

        The cache key to delete

      Returns boolean

      True if an entry existed and was deleted, false otherwise

    • Retrieves a value from the cache by key. Returns undefined if the key does not exist or the value has been garbage collected.

      Parameters

      • key: K

        The cache key to retrieve

      Returns V | undefined

      The cached object or undefined

    • Returns the cached value for the key if present, or computes and caches it using the factory.

      Parameters

      • key: K

        The cache key

      • factory: (key: K) => V

        Factory function to create the value if absent

      Returns V

      The cached or newly computed value

    • Checks whether the cache contains a non-collected value for the given key.

      Parameters

      • key: K

        The cache key to check

      Returns boolean

      True if the key exists and its value has not been collected

    • Sets a key-value pair in the cache. The value is weakly held. If the key already exists, its previous registration is cleared.

      Parameters

      • key: K

        The cache key

      • value: V

        The object value to weakly retain

      Returns this

      This cache instance for chaining