Skip to content

cacheHandler

如果你想要将缓存的页面和数据持久保存到持久存储,或者在 Next.js 应用的多个容器或实例之间共享缓存,你可以配置 Next.js 缓存位置。

¥You can configure the Next.js cache location if you want to persist cached pages and data to durable storage, or share the cache across multiple containers or instances of your Next.js application.

查看 自定义缓存处理程序 示例并了解更多实现信息。

¥View an example of a custom cache handler and learn more about the implementation.

API 参考

¥API Reference

缓存处理程序可以实现以下方法:getsetrevalidateTagresetRequestCache

¥The cache handler can implement the following methods: get, set, revalidateTag, and resetRequestCache.

get()

参数类型描述
keystring缓存值的键。

返回缓存的值,如果未找到则返回 null

¥Returns the cached value or null if not found.

set()

参数类型描述
keystring存储数据的键。
data数据或 null要缓存的数据。
ctx{ tags: [] }提供的缓存标签。

返回 Promise<void>

¥Returns Promise<void>.

revalidateTag()

参数类型描述
tagstringstring[]要重新验证的缓存标签。

返回 Promise<void>。了解有关 重新验证数据revalidateTag() 函数的更多信息。

¥Returns Promise<void>. Learn more about revalidating data or the revalidateTag() function.

resetRequestCache()

此方法会在下一个请求之前重置单个请求的临时内存缓存。

¥This method resets the temporary in-memory cache for a single request before the next request.

返回 void

¥Returns void.

需要了解:

¥Good to know:

  • revalidatePath 是缓存标签之上的便利层。调用 revalidatePath 将调用 revalidateTag 函数,然后你可以选择是否要根据路径标记缓存键。

    ¥revalidatePath is a convenience layer on top of cache tags. Calling revalidatePath will call your revalidateTag function, which you can then choose if you want to tag cache keys based on the path.

平台支持

¥Platform Support

部署选项支持
Node.js 服务器
Docker 容器
静态导出
适配器平台相关

了解如何在自托管 Next.js 时进行 配置 ISR

¥Learn how to configure ISR when self-hosting Next.js.

版本历史

¥Version History

版本更改
v14.1.0重命名为 cacheHandler 并变得稳定。
v13.4.0incrementalCacheHandlerPath 支持 revalidateTag
v13.4.0incrementalCacheHandlerPath 支持独立输出。
v12.2.0添加了实验性 incrementalCacheHandlerPath