incrementalCacheHandlerPath

在 Next.js 中,默认缓存处理器使用文件系统缓存。这无需任何配置,但您可以通过在 next.config.js 中使用 incrementalCacheHandlerPath 字段来自定义缓存处理器。

next.config.js
module.exports = {
  experimental: {
    incrementalCacheHandlerPath: require.resolve('./cache-handler.js'),
  },
}

以下是一个自定义缓存处理器的示例:

cache-handler.js
const cache = new Map()

module.exports = class CacheHandler {
  constructor(options) {
    this.options = options
    this.cache = {}
  }

  async get(key) {
    return cache.get(key)
  }

  async set(key, data) {
    cache.set(key, {
      value: data,
      lastModified: Date.now(),
    })
  }
}

API 参考

缓存处理器可以实现以下方法:getsetrevalidateTag

get()

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

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

set()

参数类型描述
keystring存储数据所用的键名。
dataData 或 null要缓存的数据。

返回 Promise<void>

revalidateTag()

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

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

On this page