Metreeca Keep
    Preparing search index...

    Function createCachingStore

    • Creates a caching store backed by a delegate.

      Serves repeated retrievals from an in-memory cache, sparing the delegate a round-trip whenever a prior retrieval can be reused; this cuts latency and backend load for read-heavy workloads, while writes and eviction keep cached results coherent and bounded.

      Retrievals are memoised under an (entry, model, locale, limit) key, where the model is canonicalised bottom-up so templates differing only in property or element order (including arrays of objects) share a cache entry, widening the set of requests a single cached result can serve.

      Important

      shape is deliberately excluded from the cache key: the retrieval result must be a pure function of (entry, model, locale, limit), with shape supplying only structural metadata that does not affect the returned payload.

      Important

      opts.locale is part of the cache key because it selects which localised content a retrieval returns. Unlike the model, the locale priority list is order-significant — ["en", "it"] and ["it", "en"] key separately — since order encodes language-negotiation preference. An omitted or empty locale list collapses to a single key, distinct from any explicit list.

      Important

      opts.limit is part of the cache key because it caps each selection's # and so changes the returned payload, with an omitted or 0 limit collapsing to a single unbounded key. opts.plain/opts.depth are excluded: they only accept or reject a model, never altering a successful payload.

      The cache offers three observable guarantees:

      • coalescing — concurrent retrievals for the same key resolve from a single delegate fetch, and a failed retrieval is never cached, so a later retrieval re-fetches;
      • read-after-write coherence — a retrieval never returns content staler than the latest write made through this store: create, update, delete, insert and remove invalidate matching entries, and a retrieval overlapping an in-progress write to the same entry is served from the delegate without being cached;
      • commit coherence — writes committed through execute, by other clients sharing the delegate, or signalled by the backend invalidate matching entries on commit, for as long as the store stays open (close ends further invalidation).

      The configurable match predicate selects which cached entries a write invalidates.

      Important

      Transaction Isolation — Inherited from the wrapped Store.

      Important

      Mutation Events — Inherited from the wrapped Store.

      Parameters

      • store: Store

        The delegate store to cache

      • options: {
            size?: number;
            ttl?: number;
            match?: (entry: string, cache: string) => boolean;
        } = {}

        Cache configuration options

        • Optional Readonlysize?: number

          Maximum number of records to cache.

          When the cache exceeds this bound, the least-recently-used record is evicted after each miss.

          0 (unlimited)
          
        • Optional Readonlyttl?: number

          Maximum record age in milliseconds.

          Records older than this value are treated as misses on the next retrieval and evicted when a sibling miss triggers the TTL sweep. Expiration is checked on every hit, so a stale record is never returned even if the sweep hasn't run yet.

          0 (no expiration)
          
        • Optional Readonlymatch?: (entry: string, cache: string) => boolean

          Predicate deciding which cached records an invalidation event discards.

          Invoked by both invalidation paths — pre-commit writes on the wrapper and reactive mutation events from the delegate — with the mutated resource's identifier and the identifier carried on each cached record. Return true to evict the record.

          () => true (every mutation clears the cache)

      Returns Store

      An immutable store with caching behaviour

      const cache = createCachingStore(restStore, { size: 1_000, ttl: 60_000 });
      const product = await cache.lookup({ entry, shape, model });