Cache
Cache<
TEntity,TResult>(setKeyOrOptions,deleteKeysFn?,invalidateKeysFn?):MethodDecorator
Defined in: packages/ddd-core/persistence/decorators/cache.decorator.ts:148
Write-through cache decorator for a CommandRepository save method.
Provides declarative cache synchronization on mutating database operations:
- Write-through update: When
save()resolves with a non-nullish result, the result is cached under the key generated bysetKey(after invalidating any secondary keys viainvalidateKeys). - Cache eviction on delete: When
save()resolves withnullorundefined(e.g. entity deletion), all keys returned bydeleteKeysare evicted from the cache. - Best-effort safety: Cache write and eviction operations swallow errors internally so that an already-committed database transaction is never converted into an application error.
- Explicit keys: Requires at least one of
setKey,deleteKeysorinvalidateKeys; scoping the key (for example withcacheKey) is the key function’s job.
Type Parameters
Section titled “Type Parameters”TEntity
Section titled “TEntity”TEntity = unknown
TResult
Section titled “TResult”TResult = unknown
Parameters
Section titled “Parameters”setKeyOrOptions
Section titled “setKeyOrOptions”CacheOptions<TEntity> | ((entity) => string) | null
deleteKeysFn?
Section titled “deleteKeysFn?”((entity) => string[]) | null
invalidateKeysFn?
Section titled “invalidateKeysFn?”((entity) => string[]) | null
Returns
Section titled “Returns”MethodDecorator
Examples
Section titled “Examples”Positional syntax on creation / update
@Cache<User, UserSnapshot>( (user) => cacheKey(User.aggregateName, { id: user.id }), null, (user) => [cacheKey(User.aggregateName, { email: user.email })],)async save(user: User): Promise<UserSnapshot> { ... }Cache maintenance runs only after the wrapped persistence method succeeds and
is best-effort: cache failures are logged rather than converting an already
durable write into an application failure. Pair this decorator with
@AcknowledgePersisted and @MapPersistenceErrors in the canonical order.
Options object syntax on eviction / delete
@Cache<User, null>({ deleteKeys: (user) => [ cacheKey(User.aggregateName, { id: user.id }), cacheKey(User.aggregateName, { email: user.email }), ],})async save(user: User): Promise<null> { ... }