Skip to content

feat: add configurable ttl on stampede protected cache, eviction telemetry#454

Open
codingsnoop wants to merge 24 commits into
mainfrom
u/emengesha/cachet-get-or-insert-with
Open

feat: add configurable ttl on stampede protected cache, eviction telemetry#454
codingsnoop wants to merge 24 commits into
mainfrom
u/emengesha/cachet-get-or-insert-with

Conversation

@codingsnoop
Copy link
Copy Markdown

This pull request introduces version 0.6.0 of the cachet crate, adding major new features for per-entry TTL control and improved eviction telemetry. The most significant changes include new get_or_insert_with and try_get_or_insert_with APIs for flexible cache entry creation, support for emitting cache.eviction telemetry events, and updates to documentation and internal structures to reflect these enhancements.

New APIs for Per-Entry TTL Control:

  • Added get_or_insert_with and try_get_or_insert_with methods to the Cache type, allowing users to provide closures that return CacheEntry<V>, which enables setting custom TTLs per entry on cache miss. These methods support both infallible and fallible computation functions, and integrate with stampede protection and concurrency mechanisms. [1] [2]

Eviction Telemetry Enhancements:

  • Introduced a new max_capacity builder method on CacheBuilder to specify a capacity threshold for emitting cache.eviction telemetry events when an insert causes an eviction. This does not enforce storage limits but enables monitoring cache evictions.
  • Added a new cache.eviction event to the telemetry system, including updates to documentation, event constants, and the CacheTelemetry interface to record these events. [1] [2] [3] [4]

Documentation and Versioning Updates:

  • Updated the crate version to 0.6.0 in Cargo.toml and all documentation links, and added release notes to the CHANGELOG.md describing the new features. [1] [2] [3]
  • Expanded documentation and examples for the new APIs and telemetry event, including in the main crate docs and README. [1] [2] [3] [4]

Internal and Test Infrastructure:

  • Extended internal merger structures and test utilities to support the new APIs and the max_capacity parameter, ensuring comprehensive coverage and correct behavior. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]

These changes provide more flexibility and observability for users of the cachet crate, especially for advanced caching scenarios requiring per-entry expiration and eviction monitoring.

Add methods where the closure returns CacheEntry<V> instead of raw V,
enabling per-entry TTL control on cache-miss computations. Both methods
include full stampede protection via dedicated Mergers.
Copilot AI review requested due to automatic review settings May 26, 2026 20:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR releases cachet v0.6.0, adding new cache-miss APIs that allow callers to return a full CacheEntry<V> (enabling per-entry TTL) and introducing eviction-related telemetry driven by an opt-in max_capacity threshold.

Changes:

  • Add Cache::{get_or_insert_with, try_get_or_insert_with} to compute-and-cache full CacheEntry<V> values (per-entry TTL support).
  • Add CacheBuilder::max_capacity and cache.eviction telemetry emission when inserts occur at/above the configured capacity threshold.
  • Bump cachet to 0.6.0 and update docs/tests accordingly.

Reviewed changes

Copilot reviewed 13 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
crates/cachet/src/cache.rs Adds the new *_with APIs and stampede-protection merger wiring.
crates/cachet/tests/cache.rs Adds integration tests covering the new APIs and stampede-protection behavior.
crates/cachet/src/builder/cache.rs Introduces max_capacity builder knob for eviction telemetry.
crates/cachet/src/builder/buildable.rs Plumbs max_capacity into constructed tiers (CacheWrapper).
crates/cachet/src/wrapper.rs Stores max_capacity and emits the new eviction telemetry on inserts.
crates/cachet/src/telemetry/cache.rs Adds CacheTelemetry::cache_eviction helper and updates tests.
crates/cachet/src/telemetry/attributes.rs Adds EVENT_EVICTION constant and uniqueness test coverage.
crates/cachet/src/lib.rs Documents the new cache.eviction INFO-level telemetry event.
crates/cachet/src/fallback.rs Updates CacheWrapper::new call sites for the new parameter.
crates/cachet/src/refresh.rs Updates CacheWrapper::new call sites for the new parameter.
crates/cachet/README.md Updates documented telemetry event list and docs.rs links for 0.6.0.
crates/cachet/CHANGELOG.md Adds 0.6.0 release notes for the new APIs.
crates/cachet/Cargo.toml Bumps crate version to 0.6.0.
Cargo.lock Updates lockfile to reflect the new cachet version.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/cachet/src/telemetry/attributes.rs Outdated
Comment thread crates/cachet/Cargo.toml
Comment thread crates/cachet/src/wrapper.rs Outdated
Comment thread crates/cachet/src/builder/cache.rs Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented May 26, 2026

Codecov Report

❌ Patch coverage is 92.89617% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.9%. Comparing base (43d51a5) to head (732c4ad).

Files with missing lines Patch % Lines
crates/cachet_memory/src/builder.rs 70.4% 13 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main    #454     +/-   ##
========================================
- Coverage   100.0%   99.9%   -0.1%     
========================================
  Files         305     307      +2     
  Lines       23698   23879    +181     
========================================
+ Hits        23698   23866    +168     
- Misses          0      13     +13     

☔ 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.

Copilot AI review requested due to automatic review settings May 26, 2026 20:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 14 changed files in this pull request and generated 6 comments.

