import invariant from 'invariant'; import type { ISecondaryEntityCache } from './EntitySecondaryCacheLoader.ts'; import type { IEntityGenericCacher } from './IEntityGenericCacher.ts'; import { CacheStatus } from './internal/ReadThroughEntityCache.ts'; import { filterMap, zipToMap } from './utils/collections/maps.ts'; /** * A custom secondary read-through entity cache is a way to add a custom second layer of caching for a particular * single entity load. One common way this may be used is to add a second layer of caching in a hot path that makes * a call to EntityLoader.loadManyByFieldEqualityConjunctionAsync is guaranteed to return at most one entity. */ export abstract class GenericSecondaryEntityCache< TFields extends Record, TIDField extends keyof TFields, TLoadParams, > implements ISecondaryEntityCache { constructor( private readonly cacher: IEntityGenericCacher, private readonly constructCacheKey: (params: Readonly) => string, ) {} public async loadManyThroughAsync( loadParamsArray: readonly Readonly[], fetcher: ( fetcherLoadParamsArray: readonly Readonly[], ) => Promise, Readonly | null>>, ): Promise, Readonly | null>> { const cacheKeys = loadParamsArray.map(this.constructCacheKey); const cacheKeyToLoadParamsMap = zipToMap(cacheKeys, loadParamsArray); const cacheLoadResults = await this.cacher.loadManyAsync(cacheKeys); invariant( cacheLoadResults.size === loadParamsArray.length, `${this.constructor.name} loadMany should return a result for each key`, ); const cacheKeysToFetch = Array.from( filterMap( cacheLoadResults, (cacheLoadResult) => cacheLoadResult.status === CacheStatus.MISS, ).keys(), ); // put cache hits and negative cache hits in result map const results: Map, Readonly | null> = new Map(); cacheLoadResults.forEach((cacheLoadResult, cacheKey) => { if (cacheLoadResult.status === CacheStatus.HIT) { const loadParams = cacheKeyToLoadParamsMap.get(cacheKey); invariant(loadParams !== undefined, 'load params should be in cache key map'); results.set(loadParams, cacheLoadResult.item); } else if (cacheLoadResult.status === CacheStatus.NEGATIVE) { const loadParams = cacheKeyToLoadParamsMap.get(cacheKey); invariant(loadParams !== undefined, 'load params should be in cache key map'); results.set(loadParams, null); } }); // fetch any misses from DB, add DB objects to results, cache DB results, inform cache of any missing DB results if (cacheKeysToFetch.length > 0) { const loadParamsToFetch = cacheKeysToFetch.map((cacheKey) => { const loadParams = cacheKeyToLoadParamsMap.get(cacheKey); invariant(loadParams !== undefined, 'load params should be in cache key map'); return loadParams; }); const fetchResults = await fetcher(loadParamsToFetch); const fetchMisses = loadParamsToFetch.filter((loadParams) => { // all values of fetchResults should be field objects or undefined return !fetchResults.get(loadParams); }); for (const fetchMiss of fetchMisses) { results.set(fetchMiss, null); } const objectsToCache: Map> = new Map(); for (const [loadParams, object] of fetchResults.entries()) { if (object) { objectsToCache.set(this.constructCacheKey(loadParams), object); results.set(loadParams, object); } } await Promise.all([ this.cacher.cacheManyAsync(objectsToCache), this.cacher.cacheDBMissesAsync(fetchMisses.map(this.constructCacheKey)), ]); } return results; } /** * Invalidate the cache for objects cached by constructCacheKey(loadParams). * * @param loadParamsArray - load params to invalidate */ public invalidateManyAsync(loadParamsArray: readonly Readonly[]): Promise { const cacheKeys = loadParamsArray.map(this.constructCacheKey); return this.cacher.invalidateManyAsync(cacheKeys); } }