common-stuff
    Preparing search index...

    Function cache

    • Caches the result of a function call by its arguments.

      Returned function exposes .clear() and .delete(...args) for manual invalidation. Optional maxSize enables LRU eviction; optional ttl (ms) makes entries expire. Promises are unwrapped — only the resolved value is stored, so concurrent calls share one inflight Promise.

      Type Parameters

      • F extends (...args: any[]) => any

      Parameters

      • func: F
      • Optionaloptions: { maxSize?: number; ttl?: number }
        • OptionalmaxSize?: number

          When set, evicts the least-recently-used entry once the cache exceeds this size.

        • Optionalttl?: number

          Time-to-live in milliseconds. Entries older than this are recomputed on read.

      Returns Cached<F>

      const fetchUser = cache(
      (id: string) => fetch(`/users/${id}`).then(r => r.json()),
      { maxSize: 100, ttl: 60_000 },
      )
      fetchUser('1') // network call
      fetchUser('1') // cached
      fetchUser.delete('1') // drop one entry
      fetchUser.clear() // drop all