Comment thread crates/cachet/src/wrapper.rs Outdated
Comment thread crates/cachet/src/builder/cache.rs Outdated
Comment thread crates/cachet/src/telemetry/cache.rs Outdated
Comment thread crates/cachet/src/telemetry/attributes.rs Outdated
Comment thread crates/cachet/src/wrapper.rs Outdated
Comment thread crates/cachet/CHANGELOG.md
codingsnoop and others added 2 commits May 26, 2026 13:36
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 26, 2026 20:37
Comment thread crates/cachet/src/builder/cache.rs Outdated
Comment thread crates/cachet/src/cache.rs
Comment thread crates/cachet/src/wrapper.rs Outdated
Comment thread crates/cachet/CHANGELOG.md Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 14 out of 15 changed files in this pull request and generated 5 comments.

Comment thread crates/cachet/src/cache.rs
Comment thread crates/cachet/src/cache.rs
Comment thread crates/cachet/src/wrapper.rs Outdated
Comment thread crates/cachet/src/telemetry/cache.rs Outdated
Comment thread crates/cachet/CHANGELOG.md
@schgoo
Copy link
Copy Markdown
Collaborator

schgoo commented May 26, 2026

I think the low-hanging fruit implementation for this is to implement a Moka eviction listener, and then use that to emit the event you want

Copilot AI review requested due to automatic review settings May 27, 2026 15:35
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 21 out of 22 changed files in this pull request and generated 4 comments.

Comment thread crates/cachet/src/telemetry/cache.rs Outdated
Comment thread crates/cachet/src/telemetry/attributes.rs Outdated
Comment thread crates/cachet/CHANGELOG.md
Comment thread crates/cachet/src/builder/cache.rs Outdated
Comment thread crates/cachet/src/builder/cache.rs Outdated
Comment thread crates/cachet/src/eviction.rs Outdated
Comment thread crates/cachet/src/wrapper.rs Outdated
Comment thread crates/cachet/Cargo.toml Outdated
Comment thread crates/cachet_memory/src/builder.rs Outdated
Comment thread crates/cachet_memory/src/builder.rs
Comment thread crates/cachet_memory/src/notification.rs
Comment thread crates/cachet/tests/eviction.rs
Copilot AI review requested due to automatic review settings May 27, 2026 20:32
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 20 out of 21 changed files in this pull request and generated 3 comments.

Comment thread crates/cachet/README.md Outdated
Comment thread crates/cachet/src/telemetry/attributes.rs Outdated
Comment thread crates/cachet_memory/src/builder.rs
Copilot AI review requested due to automatic review settings May 27, 2026 21:10
@codingsnoop codingsnoop review requested due to automatic review settings May 27, 2026 21:10
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 27, 2026 21:10
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 20 out of 21 changed files in this pull request and generated 5 comments.

Comment thread crates/cachet/Cargo.toml
Comment on lines 4 to 8
[package]
name = "cachet"
description = "A composable, customizable multi-tier caching library with rich feature support."
version = "0.5.0"
version = "0.5.1"
readme = "README.md"
Comment thread crates/cachet/README.md Outdated
Comment on lines +268 to +273
[__cargo_doc2readme_dependencies_info]: ggGmYW0CYXZlMC43LjJhdIQbLiTyV0MU86EbZU15e0PmecoboQ9jo59bnAEbyDXw04U13GlhYvRhcoQbg_hDqE88LP4bMh0J5Y4y4Osb0zDJ1kwqOsoblCGrm49Rx2thZIiCaGJ5dGVzYnVmZTAuNS4wgmZjYWNoZXRlMC42LjCCbWNhY2hldF9tZW1vcnllMC4yLjCCbmNhY2hldF9zZXJ2aWNlZTAuMS4wgmtjYWNoZXRfdGllcmUwLjEuMIJkdGlja2UwLjMuMIJndHJhY2luZ2YwLjEuNDSCaXVuaWZsaWdodGUwLjIuMA
[__link0]: https://docs.rs/cachet/0.6.0/cachet/?search=TimeToRefresh
[__link1]: https://crates.io/crates/uniflight/0.2.0
[__link10]: https://docs.rs/cachet_tier/0.1.0/cachet_tier/?search=CacheTier
[__link11]: https://docs.rs/cachet/0.5.0/cachet/?search=InsertPolicy
[__link12]: https://docs.rs/cachet/0.5.0/cachet/?search=TimeToRefresh
[__link11]: https://docs.rs/cachet/0.6.0/cachet/?search=InsertPolicy
[__link12]: https://docs.rs/cachet/0.6.0/cachet/?search=TimeToRefresh
if let Some(entry) = self.storage.get(key).await? {
return Ok(entry);
}
let entry = f().await;
if let Some(entry) = self.storage.get(key).await? {
return Ok(entry);
}
let entry = f().await.map_err(Error::from_source)?;
Comment on lines +37 to +41
// Drive enough churn to force size-based evictions. Moka's housekeeping
// runs periodically (and as a side effect of cache operations), so we keep
// exercising the cache while waiting for an eviction event to surface.
let deadline = std::time::Instant::now() + Duration::from_secs(10);
let mut i: i32 = 0;
Copilot AI review requested due to automatic review settings May 27, 2026 21:26
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 20 out of 21 changed files in this pull request and generated 1 comment.

Comment thread crates/cachet/Cargo.toml
Comment on lines 5 to 8
name = "cachet"
description = "A composable, customizable multi-tier caching library with rich feature support."
version = "0.5.0"
version = "0.5.1"
readme = "README.md"
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.

5 participants