@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 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 the locale and limit of the lookup retrieval scope. 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, locale and limit, with shape supplying structural metadata that does not affect the returned payload.

      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 share a single cached result. The scope plain and depth are not part of the key: they only accept or reject a model, and never alter 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 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;
        } = {}

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

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