@@ -169,10 +169,6 @@ class SqliteCacheStore {
169
169
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
170
170
` )
171
171
172
- this . #deleteExpiredValuesQuery = this . #db. prepare (
173
- 'DELETE FROM cacheInterceptorV1 WHERE deleteAt <= ?'
174
- )
175
-
176
172
this . #deleteByUrlQuery = this . #db. prepare (
177
173
'DELETE FROM cacheInterceptorV1 WHERE url = ?'
178
174
)
@@ -181,20 +177,22 @@ class SqliteCacheStore {
181
177
'SELECT COUNT(*) AS total FROM cacheInterceptorV1'
182
178
)
183
179
184
- const pruneLimit = this . #maxCount === Infinity
185
- ? 20
186
- : Math . max ( Math . floor ( this . #maxCount * 0.1 ) , 1 )
187
-
188
- this . #deleteOldValuesQuery = this . #db. prepare ( `
189
- DELETE FROM cacheInterceptorV1
190
- WHERE id IN (
191
- SELECT
192
- id
193
- FROM cacheInterceptorV1
194
- ORDER BY cachedAt DESC
195
- LIMIT ${ pruneLimit }
196
- )
197
- ` )
180
+ this . #deleteExpiredValuesQuery = this . #db. prepare (
181
+ 'DELETE FROM cacheInterceptorV1 WHERE deleteAt <= ?'
182
+ )
183
+
184
+ this . #deleteOldValuesQuery = this . #maxCount === Infinity
185
+ ? null
186
+ : this . #db. prepare ( `
187
+ DELETE FROM cacheInterceptorV1
188
+ WHERE id IN (
189
+ SELECT
190
+ id
191
+ FROM cacheInterceptorV1
192
+ ORDER BY cachedAt DESC
193
+ LIMIT ?
194
+ )
195
+ ` )
198
196
}
199
197
200
198
close ( ) {
@@ -241,50 +239,39 @@ class SqliteCacheStore {
241
239
assertCacheValue ( value )
242
240
243
241
const url = this . #makeValueUrl( key )
244
- let currentSize = 0
242
+ let size = 0
245
243
/**
246
244
* @type {Buffer[] | null }
247
245
*/
248
- let body = key . method !== 'HEAD' ? [ ] : null
249
- const maxEntrySize = this . #maxEntrySize
250
- const findValue = this . #findValue. bind ( this )
251
- const updateValueQuery = this . #updateValueQuery
252
- const insertValueQuery = this . #insertValueQuery
253
-
254
- this . prune ( )
246
+ const body = [ ]
247
+ const store = this
255
248
256
249
const writable = new Writable ( {
257
250
write ( chunk , encoding , callback ) {
258
251
if ( typeof chunk === 'string' ) {
259
252
chunk = Buffer . from ( chunk , encoding )
260
253
}
261
254
262
- currentSize += chunk . byteLength
263
-
264
- if ( body ) {
265
- if ( currentSize >= maxEntrySize ) {
266
- body = null
267
- this . end ( )
268
- return callback ( )
269
- }
255
+ size += chunk . byteLength
270
256
257
+ if ( size < store . #maxEntrySize) {
271
258
body . push ( chunk )
259
+ } else {
260
+ this . destroy ( )
272
261
}
273
262
274
263
callback ( )
275
264
} ,
276
265
final ( callback ) {
277
- if ( body === null ) {
278
- return callback ( )
279
- }
266
+ store . prune ( )
280
267
281
268
/**
282
269
* @type {SqliteStoreValue | undefined }
283
270
*/
284
- const existingValue = findValue ( key , true )
271
+ const existingValue = store . # findValue( key , true )
285
272
if ( existingValue ) {
286
273
// Updating an existing response, let's delete it
287
- updateValueQuery . run (
274
+ store . # updateValueQuery. run (
288
275
JSON . stringify ( stringifyBufferArray ( body ) ) ,
289
276
value . deleteAt ,
290
277
value . statusCode ,
@@ -298,7 +285,7 @@ class SqliteCacheStore {
298
285
)
299
286
} else {
300
287
// New response, let's insert it
301
- insertValueQuery . run (
288
+ store . # insertValueQuery. run (
302
289
url ,
303
290
key . method ,
304
291
JSON . stringify ( stringifyBufferArray ( body ) ) ,
@@ -339,15 +326,25 @@ class SqliteCacheStore {
339
326
* @returns {Number } The number of entries removed
340
327
*/
341
328
prune ( ) {
342
- const total = this . size
329
+ if ( this . size <= this . #maxCount) {
330
+ return 0
331
+ }
343
332
344
- if ( total <= this . #maxCount) {
345
- return
333
+ {
334
+ const removed = this . #deleteExpiredValuesQuery. run ( Date . now ( ) ) . changes
335
+ if ( removed > 0 ) {
336
+ return removed
337
+ }
346
338
}
347
339
348
- const res = this . #deleteOldValuesQuery. run ( )
340
+ {
341
+ const removed = this . #deleteOldValuesQuery. run ( Math . max ( Math . floor ( this . #maxCount * 0.1 ) , 1 ) ) . changes
342
+ if ( removed > 0 ) {
343
+ return removed
344
+ }
345
+ }
349
346
350
- return res . changes
347
+ return 0
351
348
}
352
349
353
350
/**
@@ -369,7 +366,7 @@ class SqliteCacheStore {
369
366
370
367
/**
371
368
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey } key
372
- * @param {boolean } [canBeExpired=false]
369
+ * @param {Boolean } [canBeExpired=false]
373
370
* @returns {(SqliteStoreValue & { vary?: Record<string, string[]> }) | undefined }
374
371
*/
375
372
#findValue ( key , canBeExpired = false ) {
@@ -388,7 +385,6 @@ class SqliteCacheStore {
388
385
const now = Date . now ( )
389
386
for ( const value of values ) {
390
387
if ( now >= value . deleteAt && ! canBeExpired ) {
391
- this . #deleteExpiredValuesQuery. run ( now )
392
388
return undefined
393
389
}
394
390
0 commit comments