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
    isWeakCache: WeakCacheTypeGuard = ...

    Type guard to check if a value is an authentic WeakCache instance

    • 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

    • 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