HP TV+ Cache Model
Description
HP TV+ includes/has plans to integrate several cache layers:
- Route cache: HP TV+ should (not yet implemented) a per route (instance) cache to ensure that a user will get a fast route response whenever possible
- Service/Provider cache: HP TV+ should (not yet implemented) a per Service/Provider method response cache (after parsing).
- Fetch/Request cache: HP TV+ implements a per http/request cache
- Memory cache to store Accedo Control session to keep avoid repeated
/sessionrequest
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 remove it from the Fetch cache opting for a more simplistic implementation, but the initial setup/config process is done in:
src/services/config.tssrc/services/manager.tssrc/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.
'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 HP TV+ cache all the HTTP fetch request (Accedo Control, OVP, ...).
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 (initally 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:keyset: set a cache value based on the provided key and value;. Paramskey,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', newValuer);