Skip to main content

Read cache scopes

Every read of an entity reads its authoritative storage; there is no shared cache of entities across operations. A read cache scope lets one operation, eg a request or a page of a publication, reuse what it read before, so code that reads the same products from several places reads each of them once. Begin one with IVoxReadCacheScopeFactory for the asynchronous flow of the operation, and optionally preload into it:

using var cache = readCacheScopes.Begin();

await cache.PreloadEntitiesAsync<Product>(productIds, cancellationToken);
await cache.PreloadRelationsAsync<Category>(categoryIds, "products", cancellationToken);

// Entity stores, relation loaders, lazy relations and evaluation contexts read through it
await BuildCatalogAsync(cancellationToken);

While a scope is active, GetAsync and GetManyAsync of entity stores, the complete loads of IVoxRelationLoader, lazy relations such as product.Variants.LoadAsync(), and the reads of evaluation contexts use it. A miss reads the storage in one batch and adds what it read, including that an entity doesn't exist. Reads return copies, so changing an entity that was read doesn't change what's cached. Queries, searches, listings, pages and batches of relations aren't cached, since a set of entities that was read is no evidence of what a query or a page would return.

  • Partitions. What's cached is partitioned by the storage provider, the authoring scope, the selected contexts with their fallbacks, and the model, so a Swedish product, a draft product and a published one never share an entry.
  • Complete relations. A relation is cached only when every link of it was read and the entities it links to were read too. Counts and lookups of links, eg IVoxRelationReferences.ContainsAsync, are answered by a complete relation when the scope has one, but never make one, and neither does a page or a limited expansion.
  • Writes. A Vox write clears the active scope and the scopes it's in. A transaction doesn't use the scope, since its reads must see its staged changes, and clears it when it ends. Changes made by others aren't seen until a read misses: a scope isn't a snapshot of the storage.
  • Evaluations. Calculations, assessments and projections record what they read as dependencies before they read it, and only reuse what the scope read after that, so a change between an earlier read and the registration is read again rather than missed. Version validation and reads that change an entity always read the storage.
  • Budgets. VoxReadCacheScopeOptions limits the number and size of entities, the number of relations and the links of a relation that's cached. The oldest entries are evicted first, and Evict and Clear remove entries explicitly. Eviction only makes a later read read the storage again.
  • Nesting. A scope begun in another one is isolated from it, and the outer one is active again when it ends. Use IVoxReadCacheScopeFactory.Current to preload into the active one instead. Entities and their loaded relations stay usable after the scope ends, but a new read doesn't use it.

Vox's own evaluations read in a scope: each page of an entity projection publication, each group of recalculations, each batch of assessments, and each direct evaluation or materialization of projections, which joins the active scope of the request when there is one.