Add clearsOnMemoryPressure parameter#19
Merged
Merged
Conversation
…earing This adds a new `clearsOnMemoryPressure` parameter to the initializer (defaults to `true` for backward compatibility) that allows callers to disable automatic cache clearing on memory pressure events. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #19 +/- ##
==========================================
+ Coverage 91.75% 92.03% +0.28%
==========================================
Files 1 1
Lines 194 201 +7
==========================================
+ Hits 178 185 +7
Misses 16 16 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Owner
|
Thanks! |
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.
This PR adds a new
clearsOnMemoryPressureparameter to theLRUCacheinitializer, allowing callers to opt out of automatic cache clearing on memory pressure events.Motivation
The current behavior clears the entire cache on any memory pressure event (including
.warning). While this is sensible for caches storing computed or regenerable data, it can cause issues for caches that are backed by persistent storage (like a database).In these cases, a cache miss after memory pressure leads to empty results instead of triggering a refetch—the calling code has no way to distinguish "this key was never cached" from "this key was evicted due to memory pressure."
By allowing callers to disable memory pressure clearing, they can choose the appropriate behavior for their use case:
clearsOnMemoryPressure: true(default) — for computed/regenerable data where clearing is safeclearsOnMemoryPressure: false— for database-backed caches where the LRU eviction policy should be the only eviction mechanismChanges
clearsOnMemoryPressure: Bool = trueparameter to the initializermemoryPressureSourceoptional and only initialized when the parameter istruefalseBackward Compatibility
The parameter defaults to
true, so existing code continues to work unchanged.