Skip to content

Commit

Permalink
perf: only prune if adding new entry (nodejs#3872)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag authored Nov 23, 2024
1 parent 5a47b01 commit 43bfeb4
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions lib/cache/sqlite-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,6 @@ class SqliteCacheStore {
callback()
},
final (callback) {
store.prune()

const existingValue = store.#findValue(key, true)
if (existingValue) {
// Updating an existing response, let's overwrite it
Expand All @@ -283,6 +281,7 @@ class SqliteCacheStore {
existingValue.id
)
} else {
store.#prune()
// New response, let's insert it
store.#insertValueQuery.run(
url,
Expand Down Expand Up @@ -316,14 +315,8 @@ class SqliteCacheStore {
this.#deleteByUrlQuery.run(this.#makeValueUrl(key))
}

/**
* This method is called to prune the cache when it exceeds the maximum number
* of entries. It removes half the entries in the cache, ordering them the oldest.
*
* @returns {Number} The number of entries removed
*/
prune () {
if (this.size <= this.#maxCount) {
#prune () {
if (this.#size <= this.#maxCount) {
return 0
}

Expand All @@ -348,7 +341,7 @@ class SqliteCacheStore {
* Counts the number of rows in the cache
* @returns {Number}
*/
get size () {
get #size () {
const { total } = this.#countEntriesQuery.get()
return total
}
Expand Down

0 comments on commit 43bfeb4

Please sign in to comment.