From 43bfeb4b1aef9f89d81d5108e2b425ccf9daf4fa Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 23 Nov 2024 19:32:37 +0100 Subject: [PATCH] perf: only prune if adding new entry (#3872) --- lib/cache/sqlite-cache-store.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/lib/cache/sqlite-cache-store.js b/lib/cache/sqlite-cache-store.js index afbdbc4771d..d782aafbe0b 100644 --- a/lib/cache/sqlite-cache-store.js +++ b/lib/cache/sqlite-cache-store.js @@ -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 @@ -283,6 +281,7 @@ class SqliteCacheStore { existingValue.id ) } else { + store.#prune() // New response, let's insert it store.#insertValueQuery.run( url, @@ -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 } @@ -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 }