Skip to content

Invalidation

@requence/cache provides multiple ways to invalidate cached results, from surgical precision to broad sweeps.

Invalidate a single cached result by its exact arguments:

await cache.invalidate('getUser', 'abc')

Invalidate all cached results for a function, regardless of arguments:

await cache.invalidateAll('getUser')

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 caches
await cache.invalidateTag('user')
// Or invalidate a specific user
await cache.invalidateTag(['user', 'abc'])

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 getBase
await cache.invalidateTag('numbers')

Invalidation respects scopes — only the targeted scope is affected:

// Only clears the branch-123 scope
await cache.withScope('branch-123').invalidateAll('getUser')
// Clears all entries in the branch-123 scope
await cache.withScope('branch-123').clear()

Clear everything from the cache:

await cache.reset()

Or reset all cache instances in the process:

import { resetAllCaches } from '@requence/cache'
await resetAllCaches()