Capítulo 202 de 456
cacheHandlers lets you plug custom storage (Redis, DynamoDB, disk) behind use cache / use cache: remote, replacing the default per-process in-memory LRU cache — needed when sharing cache across instances or persisting across restarts.
cacheHandlers.default: handler used by 'use cache'.cacheHandlers.remote: handler used by 'use cache: remote'.sessions) referenced via 'use cache: <name>'.use cache: private: not configurable via cacheHandlers.get(cacheKey, softTags), set(cacheKey, pendingEntry), refreshTags(), getExpiration(tags), updateTags(tags, durations)./blog/layout, /blog/hello), prefixed internally _N_T_, passed to get() to support revalidatePath().{ value: ReadableStream<Uint8Array>, tags: string[], stale, timestamp, expire, revalidate }.import type { NextConfig } from 'next'
const nextConfig: NextConfig = {
cacheHandlers: {
default: require.resolve('./cache-handlers/default-handler.js'),
remote: require.resolve('./cache-handlers/remote-handler.js'),
},
}
export default nextConfig
get(cacheKey: string, softTags: string[]): Promise<CacheEntry | undefined>
set(cacheKey: string, pendingEntry: Promise<CacheEntry>): Promise<void>
getExpiration(tags: string[]): Promise<number>
updateTags(tags: string[], durations?: { expire?: number }): Promise<void>
| Deployment Option | Supported |
|---|---|
| Node.js server | Yes |
| Docker container | Yes |
| Static export | No |
| Adapters | Platform-specific |
| Método | Quando é chamado |
|---|---|
get | leitura de entrada de cache |
set | armazenar entrada (pode ainda estar pendente) |
refreshTags | antes de cada request, sincroniza tags externas |
getExpiration | calcula timestamp de revalidação mais recente pra um conjunto de tags |
updateTags | chamado quando revalidateTag()/tags expiram |
set() sem await pendingEntry: a entrada pode ainda estar sendo gerada; deve-se aguardar antes de persistir.get(): exceção não tratada propaga como erro de render (framework não envolve get() em try/catch); deve retornar undefined em cache miss.cacheMaxMemorySize: 0 some junto com handler custom que gerencia própria memória.updateTags (grava invalidação), refreshTags (lê invalidações) e getExpiration (retorna timestamp mais recente).value do CacheEntry é ReadableStream; use .tee() se precisar ler e também repassar o stream.default.remote.