@grafctory/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 result can be reused. This cuts latency and backend load for read-heavy workloads, while writes and eviction keep cached results coherent and bounded.

      Results are cached by entry, model and virtual, and by the locale and limit of the retrieval scope of lookup. Templates differing only in member or element order share a cached result.

      Important

      shape is not part of the cache key. A retrieval result MUST depend only on entry, model, virtual, locale and limit, with shape supplying structural metadata that does not affect the returned payload.

      Important

      The request virtual flag is part of the cache key, because virtual and held retrievals of the same entry return different collections. An omitted and a false flag share a single cached result.

      Important

      The scope 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"] are cached separately. An omitted and an empty locale list share a single cached result, distinct from any explicit list.

      Important

      The scope limit is part of the cache key, because it caps the page size of each collection and so changes the returned payload. An omitted and a 0 limit are both closed and share a single cached result, while an Infinity limit has its own. The scope stats and depth are not part of the key. They only accept or reject a model, and never alter a successful payload, so lookups differing only in them share a cached result.

      The scope option fills in the members a lookup scope leaves out before the key is built. A lookup stating no scope thus shares a cached result with one stating the default bounds explicitly. The wrapped store receives each lookup scope as filled in, so the cached result matches what the lookup would have retrieved.

      The cache offers three observable guarantees:

      • coalescing — concurrent retrievals for the same key resolve from a single delegate fetch, and a failed retrieval or one finding no resource 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;
            scope?: StoreScope;
        } = {}

        Cache configuration options, and the default retrieval scope

        • 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 for writes made through the wrapper and for mutation events signalled by the delegate. It receives the identifier of the mutated resource and the identifier of each cached record, and returns true if the record is to be evicted.

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

        • Optional Readonlyscope?: StoreScope

          The retrieval scope that fills in the members a lookup scope leaves out, one by one, before the lookup is matched against the records already cached. It is a fallback rather than a ceiling, so it never caps the bounds a lookup states for itself, even when they are wider.

          {}, leaving every lookup within the closed default bounds

      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 });