Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 708ce02

Browse files
committed
fix: cleanup sqlite store
1 parent a1fb2cc commit 708ce02

File tree

1 file changed

+43
-47
lines changed

1 file changed

+43
-47
lines changed

lib/cache/sqlite-cache-store.js

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,6 @@ class SqliteCacheStore {
169169
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
170170
`)
171171

172-
this.#deleteExpiredValuesQuery = this.#db.prepare(
173-
'DELETE FROM cacheInterceptorV1 WHERE deleteAt <= ?'
174-
)
175-
176172
this.#deleteByUrlQuery = this.#db.prepare(
177173
'DELETE FROM cacheInterceptorV1 WHERE url = ?'
178174
)
@@ -181,20 +177,22 @@ class SqliteCacheStore {
181177
'SELECT COUNT(*) AS total FROM cacheInterceptorV1'
182178
)
183179

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+
`)
198196
}
199197

200198
close () {
@@ -241,50 +239,39 @@ class SqliteCacheStore {
241239
assertCacheValue(value)
242240

243241
const url = this.#makeValueUrl(key)
244-
let currentSize = 0
242+
let size = 0
245243
/**
246244
* @type {Buffer[] | null}
247245
*/
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
255248

256249
const writable = new Writable({
257250
write (chunk, encoding, callback) {
258251
if (typeof chunk === 'string') {
259252
chunk = Buffer.from(chunk, encoding)
260253
}
261254

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
270256

257+
if (size < store.#maxEntrySize) {
271258
body.push(chunk)
259+
} else {
260+
this.destroy()
272261
}
273262

274263
callback()
275264
},
276265
final (callback) {
277-
if (body === null) {
278-
return callback()
279-
}
266+
store.prune()
280267

281268
/**
282269
* @type {SqliteStoreValue | undefined}
283270
*/
284-
const existingValue = findValue(key, true)
271+
const existingValue = store.#findValue(key, true)
285272
if (existingValue) {
286273
// Updating an existing response, let's delete it
287-
updateValueQuery.run(
274+
store.#updateValueQuery.run(
288275
JSON.stringify(stringifyBufferArray(body)),
289276
value.deleteAt,
290277
value.statusCode,
@@ -298,7 +285,7 @@ class SqliteCacheStore {
298285
)
299286
} else {
300287
// New response, let's insert it
301-
insertValueQuery.run(
288+
store.#insertValueQuery.run(
302289
url,
303290
key.method,
304291
JSON.stringify(stringifyBufferArray(body)),
@@ -339,15 +326,25 @@ class SqliteCacheStore {
339326
* @returns {Number} The number of entries removed
340327
*/
341328
prune () {
342-
const total = this.size
329+
if (this.size <= this.#maxCount) {
330+
return 0
331+
}
343332

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+
}
346338
}
347339

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+
}
349346

350-
return res.changes
347+
return 0
351348
}
352349

353350
/**
@@ -369,7 +366,7 @@ class SqliteCacheStore {
369366

370367
/**
371368
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
372-
* @param {boolean} [canBeExpired=false]
369+
* @param {Boolean} [canBeExpired=false]
373370
* @returns {(SqliteStoreValue & { vary?: Record<string, string[]> }) | undefined}
374371
*/
375372
#findValue (key, canBeExpired = false) {
@@ -388,7 +385,6 @@ class SqliteCacheStore {
388385
const now = Date.now()
389386
for (const value of values) {
390387
if (now >= value.deleteAt && !canBeExpired) {
391-
this.#deleteExpiredValuesQuery.run(now)
392388
return undefined
393389
}
394390

0 commit comments

Comments
 (0)