Skip to content

Commit

Permalink
Add configuration for LRU Cache Size (vercel#21535)
Browse files Browse the repository at this point in the history
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
- remove the cache limit and let it grow to cache all pages
- make it so small to practically disable it

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

Fixes vercel#21535
See also vercel#27325
  • Loading branch information
stefanb2 committed Jul 20, 2021
1 parent 0472f0f commit a83e419
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
30 changes: 30 additions & 0 deletions docs/api-reference/next.config.js/lrucachesize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
description: Control the size of the LRU cache in Next.js
---

# LRU Cache Size

<details>
<summary><b>Version History</b></summary>

| Version | Changes |
| --------- | --------------------- |
| `v11.0.2` | LRU Cache Size added. |

</details>

Next.js keeps the data for frequently accessed paths in a least recently used (LRU) cache in memory.
`lruCacheSize` allows you to adjust the cache size for the application.

Setting it to 0 will remove the size restriction and the cache will only grow.

Setting it to 1 will basically disable the cache as it will only ever contain the data for the last
accessed path.

The default is 50MB:

```js
module.exports = {
lruCacheSize: 50 * 1024 * 1024,
}
```
2 changes: 2 additions & 0 deletions packages/next/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export const defaultConfig: NextConfig = {
serverRuntimeConfig: {},
publicRuntimeConfig: {},
reactStrictMode: false,
// default to 50MB limit
lruCacheSize: 50 * 1024 * 1024,
experimental: {
cpus: Math.max(
1,
Expand Down
5 changes: 2 additions & 3 deletions packages/next/server/incremental-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class IncrementalCache {
locales,
}: {
dev: boolean
max?: number
max: number
distDir: string
pagesDir: string
flushToDisk?: boolean
Expand Down Expand Up @@ -81,8 +81,7 @@ export class IncrementalCache {
}

this.cache = new LRUCache({
// default to 50MB limit
max: max || 50 * 1024 * 1024,
max,
length({ value }) {
if (!value || value.kind === 'REDIRECT') return 25
// rough estimate of size of cache value
Expand Down
1 change: 1 addition & 0 deletions packages/next/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ export default class Server {
'pages'
),
locales: this.nextConfig.i18n?.locales,
max: this.nextConfig.lruCacheSize,
flushToDisk: !minimalMode && this.nextConfig.experimental.sprFlushToDisk,
})
this.responseCache = new ResponseCache(this.incrementalCache)
Expand Down

0 comments on commit a83e419

Please sign in to comment.