@@ -41,13 +41,23 @@ const memoryCache = new LRUCache<PrivateCacheEntry>(
41
41
)
42
42
const pendingSets = new Map < string , Promise < void > > ( )
43
43
44
+ const debug = process . env . NEXT_PRIVATE_DEBUG_CACHE
45
+ ? console . debug . bind ( console , 'DefaultCacheHandler:' )
46
+ : ( ) => { }
47
+
44
48
const DefaultCacheHandler : CacheHandlerV2 = {
45
49
async get ( cacheKey ) {
46
- await pendingSets . get ( cacheKey )
50
+ const pendingPromise = pendingSets . get ( cacheKey )
51
+
52
+ if ( pendingPromise ) {
53
+ debug ( 'get' , cacheKey , 'pending' )
54
+ await pendingPromise
55
+ }
47
56
48
57
const privateEntry = memoryCache . get ( cacheKey )
49
58
50
59
if ( ! privateEntry ) {
60
+ debug ( 'get' , cacheKey , 'not found' )
51
61
return undefined
52
62
}
53
63
@@ -59,22 +69,35 @@ const DefaultCacheHandler: CacheHandlerV2 = {
59
69
// In-memory caches should expire after revalidate time because it is
60
70
// unlikely that a new entry will be able to be used before it is dropped
61
71
// from the cache.
72
+ debug ( 'get' , cacheKey , 'expired' )
73
+
62
74
return undefined
63
75
}
64
76
65
77
if ( isStale ( entry . tags , entry . timestamp ) ) {
78
+ debug ( 'get' , cacheKey , 'had stale tag' )
79
+
66
80
return undefined
67
81
}
68
82
const [ returnStream , newSaved ] = entry . value . tee ( )
69
83
entry . value = newSaved
70
84
85
+ debug ( 'get' , cacheKey , 'found' , {
86
+ tags : entry . tags ,
87
+ timestamp : entry . timestamp ,
88
+ revalidate : entry . revalidate ,
89
+ expire : entry . expire ,
90
+ } )
91
+
71
92
return {
72
93
...entry ,
73
94
value : returnStream ,
74
95
}
75
96
} ,
76
97
77
98
async set ( cacheKey , pendingEntry ) {
99
+ debug ( 'set' , cacheKey , 'start' )
100
+
78
101
let resolvePending : ( ) => void = ( ) => { }
79
102
const pendingPromise = new Promise < void > ( ( resolve ) => {
80
103
resolvePending = resolve
@@ -100,8 +123,11 @@ const DefaultCacheHandler: CacheHandlerV2 = {
100
123
errorRetryCount : 0 ,
101
124
size,
102
125
} )
103
- } catch {
126
+
127
+ debug ( 'set' , cacheKey , 'done' )
128
+ } catch ( err ) {
104
129
// TODO: store partial buffer with error after we retry 3 times
130
+ debug ( 'set' , cacheKey , 'failed' , err )
105
131
} finally {
106
132
resolvePending ( )
107
133
pendingSets . delete ( cacheKey )
@@ -113,11 +139,18 @@ const DefaultCacheHandler: CacheHandlerV2 = {
113
139
} ,
114
140
115
141
async getExpiration ( ...tags ) {
116
- return Math . max ( ...tags . map ( ( tag ) => tagsManifest . get ( tag ) ?? 0 ) )
142
+ const expiration = Math . max (
143
+ ...tags . map ( ( tag ) => tagsManifest . get ( tag ) ?? 0 )
144
+ )
145
+
146
+ debug ( 'getExpiration' , { tags, expiration } )
147
+
148
+ return expiration
117
149
} ,
118
150
119
151
async expireTags ( ...tags ) {
120
152
const timestamp = Math . round ( performance . timeOrigin + performance . now ( ) )
153
+ debug ( 'expireTags' , { tags, timestamp } )
121
154
122
155
for ( const tag of tags ) {
123
156
// TODO: update file-system-cache?
0 commit comments