Skip to content

Commit

Permalink
fix: cleanup sqlite store
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 22, 2024
1 parent a1fb2cc commit 51a5596
Showing 1 changed file with 44 additions and 48 deletions.
92 changes: 44 additions & 48 deletions lib/cache/sqlite-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ class SqliteCacheStore {
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`)

this.#deleteExpiredValuesQuery = this.#db.prepare(
'DELETE FROM cacheInterceptorV1 WHERE deleteAt <= ?'
)

this.#deleteByUrlQuery = this.#db.prepare(
'DELETE FROM cacheInterceptorV1 WHERE url = ?'
)
Expand All @@ -181,20 +177,22 @@ class SqliteCacheStore {
'SELECT COUNT(*) AS total FROM cacheInterceptorV1'
)

const pruneLimit = this.#maxCount === Infinity
? 20
: Math.max(Math.floor(this.#maxCount * 0.1), 1)

this.#deleteOldValuesQuery = this.#db.prepare(`
DELETE FROM cacheInterceptorV1
WHERE id IN (
SELECT
id
FROM cacheInterceptorV1
ORDER BY cachedAt DESC
LIMIT ${pruneLimit}
)
`)
this.#deleteExpiredValuesQuery = this.#db.prepare(
'DELETE FROM cacheInterceptorV1 WHERE deleteAt <= ?'
)

this.#deleteOldValuesQuery = this.#maxCount === Infinity
? null
: this.#db.prepare(`
DELETE FROM cacheInterceptorV1
WHERE id IN (
SELECT
id
FROM cacheInterceptorV1
ORDER BY cachedAt DESC
LIMIT ?
)
`)
}

close () {
Expand Down Expand Up @@ -241,50 +239,39 @@ class SqliteCacheStore {
assertCacheValue(value)

const url = this.#makeValueUrl(key)
let currentSize = 0
let size = 0
/**
* @type {Buffer[] | null}
*/
let body = key.method !== 'HEAD' ? [] : null
const maxEntrySize = this.#maxEntrySize
const findValue = this.#findValue.bind(this)
const updateValueQuery = this.#updateValueQuery
const insertValueQuery = this.#insertValueQuery

this.prune()
const body = []
const store = this

const writable = new Writable({
write (chunk, encoding, callback) {
if (typeof chunk === 'string') {
chunk = Buffer.from(chunk, encoding)
}

currentSize += chunk.byteLength

if (body) {
if (currentSize >= maxEntrySize) {
body = null
this.end()
return callback()
}
size += chunk.byteLength

if (size < store.#maxEntrySize) {
body.push(chunk)
} else {
this.destroy()
}

callback()
},
final (callback) {
if (body === null) {
return callback()
}
store.prune()

/**
* @type {SqliteStoreValue | undefined}
*/
const existingValue = findValue(key, true)
const existingValue = store.#findValue(key, true)
if (existingValue) {
// Updating an existing response, let's delete it
updateValueQuery.run(
// Updating an existing response, let's overwrite it
store.#updateValueQuery.run(
JSON.stringify(stringifyBufferArray(body)),
value.deleteAt,
value.statusCode,
Expand All @@ -298,7 +285,7 @@ class SqliteCacheStore {
)
} else {
// New response, let's insert it
insertValueQuery.run(
store.#insertValueQuery.run(
url,
key.method,
JSON.stringify(stringifyBufferArray(body)),
Expand Down Expand Up @@ -339,15 +326,25 @@ class SqliteCacheStore {
* @returns {Number} The number of entries removed
*/
prune () {
const total = this.size
if (this.size <= this.#maxCount) {
return 0
}

if (total <= this.#maxCount) {
return
{
const removed = this.#deleteExpiredValuesQuery.run(Date.now()).changes
if (removed > 0) {
return removed
}
}

const res = this.#deleteOldValuesQuery.run()
{
const removed = this.#deleteOldValuesQuery.run(Math.max(Math.floor(this.#maxCount * 0.1), 1)).changes
if (removed > 0) {
return removed
}
}

return res.changes
return 0
}

/**
Expand All @@ -369,7 +366,7 @@ class SqliteCacheStore {

/**
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
* @param {boolean} [canBeExpired=false]
* @param {Boolean} [canBeExpired=false]
* @returns {(SqliteStoreValue & { vary?: Record<string, string[]> }) | undefined}
*/
#findValue (key, canBeExpired = false) {
Expand All @@ -388,7 +385,6 @@ class SqliteCacheStore {
const now = Date.now()
for (const value of values) {
if (now >= value.deleteAt && !canBeExpired) {
this.#deleteExpiredValuesQuery.run(now)
return undefined
}

Expand Down

0 comments on commit 51a5596

Please # to comment.