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( ...@@ -2012,6 +2012,7 @@ export default async function build(
cacheLifeProfiles: config.cacheLife, cacheLifeProfiles: config.cacheLife,
buildId, buildId,
sriEnabled, sriEnabled,
cacheMaxMemorySize: config.cacheMaxMemorySize,
}) })
) )
...@@ -2227,7 +2228,7 @@ export default async function build( ...@@ -2227,7 +2228,7 @@ export default async function build(
isrFlushToDisk: ciEnvironment.hasNextSupport isrFlushToDisk: ciEnvironment.hasNextSupport
? false ? false
: config.experimental.isrFlushToDisk, : config.experimental.isrFlushToDisk,
maxMemoryCacheSize: config.cacheMaxMemorySize, cacheMaxMemorySize: config.cacheMaxMemorySize,
nextConfigOutput: config.output, nextConfigOutput: config.output,
pprConfig: config.experimental.ppr, pprConfig: config.experimental.ppr,
cacheLifeProfiles: config.cacheLife, cacheLifeProfiles: config.cacheLife,
......
...@@ -730,7 +730,7 @@ export async function buildAppStaticPaths({ ...@@ -730,7 +730,7 @@ export async function buildAppStaticPaths({
cacheLifeProfiles, cacheLifeProfiles,
requestHeaders, requestHeaders,
cacheHandlers, cacheHandlers,
maxMemoryCacheSize, cacheMaxMemorySize,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
nextConfigOutput, nextConfigOutput,
ComponentMod, ComponentMod,
...@@ -751,7 +751,7 @@ export async function buildAppStaticPaths({ ...@@ -751,7 +751,7 @@ export async function buildAppStaticPaths({
cacheLifeProfiles?: { cacheLifeProfiles?: {
[profile: string]: import('../../server/use-cache/cache-life').CacheLife [profile: string]: import('../../server/use-cache/cache-life').CacheLife
} }
maxMemoryCacheSize?: number cacheMaxMemorySize: number
requestHeaders: IncrementalCache['requestHeaders'] requestHeaders: IncrementalCache['requestHeaders']
nextConfigOutput: 'standalone' | 'export' | undefined nextConfigOutput: 'standalone' | 'export' | undefined
ComponentMod: AppPageModule ComponentMod: AppPageModule
...@@ -778,7 +778,7 @@ export async function buildAppStaticPaths({ ...@@ -778,7 +778,7 @@ export async function buildAppStaticPaths({
requestHeaders, requestHeaders,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
flushToDisk: isrFlushToDisk, flushToDisk: isrFlushToDisk,
cacheMaxMemorySize: maxMemoryCacheSize, cacheMaxMemorySize,
}) })
const childrenRouteParamSegments: Array<{ const childrenRouteParamSegments: Array<{
......
...@@ -27,14 +27,12 @@ import { checkIsOnDemandRevalidate } from '../../server/api-utils' ...@@ -27,14 +27,12 @@ import { checkIsOnDemandRevalidate } from '../../server/api-utils'
import { CloseController } from '../../server/web/web-on-close' import { CloseController } from '../../server/web/web-on-close'
declare const incrementalCacheHandler: any declare const incrementalCacheHandler: any
declare const nextConfig: NextConfigComplete
// OPTIONAL_IMPORT:incrementalCacheHandler // OPTIONAL_IMPORT:incrementalCacheHandler
// INJECT:nextConfig
// Initialize the cache handlers interface. // Initialize the cache handlers interface.
initializeCacheHandlers() initializeCacheHandlers(nextConfig.cacheMaxMemorySize)
// injected by the loader afterwards.
declare const nextConfig: NextConfigComplete
// INJECT:nextConfig
const maybeJSONParse = (str?: string) => (str ? JSON.parse(str) : undefined) const maybeJSONParse = (str?: string) => (str ? JSON.parse(str) : undefined)
......
...@@ -38,7 +38,7 @@ declare const user500RouteModuleOptions: any ...@@ -38,7 +38,7 @@ declare const user500RouteModuleOptions: any
// INJECT:user500RouteModuleOptions // INJECT:user500RouteModuleOptions
// Initialize the cache handlers interface. // Initialize the cache handlers interface.
initializeCacheHandlers() initializeCacheHandlers(nextConfig.cacheMaxMemorySize)
// expose this for the route-module // expose this for the route-module
;(globalThis as any).nextConfig = nextConfig ;(globalThis as any).nextConfig = nextConfig
......
...@@ -684,7 +684,7 @@ export async function isPageStatic({ ...@@ -684,7 +684,7 @@ export async function isPageStatic({
authInterrupts, authInterrupts,
originalAppPath, originalAppPath,
isrFlushToDisk, isrFlushToDisk,
maxMemoryCacheSize, cacheMaxMemorySize,
nextConfigOutput, nextConfigOutput,
cacheHandler, cacheHandler,
cacheHandlers, cacheHandlers,
...@@ -708,7 +708,7 @@ export async function isPageStatic({ ...@@ -708,7 +708,7 @@ export async function isPageStatic({
pageRuntime?: ServerRuntime pageRuntime?: ServerRuntime
originalAppPath?: string originalAppPath?: string
isrFlushToDisk?: boolean isrFlushToDisk?: boolean
maxMemoryCacheSize?: number cacheMaxMemorySize: number
cacheHandler?: string cacheHandler?: string
cacheHandlers?: Record<string, string | undefined> cacheHandlers?: Record<string, string | undefined>
cacheLifeProfiles?: { cacheLifeProfiles?: {
...@@ -740,7 +740,7 @@ export async function isPageStatic({ ...@@ -740,7 +740,7 @@ export async function isPageStatic({
distDir, distDir,
dir, dir,
flushToDisk: isrFlushToDisk, flushToDisk: isrFlushToDisk,
cacheMaxMemorySize: maxMemoryCacheSize, cacheMaxMemorySize,
}) })
const isPageStaticSpan = trace('is-page-static-utils', parentId) const isPageStaticSpan = trace('is-page-static-utils', parentId)
...@@ -866,7 +866,7 @@ export async function isPageStatic({ ...@@ -866,7 +866,7 @@ export async function isPageStatic({
distDir, distDir,
requestHeaders: {}, requestHeaders: {},
isrFlushToDisk, isrFlushToDisk,
maxMemoryCacheSize, cacheMaxMemorySize,
cacheHandler, cacheHandler,
cacheLifeProfiles, cacheLifeProfiles,
ComponentMod, ComponentMod,
......
...@@ -20,7 +20,7 @@ export async function createIncrementalCache({ ...@@ -20,7 +20,7 @@ export async function createIncrementalCache({
requestHeaders, requestHeaders,
}: { }: {
cacheHandler?: string cacheHandler?: string
cacheMaxMemorySize?: number cacheMaxMemorySize: number
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
distDir: string distDir: string
dir: string dir: string
...@@ -38,7 +38,7 @@ export async function createIncrementalCache({ ...@@ -38,7 +38,7 @@ export async function createIncrementalCache({
) )
} }
if (cacheHandlers && initializeCacheHandlers()) { if (cacheHandlers && initializeCacheHandlers(cacheMaxMemorySize)) {
for (const [kind, handler] of Object.entries(cacheHandlers)) { for (const [kind, handler] of Object.entries(cacheHandlers)) {
if (!handler) continue if (!handler) continue
......
...@@ -27,7 +27,7 @@ export interface ExportPagesInput { ...@@ -27,7 +27,7 @@ export interface ExportPagesInput {
pagesDataDir: string pagesDataDir: string
renderOpts: WorkerRenderOptsPartial renderOpts: WorkerRenderOptsPartial
nextConfig: NextConfigComplete nextConfig: NextConfigComplete
cacheMaxMemorySize: NextConfigComplete['cacheMaxMemorySize'] | undefined cacheMaxMemorySize: NextConfigComplete['cacheMaxMemorySize']
fetchCache: boolean | undefined fetchCache: boolean | undefined
cacheHandler: string | undefined cacheHandler: string | undefined
fetchCacheKeyPrefix: string | undefined fetchCacheKeyPrefix: string | undefined
......
...@@ -765,7 +765,7 @@ export default class DevServer extends Server { ...@@ -765,7 +765,7 @@ export default class DevServer extends Server {
cacheLifeProfiles: this.nextConfig.cacheLife, cacheLifeProfiles: this.nextConfig.cacheLife,
fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix, fetchCacheKeyPrefix: this.nextConfig.experimental.fetchCacheKeyPrefix,
isrFlushToDisk: this.nextConfig.experimental.isrFlushToDisk, isrFlushToDisk: this.nextConfig.experimental.isrFlushToDisk,
maxMemoryCacheSize: this.nextConfig.cacheMaxMemorySize, cacheMaxMemorySize: this.nextConfig.cacheMaxMemorySize,
nextConfigOutput: this.nextConfig.output, nextConfigOutput: this.nextConfig.output,
buildId: this.buildId, buildId: this.buildId,
authInterrupts: Boolean(this.nextConfig.experimental.authInterrupts), authInterrupts: Boolean(this.nextConfig.experimental.authInterrupts),
......
...@@ -42,7 +42,7 @@ export async function loadStaticPaths({ ...@@ -42,7 +42,7 @@ export async function loadStaticPaths({
page, page,
isrFlushToDisk, isrFlushToDisk,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
maxMemoryCacheSize, cacheMaxMemorySize,
requestHeaders, requestHeaders,
cacheHandler, cacheHandler,
cacheHandlers, cacheHandlers,
...@@ -63,7 +63,7 @@ export async function loadStaticPaths({ ...@@ -63,7 +63,7 @@ export async function loadStaticPaths({
page: string page: string
isrFlushToDisk?: boolean isrFlushToDisk?: boolean
fetchCacheKeyPrefix?: string fetchCacheKeyPrefix?: string
maxMemoryCacheSize?: number cacheMaxMemorySize: number
requestHeaders: IncrementalCache['requestHeaders'] requestHeaders: IncrementalCache['requestHeaders']
cacheHandler?: string cacheHandler?: string
cacheHandlers?: NextConfigComplete['experimental']['cacheHandlers'] cacheHandlers?: NextConfigComplete['experimental']['cacheHandlers']
...@@ -85,7 +85,7 @@ export async function loadStaticPaths({ ...@@ -85,7 +85,7 @@ export async function loadStaticPaths({
requestHeaders, requestHeaders,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
flushToDisk: isrFlushToDisk, flushToDisk: isrFlushToDisk,
cacheMaxMemorySize: maxMemoryCacheSize, cacheMaxMemorySize,
}) })
// update work memory runtime-config // update work memory runtime-config
...@@ -128,7 +128,7 @@ export async function loadStaticPaths({ ...@@ -128,7 +128,7 @@ export async function loadStaticPaths({
cacheLifeProfiles, cacheLifeProfiles,
isrFlushToDisk, isrFlushToDisk,
fetchCacheKeyPrefix, fetchCacheKeyPrefix,
maxMemoryCacheSize, cacheMaxMemorySize,
ComponentMod: components.ComponentMod, ComponentMod: components.ComponentMod,
nextConfigOutput, nextConfigOutput,
isRoutePPREnabled, isRoutePPREnabled,
......
import { createDefaultCacheHandler } from './default'
/** /**
* This is the default "use cache" handler it defaults to an in-memory store. * Used for edge runtime compatibility.
* 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 * @deprecated Use createDefaultCacheHandler instead.
* 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.
*/ */
export default createDefaultCacheHandler(50 * 1024 * 1024)
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
/**
* 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< ...@@ -459,12 +459,15 @@ export default class NextNodeServer extends BaseServer<
} }
private async loadCustomCacheHandlers() { private async loadCustomCacheHandlers() {
const { cacheHandlers } = this.nextConfig.experimental const {
cacheMaxMemorySize,
experimental: { cacheHandlers },
} = this.nextConfig
if (!cacheHandlers) return if (!cacheHandlers) return
// If we've already initialized the cache handlers interface, don't do it // If we've already initialized the cache handlers interface, don't do it
// again. // again.
if (!initializeCacheHandlers()) return if (!initializeCacheHandlers(cacheMaxMemorySize)) return
for (const [kind, handler] of Object.entries(cacheHandlers)) { for (const [kind, handler] of Object.entries(cacheHandlers)) {
if (!handler) continue if (!handler) continue
......
...@@ -381,12 +381,15 @@ export abstract class RouteModule< ...@@ -381,12 +381,15 @@ export abstract class RouteModule<
nextConfig: NextConfigComplete nextConfig: NextConfigComplete
) { ) {
if (process.env.NEXT_RUNTIME !== 'edge') { if (process.env.NEXT_RUNTIME !== 'edge') {
const { cacheHandlers } = nextConfig.experimental const {
cacheMaxMemorySize,
experimental: { cacheHandlers },
} = nextConfig
if (!cacheHandlers) return if (!cacheHandlers) return
// If we've already initialized the cache handlers interface, don't do it // If we've already initialized the cache handlers interface, don't do it
// again. // again.
if (!initializeCacheHandlers()) return if (!initializeCacheHandlers(cacheMaxMemorySize)) return
for (const [kind, handler] of Object.entries(cacheHandlers)) { for (const [kind, handler] of Object.entries(cacheHandlers)) {
if (!handler) continue 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' import type { CacheHandler } from '../lib/cache-handlers/types'
const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE const debug = process.env.NEXT_PRIVATE_DEBUG_CACHE
...@@ -27,9 +27,11 @@ const reference: typeof globalThis & { ...@@ -27,9 +27,11 @@ const reference: typeof globalThis & {
/** /**
* Initialize the cache handlers. * 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. * @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 the cache handlers have already been initialized, don't do it again.
if (reference[handlersMapSymbol]) { if (reference[handlersMapSymbol]) {
debug?.('cache handlers already initialized') debug?.('cache handlers already initialized')
...@@ -47,7 +49,7 @@ export function initializeCacheHandlers(): boolean { ...@@ -47,7 +49,7 @@ export function initializeCacheHandlers(): boolean {
fallback = reference[handlersSymbol].DefaultCache fallback = reference[handlersSymbol].DefaultCache
} else { } else {
debug?.('setting "default" cache handler from default') debug?.('setting "default" cache handler from default')
fallback = DefaultCacheHandler fallback = createDefaultCacheHandler(cacheMaxMemorySize)
} }
reference[handlersMapSymbol].set('default', fallback) reference[handlersMapSymbol].set('default', fallback)
...@@ -63,10 +65,12 @@ export function initializeCacheHandlers(): boolean { ...@@ -63,10 +65,12 @@ export function initializeCacheHandlers(): boolean {
reference[handlersMapSymbol].set('remote', fallback) reference[handlersMapSymbol].set('remote', fallback)
} }
} else { } else {
const handler = createDefaultCacheHandler(cacheMaxMemorySize)
debug?.('setting "default" cache handler from default') debug?.('setting "default" cache handler from default')
reference[handlersMapSymbol].set('default', DefaultCacheHandler) reference[handlersMapSymbol].set('default', handler)
debug?.('setting "remote" cache handler from default') debug?.('setting "remote" cache handler from default')
reference[handlersMapSymbol].set('remote', DefaultCacheHandler) reference[handlersMapSymbol].set('remote', handler)
} }
// Create a set of the cache handlers. // 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