You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/1.docs/7.cache.md
+52-8Lines changed: 52 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -328,7 +328,58 @@ When `swr` is disabled and a cached value has expired:
328
328
1. The stale entry is cleared.
329
329
2. The client waits for the function/handler to resolve with a fresh value.
330
330
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.
const data =awaitfetch(`https://api.github.com/repos/${repo}`).then(res=>res.json());
344
+
returndata.stargazers_count;
345
+
}, {
346
+
maxAge: 60*60,
347
+
name: "ghStars",
348
+
getKey: (repo:string) =>repo,
349
+
});
350
+
351
+
awaitcachedGHStars("unjs/nitro"); // populates the cache
352
+
awaitcachedGHStars.invalidate("unjs/nitro"); // removes the entry
353
+
awaitcachedGHStars("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
+
awaitinvalidateCache({
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
332
383
333
384
When using the `defineCachedFunction` or `defineCachedHandler` functions, the cache key is generated using the following pattern:
334
385
@@ -356,13 +407,6 @@ Will generate the following cache key:
356
407
cache:nitro/functions:getAccessToken:default.json
357
408
```
358
409
359
-
You can invalidate the cached function entry with:
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