Skip to content

Commit 6b238f3

Browse files
authored
docs(cache): invalidation using ocache (#4216)
1 parent 2f02e40 commit 6b238f3

1 file changed

Lines changed: 52 additions & 8 deletions

File tree

docs/1.docs/7.cache.md

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,58 @@ When `swr` is disabled and a cached value has expired:
328328
1. The stale entry is cleared.
329329
2. The client waits for the function/handler to resolve with a fresh value.
330330

331-
## Cache keys and invalidation
331+
## Cache invalidation
332+
333+
Cached entries can be invalidated programmatically at runtime (for example from a webhook when the underlying data changes) without waiting for `maxAge` to expire.
334+
335+
### `.invalidate()` method
336+
337+
Every function created with `defineCachedFunction` exposes an `.invalidate()` method. Arguments are passed through `getKey` to generate the cache key.
338+
339+
```ts
340+
import { defineCachedFunction } from "nitro/cache";
341+
342+
const cachedGHStars = defineCachedFunction(async (repo: string) => {
343+
const data = await fetch(`https://api.github.com/repos/${repo}`).then(res => res.json());
344+
return data.stargazers_count;
345+
}, {
346+
maxAge: 60 * 60,
347+
name: "ghStars",
348+
getKey: (repo: string) => repo,
349+
});
350+
351+
await cachedGHStars("unjs/nitro"); // populates the cache
352+
await cachedGHStars.invalidate("unjs/nitro"); // removes the entry
353+
await cachedGHStars("unjs/nitro"); // re-invokes the function
354+
```
355+
356+
If no cached entry matches the given arguments, `.invalidate()` resolves without error and leaves storage unchanged.
357+
358+
### `invalidateCache()` helper
359+
360+
`invalidateCache` is a helper from `ocache`. It invalidates a cached entry from the cache options used to define it, so invalidation can live anywhere in your app, independent of where the cached function is defined.
361+
362+
Pass the same `name`, `group`, `base`, and `getKey` used when defining the function, along with the `args` identifying the entry to remove:
363+
364+
```ts
365+
import { invalidateCache } from "ocache";
366+
367+
await invalidateCache({
368+
options: {
369+
name: "ghStars",
370+
group: "nitro/functions",
371+
getKey: (repo: string) => repo,
372+
},
373+
args: ["unjs/nitro"],
374+
});
375+
```
376+
377+
::important
378+
The `name`, `group`, `base`, and `getKey` passed to `invalidateCache` must match the ones used when the cached function was defined. Mismatched options resolve to a different storage key and will not invalidate the intended entry. :br :br
379+
Nitro defaults to `group: 'nitro/functions'` for cached functions and `group: 'nitro/handlers'` for cached handlers (or `'nitro/route-rules'` when using [route rules](#using-route-rules)).
380+
::
381+
382+
## Cache keys
332383

333384
When using the `defineCachedFunction` or `defineCachedHandler` functions, the cache key is generated using the following pattern:
334385

@@ -356,13 +407,6 @@ Will generate the following cache key:
356407
cache:nitro/functions:getAccessToken:default.json
357408
```
358409

359-
You can invalidate the cached function entry with:
360-
361-
```ts
362-
import { useStorage } from "nitro/storage";
363-
364-
await useStorage('cache').removeItem('nitro/functions:getAccessToken:default.json')
365-
```
366410

367411
::note
368412
For cached handlers, the cache key includes a hash of the URL path and, when using the [`varies`](#handler-only-options) option, hashes of the specified header values appended to the key.

0 commit comments

Comments
 (0)