Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

perf: only prune if adding new entry #3872

Merged
merged 2 commits into from
Nov 23, 2024
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
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
Loading