1
1
import type { RouteMetadata } from '../../../export/routes/types'
2
- import type { CacheHandler , CacheHandlerContext , CacheHandlerValue } from './ '
2
+ import type { CacheHandler , CacheHandlerContext , CacheHandlerValue } from '.'
3
3
import type { CacheFs } from '../../../shared/lib/utils'
4
4
import {
5
5
CachedRouteKind ,
@@ -10,7 +10,7 @@ import {
10
10
type SetIncrementalResponseCacheContext ,
11
11
} from '../../response-cache'
12
12
13
- import { LRUCache } from '../lru-cache'
13
+ import type { LRUCache } from '../lru-cache'
14
14
import path from '../../../shared/lib/isomorphic/path'
15
15
import {
16
16
NEXT_CACHE_TAGS_HEADER ,
@@ -23,6 +23,7 @@ import {
23
23
} from '../../../lib/constants'
24
24
import { isStale , tagsManifest } from './tags-manifest.external'
25
25
import { MultiFileWriter } from '../../../lib/multi-file-writer'
26
+ import { getMemoryCache } from './memory-cache.external'
26
27
27
28
type FileSystemCacheContext = Omit <
28
29
CacheHandlerContext ,
@@ -32,54 +33,31 @@ type FileSystemCacheContext = Omit<
32
33
serverDistDir : string
33
34
}
34
35
35
- let memoryCache : LRUCache < CacheHandlerValue > | undefined
36
-
37
36
export default class FileSystemCache implements CacheHandler {
38
37
private fs : FileSystemCacheContext [ 'fs' ]
39
38
private flushToDisk ?: FileSystemCacheContext [ 'flushToDisk' ]
40
39
private serverDistDir : FileSystemCacheContext [ 'serverDistDir' ]
41
40
private revalidatedTags : string [ ]
42
- private debug : boolean
41
+ private static debug : boolean = ! ! process . env . NEXT_PRIVATE_DEBUG_CACHE
42
+ private static memoryCache : LRUCache < CacheHandlerValue > | undefined
43
43
44
44
constructor ( ctx : FileSystemCacheContext ) {
45
45
this . fs = ctx . fs
46
46
this . flushToDisk = ctx . flushToDisk
47
47
this . serverDistDir = ctx . serverDistDir
48
48
this . revalidatedTags = ctx . revalidatedTags
49
- this . debug = ! ! process . env . NEXT_PRIVATE_DEBUG_CACHE
50
49
51
50
if ( ctx . maxMemoryCacheSize ) {
52
- if ( ! memoryCache ) {
53
- if ( this . debug ) {
51
+ if ( ! FileSystemCache . memoryCache ) {
52
+ if ( FileSystemCache . debug ) {
54
53
console . log ( 'using memory store for fetch cache' )
55
54
}
56
55
57
- memoryCache = new LRUCache ( ctx . maxMemoryCacheSize , function length ( {
58
- value,
59
- } ) {
60
- if ( ! value ) {
61
- return 25
62
- } else if ( value . kind === CachedRouteKind . REDIRECT ) {
63
- return JSON . stringify ( value . props ) . length
64
- } else if ( value . kind === CachedRouteKind . IMAGE ) {
65
- throw new Error ( 'invariant image should not be incremental-cache' )
66
- } else if ( value . kind === CachedRouteKind . FETCH ) {
67
- return JSON . stringify ( value . data || '' ) . length
68
- } else if ( value . kind === CachedRouteKind . APP_ROUTE ) {
69
- return value . body . length
70
- }
71
- // rough estimate of size of cache value
72
- return (
73
- value . html . length +
74
- ( JSON . stringify (
75
- value . kind === CachedRouteKind . APP_PAGE
76
- ? value . rscData
77
- : value . pageData
78
- ) ?. length || 0 )
79
- )
80
- } )
56
+ FileSystemCache . memoryCache = getMemoryCache ( ctx . maxMemoryCacheSize )
57
+ } else if ( FileSystemCache . debug ) {
58
+ console . log ( 'memory store already initialized' )
81
59
}
82
- } else if ( this . debug ) {
60
+ } else if ( FileSystemCache . debug ) {
83
61
console . log ( 'not using memory store for fetch cache' )
84
62
}
85
63
}
@@ -92,7 +70,7 @@ export default class FileSystemCache implements CacheHandler {
92
70
let [ tags ] = args
93
71
tags = typeof tags === 'string' ? [ tags ] : tags
94
72
95
- if ( this . debug ) {
73
+ if ( FileSystemCache . debug ) {
96
74
console . log ( 'revalidateTag' , tags )
97
75
}
98
76
@@ -111,9 +89,9 @@ export default class FileSystemCache implements CacheHandler {
111
89
const [ key , ctx ] = args
112
90
const { kind } = ctx
113
91
114
- let data = memoryCache ?. get ( key )
92
+ let data = FileSystemCache . memoryCache ?. get ( key )
115
93
116
- if ( this . debug ) {
94
+ if ( FileSystemCache . debug ) {
117
95
if ( kind === IncrementalCacheKind . FETCH ) {
118
96
console . log ( 'get' , key , ctx . tags , kind , ! ! data )
119
97
} else {
@@ -182,7 +160,7 @@ export default class FileSystemCache implements CacheHandler {
182
160
// TODO: remove this when we can send the tags
183
161
// via header on GET same as SET
184
162
if ( ! tags ?. every ( ( tag ) => storedTags ?. includes ( tag ) ) ) {
185
- if ( this . debug ) {
163
+ if ( FileSystemCache . debug ) {
186
164
console . log ( 'tags vs storedTags mismatch' , tags , storedTags )
187
165
}
188
166
await this . set ( key , data . value , {
@@ -291,7 +269,7 @@ export default class FileSystemCache implements CacheHandler {
291
269
}
292
270
293
271
if ( data ) {
294
- memoryCache ?. set ( key , data )
272
+ FileSystemCache . memoryCache ?. set ( key , data )
295
273
}
296
274
} catch {
297
275
return null
@@ -345,12 +323,12 @@ export default class FileSystemCache implements CacheHandler {
345
323
data : IncrementalCacheValue | null ,
346
324
ctx : SetIncrementalFetchCacheContext | SetIncrementalResponseCacheContext
347
325
) {
348
- memoryCache ?. set ( key , {
326
+ FileSystemCache . memoryCache ?. set ( key , {
349
327
value : data ,
350
328
lastModified : Date . now ( ) ,
351
329
} )
352
330
353
- if ( this . debug ) {
331
+ if ( FileSystemCache . debug ) {
354
332
console . log ( 'set' , key )
355
333
}
356
334
0 commit comments