cacheTag
了解如何使用 cacheTag 函数在 Next.js 应用中管理缓存失效。
cacheTag 函数允许你为缓存数据打标签,以便按需失效。通过将标签与缓存条目关联,你可以有选择地清除或重新验证特定的缓存条目,而不影响其他缓存数据。
用法
要使用 cacheTag,请在 next.config.js 文件中启用 cacheComponents 标志:
TypeScript
next.config.tsts
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheComponents: true,
}
export default nextConfig
JavaScript
next.config.jsjs
const nextConfig = {
cacheComponents: true,
}
module.exports = nextConfig
cacheTag 函数接受一个或多个字符串值。
TypeScript
app/data.tstsx
import { cacheTag } from 'next/cache'
export async function getData() {
'use cache'
cacheTag('my-data')
const res = await fetch('https://api.example.com/data')
const data = await res.json()
return data
}
JavaScript
app/data.jsjsx
import { cacheTag } from 'next/cache'
export async function getData() {
'use cache'
cacheTag('my-data')
const res = await fetch('https://api.example.com/data')
const data = await res.json()
return data
}
- 对于读己所写场景,例如表单和其他由用户触发的变更——用户做出更改后,下一次读取应立即获取最新数据——请使用
updateTag。updateTag仅在服务器函数内部可用。 - 当可以接受在后台进行重新验证期间提供过期数据,或需要从路由处理器或其他上下文执行重新验证时,请使用
revalidateTag。
例如,这个服务器函数会添加一篇帖子,然后清除所有标记为 'my-data' 的缓存条目,使下一次读取能反映该更改:
TypeScript
app/action.tstsx
'use server'
import { updateTag } from 'next/cache'
export default async function submit() {
await addPost()
updateTag('my-data')
}
JavaScript
app/action.jsjsx
'use server'
import { updateTag } from 'next/cache'
export default async function submit() {
await addPost()
updateTag('my-data')
}
对于多服务器实例的部署,请参阅分布式标签协调。
须知
- 幂等标签:多次应用同一标签不会产生额外效果。
- 多个标签:通过向
cacheTag传入多个字符串值,可以为单个缓存条目分配多个标签。
cacheTag('tag-one', 'tag-two')
- 限制:单次
cacheTag()调用最多接受 128 个标签,每个标签的最大长度为 256 个字符。超过 256 个字符的标签会被跳过,单次调用中第 128 个之后的标签会被丢弃。这两种情况都会在控制台输出警告。
示例
为组件或函数打标签
在缓存函数或组件中调用 cacheTag,为缓存数据打标签:
TypeScript
app/components/bookings.tsxtsx
import { cacheTag } from 'next/cache'
interface BookingsProps {
type: string
}
export async function Bookings({ type = 'haircut' }: BookingsProps) {
'use cache'
cacheTag('bookings-data')
async function getBookingsData() {
const response = await fetch(
`https://api.example.com/bookings?type=${encodeURIComponent(type)}`
)
const data = await response.json()
return data
}
return //...
}
JavaScript
app/components/bookings.jsjsx
import { cacheTag } from 'next/cache'
export async function Bookings({ type = 'haircut' }) {
'use cache'
cacheTag('bookings-data')
async function getBookingsData() {
const response = await fetch(
`https://api.example.com/bookings?type=${encodeURIComponent(type)}`
)
const data = await response.json()
return data
}
return //...
}
从外部数据创建标签
你可以使用异步函数返回的数据来为缓存条目打上标签。
TypeScript
app/components/bookings.tsxtsx
import { cacheTag } from 'next/cache'
interface BookingsProps {
type: string
}
export async function Bookings({ type = 'haircut' }: BookingsProps) {
async function getBookingsData() {
'use cache'
const response = await fetch(
`https://api.example.com/bookings?type=${encodeURIComponent(type)}`
)
const data = await response.json()
cacheTag('bookings-data', data.id)
return data
}
return //...
}
JavaScript
app/components/bookings.jsjsx
import { cacheTag } from 'next/cache'
export async function Bookings({ type = 'haircut' }) {
async function getBookingsData() {
'use cache'
const response = await fetch(
`https://api.example.com/bookings?type=${encodeURIComponent(type)}`
)
const data = await response.json()
cacheTag('bookings-data', data.id)
return data
}
return //...
}
使带标签的缓存失效
使用 revalidateTag时,你可以在需要时使特定标签的缓存失效:
TypeScript
app/actions.tstsx
'use server'
import { revalidateTag } from 'next/cache'
export async function updateBookings() {
await updateBookingData()
revalidateTag('bookings-data', 'max')
}
JavaScript
app/actions.jsjsx
'use server'
import { revalidateTag } from 'next/cache'
export async function updateBookings() {
await updateBookingData()
revalidateTag('bookings-data', 'max')
}
版本历史
| 版本 | 变更 |
|---|---|
v16.0.0 |
cacheTag 已稳定。unstable_cacheTag 已弃用。 |
请参阅 Next.js 16 升级指南,将导入从 unstable_cacheTag 更新为 cacheTag。
相关
查看相关 API 参考。
- cacheComponents:了解如何在 Next.js 中启用 cacheComponents 标志。
- use cache:了解如何使用 "use cache" 指令在 Next.js 应用中缓存数据。
- revalidateTag:revalidateTag 函数的 API 参考。
- updateTag:updateTag 函数的 API 参考。
- cacheLife:了解如何使用 cacheLife 函数为缓存函数或组件设置缓存过期时间。