Unverified Commit dbfce34e authored by Stefan Becker's avatar Stefan Becker Committed by GitHub

Add configuration for ISR Memory Cache Size (#21535) (#27337)

This allows the user to adjust the LRU cache size according to the
application need:

- optimize the cache size based on average page HTML & JSON size
- disable the memory cache by setting the size to 0

The hard-coded default of 50MB has been moved from the code to the
default configuration object.

Fixes #21535
See also #27325



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [X] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [X] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [X] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes
parent e65c56e7
...@@ -101,6 +101,7 @@ export type NextConfig = { [key: string]: any } & { ...@@ -101,6 +101,7 @@ export type NextConfig = { [key: string]: any } & {
esmExternals?: boolean | 'loose' esmExternals?: boolean | 'loose'
staticPageGenerationTimeout?: number staticPageGenerationTimeout?: number
pageDataCollectionTimeout?: number pageDataCollectionTimeout?: number
isrMemoryCacheSize?: number
} }
} }
...@@ -166,6 +167,8 @@ export const defaultConfig: NextConfig = { ...@@ -166,6 +167,8 @@ export const defaultConfig: NextConfig = {
esmExternals: false, esmExternals: false,
staticPageGenerationTimeout: 60, staticPageGenerationTimeout: 60,
pageDataCollectionTimeout: 60, pageDataCollectionTimeout: 60,
// default to 50MB limit
isrMemoryCacheSize: 50 * 1024 * 1024,
}, },
future: { future: {
strictPostcssConfiguration: false, strictPostcssConfiguration: false,
......
...@@ -39,7 +39,7 @@ export class IncrementalCache { ...@@ -39,7 +39,7 @@ export class IncrementalCache {
} }
prerenderManifest: PrerenderManifest prerenderManifest: PrerenderManifest
cache: LRUCache<string, IncrementalCacheEntry> cache?: LRUCache<string, IncrementalCacheEntry>
locales?: string[] locales?: string[]
constructor({ constructor({
...@@ -85,9 +85,9 @@ export class IncrementalCache { ...@@ -85,9 +85,9 @@ export class IncrementalCache {
max = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10) max = parseInt(process.env.__NEXT_TEST_MAX_ISR_CACHE, 10)
} }
if (max) {
this.cache = new LRUCache({ this.cache = new LRUCache({
// default to 50MB limit max,
max: max || 50 * 1024 * 1024,
length({ value }) { length({ value }) {
if (!value || value.kind === 'REDIRECT') return 25 if (!value || value.kind === 'REDIRECT') return 25
// rough estimate of size of cache value // rough estimate of size of cache value
...@@ -95,6 +95,7 @@ export class IncrementalCache { ...@@ -95,6 +95,7 @@ export class IncrementalCache {
}, },
}) })
} }
}
private getSeedPath(pathname: string, ext: string): string { private getSeedPath(pathname: string, ext: string): string {
return path.join(this.incrementalOptions.pagesDir!, `${pathname}.${ext}`) return path.join(this.incrementalOptions.pagesDir!, `${pathname}.${ext}`)
...@@ -133,7 +134,7 @@ export class IncrementalCache { ...@@ -133,7 +134,7 @@ export class IncrementalCache {
if (this.incrementalOptions.dev) return null if (this.incrementalOptions.dev) return null
pathname = normalizePagePath(pathname) pathname = normalizePagePath(pathname)
let data = this.cache.get(pathname) let data = this.cache && this.cache.get(pathname)
// let's check the disk for seed data // let's check the disk for seed data
if (!data) { if (!data) {
...@@ -157,7 +158,9 @@ export class IncrementalCache { ...@@ -157,7 +158,9 @@ export class IncrementalCache {
pageData, pageData,
}, },
} }
if (this.cache) {
this.cache.set(pathname, data) this.cache.set(pathname, data)
}
} catch (_) { } catch (_) {
// unable to get data from disk // unable to get data from disk
} }
...@@ -204,10 +207,15 @@ export class IncrementalCache { ...@@ -204,10 +207,15 @@ export class IncrementalCache {
} }
pathname = normalizePagePath(pathname) pathname = normalizePagePath(pathname)
if (this.cache) {
this.cache.set(pathname, { this.cache.set(pathname, {
revalidateAfter: this.calculateRevalidate(pathname, new Date().getTime()), revalidateAfter: this.calculateRevalidate(
pathname,
new Date().getTime()
),
value: data, value: data,
}) })
}
// TODO: This option needs to cease to exist unless it stops mutating the // TODO: This option needs to cease to exist unless it stops mutating the
// `next build` output's manifest. // `next build` output's manifest.
......
...@@ -276,6 +276,7 @@ export default class Server { ...@@ -276,6 +276,7 @@ export default class Server {
'pages' 'pages'
), ),
locales: this.nextConfig.i18n?.locales, locales: this.nextConfig.i18n?.locales,
max: this.nextConfig.experimental.isrMemoryCacheSize,
flushToDisk: !minimalMode && this.nextConfig.experimental.sprFlushToDisk, flushToDisk: !minimalMode && this.nextConfig.experimental.sprFlushToDisk,
}) })
this.responseCache = new ResponseCache(this.incrementalCache) this.responseCache = new ResponseCache(this.incrementalCache)
......
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