Skip to main content

Class: WriteThroughCache

Defined in: src/cache/WriteThroughCache.ts:34

High-performance Write-Through & Read-Through caching coordinator. Automatically synchronizes cache state with database operations, evicts stale entries, and handles L2 entity cache invalidation on mutations.

Example​

const wtCache = new WriteThroughCache();

// Read-through caching
const user = await wtCache.readThrough(`user:${id}`, () => fetchUserFromDb(id), { tags: ['users'] });

// Write-through persistence
await wtCache.saveEntityThrough(User, user.id, user, u => ctx.set(User).update(u.id, u));

Constructors​

Constructor​

new WriteThroughCache(options?): WriteThroughCache

Defined in: src/cache/WriteThroughCache.ts:39

Parameters​

ParameterType
options?WriteThroughCacheOptions

Returns​

WriteThroughCache

Methods​

readThrough()​

readThrough<T>(key, loader, options?): Promise<T>

Defined in: src/cache/WriteThroughCache.ts:48

Retrieves an item from cache if present; otherwise runs loader(), caches the result, and returns it.

Type Parameters​

Type Parameter
T

Parameters​

ParameterType
keystring
loader() => Promise<T>
options?ReadThroughOptions

Returns​

Promise<T>


writeThrough()​

writeThrough<T>(key, value, writer, options?): Promise<T>

Defined in: src/cache/WriteThroughCache.ts:68

Executes a database write and updates the cache with the newly persisted data.

Type Parameters​

Type Parameter
T

Parameters​

ParameterType
keystring
valueT
writer(val) => Promise<T>
options?ReadThroughOptions

Returns​

Promise<T>


deleteThrough()​

deleteThrough(key, deleter, invalidateTags?): Promise<void>

Defined in: src/cache/WriteThroughCache.ts:82

Executes a database delete, evicts the cache key, and invalidates any related tags.

Parameters​

ParameterType
keystring
deleter() => Promise<void>
invalidateTags?string[]

Returns​

Promise<void>


readEntityThrough()​

readEntityThrough<T>(target, id, loader, ttlMs?): Promise<T | null>

Defined in: src/cache/WriteThroughCache.ts:97

Read-through helper specifically for entities by primary key via the L2 EntityCache.

Type Parameters​

Type Parameter
T extends object

Parameters​

ParameterType
targetEntityTarget<T>
idunknown
loader() => Promise<T | null>
ttlMs?number

Returns​

Promise<T | null>


saveEntityThrough()​

saveEntityThrough<T>(target, id, entity, persister, ttlMs?): Promise<T>

Defined in: src/cache/WriteThroughCache.ts:119

Write-through helper for entities: executes persister, writes to L2 EntityCache, and invalidates query cache collections tagged with the entity type.

Type Parameters​

Type Parameter
T extends object

Parameters​

ParameterType
targetEntityTarget<T>
idunknown
entityT
persister(entity) => Promise<T>
ttlMs?number

Returns​

Promise<T>


deleteEntityThrough()​

deleteEntityThrough<T>(target, id, deleter): Promise<void>

Defined in: src/cache/WriteThroughCache.ts:138

Delete helper for entities: executes deleter, removes from L2 EntityCache, and invalidates collection tags.

Type Parameters​

Type Parameter
T extends object

Parameters​

ParameterType
targetEntityTarget<T>
idunknown
deleter() => Promise<void>

Returns​

Promise<void>


attachToContext()​

attachToContext(context): this

Defined in: src/cache/WriteThroughCache.ts:153

Attaches this cache to a DbContext instance, listening for entity lifecycle events and automatically invalidating corresponding L2 cache entries and query tags.

Parameters​

ParameterType
contextDbContext

Returns​

this

Properties​

taggedCache​

readonly taggedCache: TaggedQueryCache

Defined in: src/cache/WriteThroughCache.ts:35


entityCache​

readonly entityCache: EntityCache

Defined in: src/cache/WriteThroughCache.ts:36