Repository navigation
cacheable - fix: count falsy values in delete stats - #1743
Merged
Merged
Conversation
Before, delete read the value with get() and changed the stats only for
a truthy value. A cached 0, "", false, null, or undefined value did not
decrease count, ksize, or vsize, and did not increase deletes.
Now delete reads the raw entry with get(key, { raw: true }). It changes
the stats for each entry that is in the cache. It does not use getRaw(),
because getRaw() does not catch a store error. With getRaw(), a failed
primary read stops the delete.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DQyS4TFa7JFA5PEcKuLj4T
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1743 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 29 29
Lines 3615 3620 +5
Branches 810 811 +1
=========================================
+ Hits 3615 3620 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please check if the PR fulfills these requirements
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix. This is a follow-up to #1742. The review of #1742 found this bug.
Problem
When stats are on,
Cacheable.delete(key)read the value withget(). It changed the stats only when the value was truthy. Thus a cached0,"",false,null, orundefineddid not decreasecount,ksize, orvsize, and did not increasedeletes.take(key)callsdelete, so it had the same bug.Example:
set("hits", 0), thendelete("hits"). After this,stats.countstays at 1.deleteManydoes not have this bug after #1742. SodeleteanddeleteManygave different stats for the same keys.Change
packages/cacheable/src/index.ts,deleteInternal:this._primary.get(key, { raw: true }). It changes the stats for each entry that is in the cache. It skips a missing key.entry.value, which is the value thatsetsized.getRaw(). Keyvget()catches a store error, butgetRaw()does not. WithgetRaw(), a failed primary read makesdelete()reject before the delete starts./* v8 ignore next */comment. The tests now reach both branches.Verification
pnpm testpasses inpackages/cacheable,packages/cache-manager, andpackages/flat-cache, with a local Redis. I did not runpackages/net, because its tests need the mockhttp container.pnpm buildpasses inpackages/cacheable.cacheable/src/index.tsis the same as onmain. The only branch that is not covered is line 422 (thesyncsetter). This PR does not change it.Test audit:
cacheable stats enabled > should get stats on delete. This test owns the stats contract ofdelete, so I extended it and did not add a near-duplicate.deletechanges the stats for each key that is in the cache, for all values. A missing key does not change the stats."value",0,"",false,null, andundefined, and one missing key. It comparesksizeandvsizewith a cache that holds only the remaining key.main(deletesis 1, expected 6). It also fails withget()and an!== undefinedcheck (deletesis 5, expected 6), because of the cachedundefinedvalue.cacheable stats enabled > should delete with stats when the primary store read fails.delete. This was the behavior onmain.getRaw()for the stats read ("promise rejected ... instead of resolving"). The first version of this fix usedgetRaw(), so this regression is credible.delete. The test mocks only the storeget, which is the real boundary for this error.Generated by Claude Code