import invariant from 'invariant'; import type { EntityConfiguration } from '../../EntityConfiguration.ts'; import type { IEntityCacheAdapter } from '../../IEntityCacheAdapter.ts'; import type { IEntityCacheAdapterProvider } from '../../IEntityCacheAdapterProvider.ts'; import type { IEntityLoadKey, IEntityLoadValue } from '../../internal/EntityLoadInterfaces.ts'; import type { CacheLoadResult } from '../../internal/ReadThroughEntityCache.ts'; import { CacheStatus } from '../../internal/ReadThroughEntityCache.ts'; export class NoCacheStubCacheAdapterProvider implements IEntityCacheAdapterProvider { getCacheAdapter, TIDField extends keyof TFields>( _entityConfiguration: EntityConfiguration, ): IEntityCacheAdapter { return new NoCacheStubCacheAdapter(); } } export class NoCacheStubCacheAdapter< TFields extends Record, TIDField extends keyof TFields, > implements IEntityCacheAdapter { public async loadManyAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >( key: TLoadKey, values: readonly TLoadValue[], ): Promise>> { return values.reduce((acc: Map>, v) => { acc.set(v, { status: CacheStatus.MISS, }); return acc; }, key.vendNewLoadValueMap>()); } public async cacheManyAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >(_key: TLoadKey, _objectMap: ReadonlyMap>): Promise {} public async cacheDBMissesAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >(_key: TLoadKey, _values: readonly TLoadValue[]): Promise {} public async invalidateManyAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >(_key: TLoadKey, _values: readonly TLoadValue[]): Promise {} } // Sentinel value we store in the in-memory cache to negatively cache a database miss. // The sentinel value is distinct from any (positively) cached value. export const DOES_NOT_EXIST = Symbol('inMemoryCacheDoesNotExistValue'); export class InMemoryFullCacheStubCacheAdapterProvider implements IEntityCacheAdapterProvider { private readonly cache: Map | typeof DOES_NOT_EXIST> = new Map(); getCacheAdapter, TIDField extends keyof TFields>( entityConfiguration: EntityConfiguration, ): IEntityCacheAdapter { return new InMemoryFullCacheStubCacheAdapter( entityConfiguration, this.cache as Map>, ); } } export class InMemoryFullCacheStubCacheAdapter< TFields extends Record, TIDField extends keyof TFields, > implements IEntityCacheAdapter { constructor( private readonly entityConfiguration: EntityConfiguration, private readonly cache: Map | typeof DOES_NOT_EXIST>, ) {} public async loadManyAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >( key: TLoadKey, values: readonly TLoadValue[], ): Promise>> { const results = key.vendNewLoadValueMap>(); values.forEach((value) => { const cacheKey = this.createCacheKey(key, value); if (!this.cache.has(cacheKey)) { results.set(value, { status: CacheStatus.MISS, }); } else { const objectForFieldValue = this.cache.get(cacheKey); invariant(objectForFieldValue !== undefined, 'should have set value for key'); if (objectForFieldValue === DOES_NOT_EXIST) { results.set(value, { status: CacheStatus.NEGATIVE, }); } else { results.set(value, { status: CacheStatus.HIT, item: objectForFieldValue, }); } } }); return results; } public async cacheManyAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >(key: TLoadKey, objectMap: ReadonlyMap>): Promise { objectMap.forEach((obj, value) => { const cacheKey = this.createCacheKey(key, value); this.cache.set(cacheKey, obj); }); } public async cacheDBMissesAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >(key: TLoadKey, values: readonly TLoadValue[]): Promise { values.forEach((value) => { const cacheKey = this.createCacheKey(key, value); this.cache.set(cacheKey, DOES_NOT_EXIST); }); } public async invalidateManyAsync< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >(key: TLoadKey, values: readonly TLoadValue[]): Promise { values.forEach((value) => { const cacheKey = this.createCacheKey(key, value); this.cache.delete(cacheKey); }); } private createCacheKey< TLoadKey extends IEntityLoadKey, TSerializedLoadValue, TLoadValue extends IEntityLoadValue, >(key: TLoadKey, value: TLoadValue): string { const cacheKeyType = key.getLoadMethodType(); const parts = key.createCacheKeyPartsForLoadValue(this.entityConfiguration, value); return [ this.entityConfiguration.tableName, cacheKeyType, `v${this.entityConfiguration.cacheKeyVersion}`, ...parts, ].join(':'); } }