Skip to content

Add clearsOnMemoryPressure parameter#19

Merged
nicklockwood merged 1 commit into
nicklockwood:mainfrom
FelixLisczyk:sn-612
Jan 6, 2026
Merged

Add clearsOnMemoryPressure parameter#19
nicklockwood merged 1 commit into
nicklockwood:mainfrom
FelixLisczyk:sn-612

Conversation

@FelixLisczyk

Copy link
Copy Markdown
Contributor

This PR adds a new clearsOnMemoryPressure parameter to the LRUCache initializer, 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 safe
  • clearsOnMemoryPressure: false — for database-backed caches where the LRU eviction policy should be the only eviction mechanism

Changes

  • Added clearsOnMemoryPressure: Bool = true parameter to the initializer
  • Made memoryPressureSource optional and only initialized when the parameter is true
  • Also skips registering for the deprecated notification when false
  • Added a unit test for the new behavior

Backward Compatibility

The parameter defaults to true, so existing code continues to work unchanged.

…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

codecov Bot commented Jan 5, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.03%. Comparing base (0d91406) to head (12f4b1c).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
Sources/LRUCache.swift 86.66% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nicklockwood nicklockwood merged commit 8de1154 into nicklockwood:main Jan 6, 2026
5 of 6 checks passed
@nicklockwood

Copy link
Copy Markdown
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants