cacheLife
cacheLife 函数用于设置函数或组件的缓存生命周期。它应该与 use cache 指令一起使用,并在函 数或组件的范围内。
用法
要使用 cacheLife,请在 next.config.js 文件中启用 dynamicIO 标志:
- TypeScript
- JavaScript
next.config.ts
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
experimental: {
dynamicIO: true,
},
}
export default nextConfig
next.config.js
const nextConfig = {
experimental: {
dynamicIO: true,
},
}
export default nextConfig
然后,在函数或组件的范围内导入并调用 cacheLife 函数:
- TypeScript
- JavaScript
app/page.tsx
'use cache'
import { unstable_cacheLife as cacheLife } from 'next/cache'
export default async function Page() {
cacheLife('hours')
return <div>Page</div>
}
app/page.js
'use cache'
import { unstable_cacheLife as cacheLife } from 'next/cache'
export default async function Page() {
cacheLife('hours')
return <div>Page</div>
}