Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 43 additions & 47 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 Down Expand Up @@ -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
Loading