Skip to content

Commit

Permalink
docs: reference new shouldInvalidateCache option
Browse files Browse the repository at this point in the history
  • Loading branch information
yassilah committed Jan 5, 2023
1 parent 14f5de5 commit 71e1ec8
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions docs/content/1.guide/1.introduction/5.cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const cachedFn = cachedEventHandler(fn, options)

### Options

- `name`: Handler name. It will be guessed from function name if not provided and fallback to `_` otherwise.
- `group`: Part of cache name. Useful to organize cache storage.
- `getKey`: A function that accepts same arguments of normal function and should generate cache key. If not provided, a built-in hash function will be used.
- `integrity`: A value that changing it, will invalidate all caches for function. By default will be computed from **function code**.
- `maxAge`: Maximum age that cache is valid in seconds. Default is `1` second.
- `swr`: Enable Stale-While-Revalidate behavior. Enabled by default.

* `name`: Handler name. It will be guessed from function name if not provided and fallback to `_` otherwise.
* `group`: Part of cache name. Useful to organize cache storage.
* `getKey`: A function that accepts same arguments of normal function and should generate cache key. If not provided, a built-in hash function will be used.
* `integrity`: A value that changing it, will invalidate all caches for function. By default will be computed from **function code**.
* `maxAge`: Maximum age that cache is valid in seconds. Default is `1` second.
* `swr`: Enable Stale-While-Revalidate behavior. Enabled by default.
* `shouldInvalidateCache`: A function that returns a boolean to invalidate the current cache and create a new one.

## Examples

Expand All @@ -26,31 +26,38 @@ const cachedFn = cachedEventHandler(fn, options)
```js
// routes/cached.ts
const myFn = cachedEventHandler(async () => {
new Promise(resolve => setTimeout(resolve, 1000))
return `Response generated at ${new Date().toISOString()}`
}, { swr: true })
new Promise(resolve => setTimeout(resolve, 1000))
return `Response generated at ${new Date().toISOString()}`
}, {
swr: true
})
```

**Example:** Cache a utility function

```js
// utils/index.ts
const myFn = cachedFunction(async () => {
new Promise(resolve => setTimeout(resolve, 1000))
return Math.random()
}, { swr: true })
new Promise(resolve => setTimeout(resolve, 1000))
return Math.random()
}, {
swr: true
})
```


**Example:** Enable Cache on a group of routes (**🧪 Experimental!**)

```js
// nitro.config.ts
import { defineNitroConfig } from 'nitropack'
import {
defineNitroConfig
} from 'nitropack'

export default defineNitroConfig({
routeRules: {
'/blog/**': { swr: true }
}
routeRules: {
'/blog/**': {
swr: true
}
}
})
```

0 comments on commit 71e1ec8

Please sign in to comment.