Invalidation
Invalidation Strategies
Section titled “Invalidation Strategies”@requence/cache provides multiple ways to invalidate cached results, from surgical precision to broad sweeps.
By Specific Arguments
Section titled “By Specific Arguments”Invalidate a single cached result by its exact arguments:
await cache.invalidate('getUser', 'abc')By Function Key
Section titled “By Function Key”Invalidate all cached results for a function, regardless of arguments:
await cache.invalidateAll('getUser')By Tag
Section titled “By Tag”Invalidate all cached results that were tagged with a specific tag:
import { addCacheTag } from '@requence/cache'
const cache = createCache() .define('getUser', async (id: string) => { addCacheTag('user') addCacheTag(['user', id]) return db.users.findById(id) })
// Invalidate all user-related cachesawait cache.invalidateTag('user')
// Or invalidate a specific userawait cache.invalidateTag(['user', 'abc'])Cascade Invalidation
Section titled “Cascade Invalidation”When cached functions call other cached functions, tag invalidation cascades automatically:
const cache = createCache() .define('getBase', async (num: number) => { addCacheTag('numbers') return num * 2 }) .define('getDerived', async function (num: number) { return this.getBase(num + 1) })
// This invalidates both getDerived AND getBaseawait cache.invalidateTag('numbers')Scoped Invalidation
Section titled “Scoped Invalidation”Invalidation respects scopes — only the targeted scope is affected:
// Only clears the branch-123 scopeawait cache.withScope('branch-123').invalidateAll('getUser')
// Clears all entries in the branch-123 scopeawait cache.withScope('branch-123').clear()Full Reset
Section titled “Full Reset”Clear everything from the cache:
await cache.reset()Or reset all cache instances in the process:
import { resetAllCaches } from '@requence/cache'
await resetAllCaches()