Unverified Commit d58f7aa0 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Use cacheMaxMemorySize config in default cache handler (#85153)

### What?

This refactoring enables the `cacheMaxMemorySize` configuration option
to control the memory limits of the default "use cache" handler.

### Why?

Previously, the default cache handler was hardcoded to use 50MB of
memory, ignoring the user's `cacheMaxMemorySize` configuration. This
meant users couldn't:
- Disable internal caching when using external CDNs (by setting size to
0)
- Customize memory limits based on their deployment environment
- Control memory usage for the "use cache" handler

### How?

- Extracted cache handler creation into `createDefaultCacheHandler()`
factory function
- Threaded `cacheMaxMemorySize` through `initializeCacheHandlers()`
calls
- Harmonized parameter naming (`maxMemoryCacheSize` →
`cacheMaxMemorySize`) for consistency with the config
- Made `cacheMaxMemorySize` required in type signatures
- Added zero-size optimization (returns no-op handler when size=0)
parent 1577ba21
......@@ -2012,6 +2012,7 @@ export default async function build(
cacheLifeProfiles: config.cacheLife,
buildId,
sriEnabled,
cacheMaxMemorySize: config.cacheMaxMemorySize,
})
)
......@@ -2227,7 +2228,7 @@ export default async function build(
isrFlushToDisk: ciEnvironment.hasNextSupport
? false
: config.experimental.isrFlushToDisk,
maxMemoryCacheSize: config.cacheMaxMemorySize,
cacheMaxMemorySize: config.cacheMaxMemorySize,
nextConfigOutput: config.output,
pprConfig: config.experimental.ppr,
cacheLifeProfiles: config.cacheLife,
......
......@@ -730,7 +730,7 @@ export async function buildAppStaticPaths({
cacheLifeProfiles,
requestHeaders,
cacheHandlers,
maxMemoryCacheSize,
cacheMaxMemorySize,
fetchCacheKeyPrefix,
nextConfigOutput,
ComponentMod,
......@@ -751,7 +751,7 @@ export async function buildAppStaticPaths({
cacheLifeProfiles?: {
[profile: string]: import('../../server/use-cache/cache-life').CacheLife
}
maxMemoryCacheSize?: number
cacheMaxMemorySize: number
requestHeaders: IncrementalCache['requestHeaders']
nextConfigOutput: 'standalone' | 'export' | undefined
ComponentMod: AppPageModule
......@@ -778,7 +778,7 @@ export async function buildAppStaticPaths({
requestHeaders,
fetchCacheKeyPrefix,
flushToDisk: isrFlushToDisk,
cacheMaxMemorySize: maxMemoryCacheSize,
cacheMaxMemorySize,
})
const childrenRouteParamSegments: Array<{
......
......@@ -27,14 +27,12 @@ import { checkIsOnDemandRevalidate } from '../../server/api-utils'
import { CloseController } from '../../server/web/web-on-close'
declare const incrementalCacheHandler: any
declare const nextConfig: NextConfigComplete
// OPTIONAL_IMPORT:incrementalCacheHandler
// INJECT:nextConfig
// Initialize the cache handlers interface.
initializeCacheHandlers()
// injected by the loader afterwards.
declare const nextConfig: NextConfigComplete
// INJECT:nextConfig
initializeCacheHandlers(nextConfig.cacheMaxMemorySize)
const maybeJSONParse = (str?: string) => (str ? JSON.parse(str) : undefined)
......
......@@ -38,7 +38,7 @@ declare const user500RouteModuleOptions: any
// INJECT:user500RouteModuleOptions
// Initialize the cache handlers interface.
initializeCacheHandlers()
initializeCacheHandlers(nextConfig.cacheMaxMemorySize)
// expose this for the route-module
;(globalThis as any).nextConfig = nextConfig
......
......@@ -684,7 +684,7 @@ export async function isPageStatic({
authInterrupts,
originalAppPath,
isrFlushToDisk,
maxMemoryCacheSize,
cacheMaxMemorySize,
nextConfigOutput,
cacheHandler,
cacheHandlers,
......@@ -708,7 +708,7 @@ export async function isPageStatic({
pageRuntime?: ServerRuntime
originalAppPath?: string
isrFlushToDisk?: boolean
maxMemoryCacheSize?: number
cacheMaxMemorySize: number
cacheHandler?: string
cacheHandlers?: Record<string, string | undefined>
cacheLifeProfiles?: {
......@@ -740,7 +740,7 @@ export async function isPageStatic({
distDir,
dir,
flushToDisk: isrFlushToDisk,
cacheMaxMemorySize: maxMemoryCacheSize,
cacheMaxMemorySize,
})
const isPageStaticSpan = trace('is-page-static-utils', parentId)
......@@ -866,7 +866,7 @@ export async function isPageStatic({
distDir,
requestHeaders: {},
isrFlushToDisk,
maxMemoryCacheSize,
cacheMaxMemorySize,
cacheHandler,
cacheLifeProfiles,
ComponentMod,
......
......@@ -20,7 +20,7 @@ export async function createIncrementalCache({
requestHeaders,
}: {
cacheHandler?: string
cacheMaxMemorySize?: number
cacheMaxMemorySize: number
fetchCacheKeyPrefix?: string
distDir: string
dir: string
......@@ -38,7 +38,7 @@ export async function createIncrementalCache({
)
}
if (cacheHandlers && initializeCacheHandlers()) {
if (cacheHandlers && initializeCacheHandlers(cacheMaxMemorySize)) {
for (const [kind, handler] of Object.entries(cacheHandlers)) {
if (!handler) continue
......
......@@ -27,7 +27,7 @@ export interface ExportPagesInput {
pagesDataDir: string
renderOpts: WorkerRenderOptsPartial
nextConfig: NextConfigComplete
cacheMaxMemorySize: NextConfigComplete['cacheMaxMemorySize'] | undefined
cacheMaxMemorySize: NextConfigComplete['cacheMaxMemorySize']
fetchCache: boolean | undefined
cacheHandler: string | undefined
fetchCacheKeyPrefix: string | undefined
......
......@@ -765,7 +765,7 @@ export default class DevServer extends Server {
cacheLifeProfiles: this.nextConfig.cacheLife,
fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix,
isrFlushToDisk: this.nextConfig.experimental.isrFlushToDisk,
maxMemoryCacheSize: this.nextConfig.cacheMaxMemorySize,
cacheMaxMemorySize: this.nextConfig.cacheMaxMemorySize,
nextConfigOutput: this.nextConfig.output,
buildId: this.buildId,
authInterrupts: Boolean(this.nextConfig.experimental.authInterrupts),
......
......@@ -42,7 +42,7 @@ export async function loadStaticPaths({
page,
isrFlushToDisk,
fetchCacheKeyPrefix,
maxMemoryCacheSize,
cacheMaxMemorySize,
requestHeaders,
cacheHandler,
cacheHandlers,
......@@ -63,7 +63,7 @@ export async function loadStaticPaths({
page: string
isrFlushToDisk?: boolean
fetchCacheKeyPrefix?: string
maxMemoryCacheSize?: number
cacheMaxMemorySize: number
requestHeaders: IncrementalCache['requestHeaders']
cacheHandler?: string
cacheHandlers?: NextConfigComplete['experimental']['cacheHandlers']
......@@ -85,7 +85,7 @@ export async function loadStaticPaths({
requestHeaders,
fetchCacheKeyPrefix,
flushToDisk: isrFlushToDisk,
cacheMaxMemorySize: maxMemoryCacheSize,
cacheMaxMemorySize,
})
// update work memory runtime-config
......@@ -128,7 +128,7 @@ export async function loadStaticPaths({
cacheLifeProfiles,
isrFlushToDisk,
fetchCacheKeyPrefix,
maxMemoryCacheSize,
cacheMaxMemorySize,
ComponentMod: components.ComponentMod,
nextConfigOutput,
isRoutePPREnabled,
......
import { createDefaultCacheHandler } from './default'
/**
* This is the default "use cache" handler it defaults to an in-memory store.
* In-memory caches are fragile and should not use stale-while-revalidate
* semantics on the caches because it's not worth warming up an entry that's
* likely going to get evicted before we get to use it anyway. However, we also
* don't want to reuse a stale entry for too long so stale entries should be
* considered expired/missing in such cache handlers.
* Used for edge runtime compatibility.
*
* @deprecated Use createDefaultCacheHandler instead.
*/
import { LRUCache } from '../lru-cache'
import type { CacheEntry, CacheHandler } from './types'
import {
areTagsExpired,
areTagsStale,
tagsManifest,
type TagManifestEntry,
} from '../incremental-cache/tags-manifest.external'
type PrivateCacheEntry = {
entry: CacheEntry
// For the default cache we store errored cache
// entries and allow them to be used up to 3 times
// after that we want to dispose it and try for fresh
// If an entry is errored we return no entry
// three times so that we retry hitting origin (MISS)
// and then if it still fails to set after the third we
// return the errored content and use expiration of
// Math.min(30, entry.expiration)
isErrored: boolean
errorRetryCount: number
// compute size on set since we need to read size
// of the ReadableStream for LRU evicting
size: number
}
// LRU cache default to max 50 MB but in future track
const memoryCache = new LRUCache<PrivateCacheEntry>(
50 * 1024 * 1024,
(entry) => entry.size
)
const pendingSets = new Map<string, Promise<void>>()
const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE
? console.debug.bind(console, 'DefaultCacheHandler:')
: undefined
const DefaultCacheHandler: CacheHandler = {
async get(cacheKey) {
const pendingPromise = pendingSets.get(cacheKey)
if (pendingPromise) {
debug?.('get', cacheKey, 'pending')
await pendingPromise
}
const privateEntry = memoryCache.get(cacheKey)
if (!privateEntry) {
debug?.('get', cacheKey, 'not found')
return undefined
}
const entry = privateEntry.entry
if (
performance.timeOrigin + performance.now() >
entry.timestamp + entry.revalidate * 1000
) {
// In-memory caches should expire after revalidate time because it is
// unlikely that a new entry will be able to be used before it is dropped
// from the cache.
debug?.('get', cacheKey, 'expired')
return undefined
}
let revalidate = entry.revalidate
if (areTagsExpired(entry.tags, entry.timestamp)) {
debug?.('get', cacheKey, 'had expired tag')
return undefined
}
if (areTagsStale(entry.tags, entry.timestamp)) {
debug?.('get', cacheKey, 'had stale tag')
revalidate = -1
}
const [returnStream, newSaved] = entry.value.tee()
entry.value = newSaved
debug?.('get', cacheKey, 'found', {
tags: entry.tags,
timestamp: entry.timestamp,
expire: entry.expire,
revalidate,
})
return {
...entry,
revalidate,
value: returnStream,
}
},
async set(cacheKey, pendingEntry) {
debug?.('set', cacheKey, 'start')
let resolvePending: () => void = () => {}
const pendingPromise = new Promise<void>((resolve) => {
resolvePending = resolve
})
pendingSets.set(cacheKey, pendingPromise)
const entry = await pendingEntry
let size = 0
try {
const [value, clonedValue] = entry.value.tee()
entry.value = value
const reader = clonedValue.getReader()
for (let chunk; !(chunk = await reader.read()).done; ) {
size += Buffer.from(chunk.value).byteLength
}
memoryCache.set(cacheKey, {
entry,
isErrored: false,
errorRetryCount: 0,
size,
})
debug?.('set', cacheKey, 'done')
} catch (err) {
// TODO: store partial buffer with error after we retry 3 times
debug?.('set', cacheKey, 'failed', err)
} finally {
resolvePending()
pendingSets.delete(cacheKey)
}
},
async refreshTags() {
// Nothing to do for an in-memory cache handler.
},
async getExpiration(tags) {
const expirations = tags.map((tag) => {
const entry = tagsManifest.get(tag)
if (!entry) return 0
// Return the most recent timestamp (either expired or stale)
return entry.expired || 0
})
const expiration = Math.max(...expirations, 0)
debug?.('getExpiration', { tags, expiration })
return expiration
},
async updateTags(tags, durations) {
const now = Math.round(performance.timeOrigin + performance.now())
debug?.('updateTags', { tags, timestamp: now })
for (const tag of tags) {
// TODO: update file-system-cache?
const existingEntry = tagsManifest.get(tag) || {}
if (durations) {
// Use provided durations directly
const updates: TagManifestEntry = { ...existingEntry }
// mark as stale immediately
updates.stale = now
if (durations.expire !== undefined) {
updates.expired = now + durations.expire * 1000 // Convert seconds to ms
}
tagsManifest.set(tag, updates)
} else {
// Update expired field for immediate expiration (default behavior when no durations provided)
tagsManifest.set(tag, { ...existingEntry, expired: now })
}
}
},
}
export default DefaultCacheHandler
export default createDefaultCacheHandler(50 * 1024 * 1024)
/**
* This is the default "use cache" handler it defaults to an in-memory store.
* In-memory caches are fragile and should not use stale-while-revalidate
* semantics on the caches because it's not worth warming up an entry that's
* likely going to get evicted before we get to use it anyway. However, we also
* don't want to reuse a stale entry for too long so stale entries should be
* considered expired/missing in such cache handlers.
*/
import { LRUCache } from '../lru-cache'
import type { CacheEntry, CacheHandler } from './types'
import {
areTagsExpired,
areTagsStale,
tagsManifest,
type TagManifestEntry,
} from '../incremental-cache/tags-manifest.external'
type PrivateCacheEntry = {
entry: CacheEntry
// For the default cache we store errored cache
// entries and allow them to be used up to 3 times
// after that we want to dispose it and try for fresh
// If an entry is errored we return no entry
// three times so that we retry hitting origin (MISS)
// and then if it still fails to set after the third we
// return the errored content and use expiration of
// Math.min(30, entry.expiration)
isErrored: boolean
errorRetryCount: number
// compute size on set since we need to read size
// of the ReadableStream for LRU evicting
size: number
}
export function createDefaultCacheHandler(maxSize: number): CacheHandler {
// If the max size is 0, return a cache handler that doesn't cache anything,
// this avoids an unnecessary LRUCache instance and potential memory
// allocation.
if (maxSize === 0) {
return {
get: () => Promise.resolve(undefined),
set: () => Promise.resolve(),
refreshTags: () => Promise.resolve(),
getExpiration: () => Promise.resolve(0),
updateTags: () => Promise.resolve(),
}
}
const memoryCache = new LRUCache<PrivateCacheEntry>(
maxSize,
(entry) => entry.size
)
const pendingSets = new Map<string, Promise<void>>()
const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE
? console.debug.bind(console, 'DefaultCacheHandler:')
: undefined
return {
async get(cacheKey) {
const pendingPromise = pendingSets.get(cacheKey)
if (pendingPromise) {
debug?.('get', cacheKey, 'pending')
await pendingPromise
}
const privateEntry = memoryCache.get(cacheKey)
if (!privateEntry) {
debug?.('get', cacheKey, 'not found')
return undefined
}
const entry = privateEntry.entry
if (
performance.timeOrigin + performance.now() >
entry.timestamp + entry.revalidate * 1000
) {
// In-memory caches should expire after revalidate time because it is
// unlikely that a new entry will be able to be used before it is dropped
// from the cache.
debug?.('get', cacheKey, 'expired')
return undefined
}
let revalidate = entry.revalidate
if (areTagsExpired(entry.tags, entry.timestamp)) {
debug?.('get', cacheKey, 'had expired tag')
return undefined
}
if (areTagsStale(entry.tags, entry.timestamp)) {
debug?.('get', cacheKey, 'had stale tag')
revalidate = -1
}
const [returnStream, newSaved] = entry.value.tee()
entry.value = newSaved
debug?.('get', cacheKey, 'found', {
tags: entry.tags,
timestamp: entry.timestamp,
expire: entry.expire,
revalidate,
})
return {
...entry,
revalidate,
value: returnStream,
}
},
async set(cacheKey, pendingEntry) {
debug?.('set', cacheKey, 'start')
let resolvePending: () => void = () => {}
const pendingPromise = new Promise<void>((resolve) => {
resolvePending = resolve
})
pendingSets.set(cacheKey, pendingPromise)
const entry = await pendingEntry
let size = 0
try {
const [value, clonedValue] = entry.value.tee()
entry.value = value
const reader = clonedValue.getReader()
for (let chunk; !(chunk = await reader.read()).done; ) {
size += Buffer.from(chunk.value).byteLength
}
memoryCache.set(cacheKey, {
entry,
isErrored: false,
errorRetryCount: 0,
size,
})
debug?.('set', cacheKey, 'done')
} catch (err) {
// TODO: store partial buffer with error after we retry 3 times
debug?.('set', cacheKey, 'failed', err)
} finally {
resolvePending()
pendingSets.delete(cacheKey)
}
},
async refreshTags() {
// Nothing to do for an in-memory cache handler.
},
async getExpiration(tags) {
const expirations = tags.map((tag) => {
const entry = tagsManifest.get(tag)
if (!entry) return 0
// Return the most recent timestamp (either expired or stale)
return entry.expired || 0
})
const expiration = Math.max(...expirations, 0)
debug?.('getExpiration', { tags, expiration })
return expiration
},
async updateTags(tags, durations) {
const now = Math.round(performance.timeOrigin + performance.now())
debug?.('updateTags', { tags, timestamp: now })
for (const tag of tags) {
// TODO: update file-system-cache?
const existingEntry = tagsManifest.get(tag) || {}
if (durations) {
// Use provided durations directly
const updates: TagManifestEntry = { ...existingEntry }
// mark as stale immediately
updates.stale = now
if (durations.expire !== undefined) {
updates.expired = now + durations.expire * 1000 // Convert seconds to ms
}
tagsManifest.set(tag, updates)
} else {
// Update expired field for immediate expiration (default behavior when no durations provided)
tagsManifest.set(tag, { ...existingEntry, expired: now })
}
}
},
}
}
......@@ -459,12 +459,15 @@ export default class NextNodeServer extends BaseServer<
}
private async loadCustomCacheHandlers() {
const { cacheHandlers } = this.nextConfig.experimental
const {
cacheMaxMemorySize,
experimental: { cacheHandlers },
} = this.nextConfig
if (!cacheHandlers) return
// If we've already initialized the cache handlers interface, don't do it
// again.
if (!initializeCacheHandlers()) return
if (!initializeCacheHandlers(cacheMaxMemorySize)) return
for (const [kind, handler] of Object.entries(cacheHandlers)) {
if (!handler) continue
......
......@@ -381,12 +381,15 @@ export abstract class RouteModule<
nextConfig: NextConfigComplete
) {
if (process.env.NEXT_RUNTIME !== 'edge') {
const { cacheHandlers } = nextConfig.experimental
const {
cacheMaxMemorySize,
experimental: { cacheHandlers },
} = nextConfig
if (!cacheHandlers) return
// If we've already initialized the cache handlers interface, don't do it
// again.
if (!initializeCacheHandlers()) return
if (!initializeCacheHandlers(cacheMaxMemorySize)) return
for (const [kind, handler] of Object.entries(cacheHandlers)) {
if (!handler) continue
......
import DefaultCacheHandler from '../lib/cache-handlers/default.external'
import { createDefaultCacheHandler } from '../lib/cache-handlers/default'
import type { CacheHandler } from '../lib/cache-handlers/types'
const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE
......@@ -27,9 +27,11 @@ const reference: typeof globalThis & {
/**
* Initialize the cache handlers.
* @param cacheMaxMemorySize - The maximum memory size of the cache in bytes, if
* not provided, the default memory size will be used.
* @returns `true` if the cache handlers were initialized, `false` if they were already initialized.
*/
export function initializeCacheHandlers(): boolean {
export function initializeCacheHandlers(cacheMaxMemorySize: number): boolean {
// If the cache handlers have already been initialized, don't do it again.
if (reference[handlersMapSymbol]) {
debug?.('cache handlers already initialized')
......@@ -47,7 +49,7 @@ export function initializeCacheHandlers(): boolean {
fallback = reference[handlersSymbol].DefaultCache
} else {
debug?.('setting "default" cache handler from default')
fallback = DefaultCacheHandler
fallback = createDefaultCacheHandler(cacheMaxMemorySize)
}
reference[handlersMapSymbol].set('default', fallback)
......@@ -63,10 +65,12 @@ export function initializeCacheHandlers(): boolean {
reference[handlersMapSymbol].set('remote', fallback)
}
} else {
const handler = createDefaultCacheHandler(cacheMaxMemorySize)
debug?.('setting "default" cache handler from default')
reference[handlersMapSymbol].set('default', DefaultCacheHandler)
reference[handlersMapSymbol].set('default', handler)
debug?.('setting "remote" cache handler from default')
reference[handlersMapSymbol].set('remote', DefaultCacheHandler)
reference[handlersMapSymbol].set('remote', handler)
}
// Create a set of the cache handlers.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment