import nullthrows from '@expo/nullthrows'; import type { IEntityCacheAdapter } from './IEntityCacheAdapter.ts'; import type { IEntityLoadKey, IEntityLoadValue } from './internal/EntityLoadInterfaces.ts'; import type { CacheLoadResult } from './internal/ReadThroughEntityCache.ts'; import { CacheStatus } from './internal/ReadThroughEntityCache.ts'; /** * A IEntityCacheAdapter that composes other IEntityCacheAdapter instances. */ export class ComposedEntityCacheAdapter< TFields extends Record, TIDField extends keyof TFields, > implements IEntityCacheAdapter { /** * @param cacheAdapters - list of cache adapters to compose in order of precedence. * Earlier cache adapters are read from first and written to (including invalidations) last. * Typically, caches closer to the application should be ordered before caches closer to the database. * A lower layer cache is closer to the database, while a higher layer cache is closer to the application. */ constructor(private readonly cacheAdapters: IEntityCacheAdapter[]) {} public async loadManyAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >( key: TLoadKey, values: readonly TLoadValue[], ): Promise>> { const retMap = key.vendNewLoadValueMap>(); const fulfilledValuesByCacheIndex: TLoadValue[][] = Array.from( { length: this.cacheAdapters.length }, () => [], ); let unfulfilledValues = values; for (let i = 0; i < this.cacheAdapters.length; i++) { const cacheAdapter = nullthrows(this.cacheAdapters[i]); const cacheResultsFromAdapter = await cacheAdapter.loadManyAsync(key, unfulfilledValues); const newUnfulfilledValues = []; for (const [value, cacheResult] of cacheResultsFromAdapter) { if (cacheResult.status === CacheStatus.MISS) { newUnfulfilledValues.push(value); } else { retMap.set(value, cacheResult); nullthrows(fulfilledValuesByCacheIndex[i]).push(value); } } unfulfilledValues = newUnfulfilledValues; if (unfulfilledValues.length === 0) { break; } } // Recache values from lower layers that were not found in higher layers // Write to lower layers first for (let i = this.cacheAdapters.length - 1; i >= 0; i--) { const cacheAdapter = nullthrows(this.cacheAdapters[i]); const hitsToCache = key.vendNewLoadValueMap>(); const negativesToCache: TLoadValue[] = []; // Loop over all lower layer caches to collect hits and misses for (let j = i + 1; j < this.cacheAdapters.length; j++) { const fulfilledValues = nullthrows(fulfilledValuesByCacheIndex[j]); fulfilledValues.forEach((value) => { const cacheResult = nullthrows(retMap.get(value)); if (cacheResult.status === CacheStatus.HIT) { hitsToCache.set(value, cacheResult.item); } else if (cacheResult.status === CacheStatus.NEGATIVE) { negativesToCache.push(value); } }); } const promises = []; if (hitsToCache.size > 0) { promises.push(cacheAdapter.cacheManyAsync(key, hitsToCache)); } if (negativesToCache.length > 0) { promises.push(cacheAdapter.cacheDBMissesAsync(key, negativesToCache)); } await Promise.all(promises); } for (const value of unfulfilledValues) { retMap.set(value, { status: CacheStatus.MISS }); } return retMap; } public async cacheManyAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >(key: TLoadKey, objectMap: ReadonlyMap>): Promise { // write to lower layers first for (let i = this.cacheAdapters.length - 1; i >= 0; i--) { const cacheAdapter = nullthrows(this.cacheAdapters[i]); await cacheAdapter.cacheManyAsync(key, objectMap); } } public async cacheDBMissesAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >(key: TLoadKey, values: readonly TLoadValue[]): Promise { // write to lower layers first for (let i = this.cacheAdapters.length - 1; i >= 0; i--) { const cacheAdapter = nullthrows(this.cacheAdapters[i]); await cacheAdapter.cacheDBMissesAsync(key, values); } } public async invalidateManyAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >(key: TLoadKey, values: readonly TLoadValue[]): Promise { // delete from lower layers first for (let i = this.cacheAdapters.length - 1; i >= 0; i--) { const cacheAdapter = nullthrows(this.cacheAdapters[i]); await cacheAdapter.invalidateManyAsync(key, values); } } }