Skip to main content

Assemble Web Cache Model

Description

Assemble Web includes/has plans to integrate several cache layers:

  • Route cache: Assemble Web should (not yet implemented) use a per route (instance) cache to ensure that a user will get a fast route response whenever possible
  • Service/Provider cache: Assemble Web should (not yet implemented) use a per Service/Provider method response cache (after parsing).
  • Fetch/Request cache: Assemble Web implements a per http/request cache
  • Memory cache to store Accedo Control session to avoid repeated /session request

Route cache Details

To Be defined.

We need to investigate how to use Next.js capabilities to optimize how routes are cached

Service/Provider cache Details

The initial Service/Provider cache configuration model was defined to be mixed within the Fetch/Request cache but we finally removed it from the Fetch cache opting for a more simplistic implementation, but the initial setup/config process is done in:

  • src/services/config.ts
  • src/services/manager.ts
  • src/services/cache.ts

and the initial config usage and extra service fetcher utility was created in:

  • src/utils/fetcher.ts

The current implementation sets a cache configuration in src/services/config/ts, a cache object where you can pass either a defaultCache or a function name for more granularity. The value of these is a revalidateSecs key that is parsed into a NextJS time-based revalidation header.

const serviceConfig: ServiceRegistry = {
// ...
ovp: {
provider: new AccedoOvp(),
cache: {
defaultCache: { revalidateSecs: 5 * 60 },
getMovie: { revalidateSecs: 2 * 60 },
},
},
};

Fetch/Request cache

Using the Next.js request cache and a default ENV VAR configurable CACHE_REVALIDATION + default DEFAULT_CACHE_REVALIDATION constant Assemble Web cache all the HTTP fetch request (Accedo Control, OVP, ...).

warning

Next.js doesn't allow to define the revalidate value on config and the appInitialization is done before routes/layout are handled from Next.js

In-Memory cache

We have created the file src/utils/memoryCache.ts to implement a very basic memory store cache.

There we define the possible keys for such store (initially just CONTROL_SESSION_KEY) but more can be added if it fits the projects needs.

Methods

  • get: get a cache if exists from the memory cache. Params: key
  • set: set a cache value based on the provided key and value;. Params key, value

Exports

  • CacheKeysTypes: enum of keys for the Memory Cache.
  • CacheKeysTypesKeys: Union of the keys for the Memory cache.
  • MemoryCacheType: Memory Cache type for casting.
  • MemoryCacheType: Memory cache class

Usage

import { MemoryCache } from '@/utils/memoryCache';

const cache = MemoryCache.getInstance();
const prevValue = cache.get('key');
cache.set('key', newValue);