feat: add configurable ttl on stampede protected cache, eviction telemetry#454
feat: add configurable ttl on stampede protected cache, eviction telemetry#454codingsnoop wants to merge 24 commits into
Conversation
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.
There was a problem hiding this comment.
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 fullCacheEntry<V>values (per-entry TTL support). - Add
CacheBuilder::max_capacityandcache.evictiontelemetry emission when inserts occur at/above the configured capacity threshold. - Bump
cachetto 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.
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
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 |
…b.com/microsoft/oxidizer into u/emengesha/cachet-get-or-insert-with
…et_memory builder
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…b.com/microsoft/oxidizer into u/emengesha/cachet-get-or-insert-with
| [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" |
| [__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)?; |
| // 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; |
| 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" |
This pull request introduces version 0.6.0 of the
cachetcrate, adding major new features for per-entry TTL control and improved eviction telemetry. The most significant changes include newget_or_insert_withandtry_get_or_insert_withAPIs for flexible cache entry creation, support for emittingcache.evictiontelemetry events, and updates to documentation and internal structures to reflect these enhancements.New APIs for Per-Entry TTL Control:
get_or_insert_withandtry_get_or_insert_withmethods to theCachetype, allowing users to provide closures that returnCacheEntry<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:
max_capacitybuilder method onCacheBuilderto specify a capacity threshold for emittingcache.evictiontelemetry events when an insert causes an eviction. This does not enforce storage limits but enables monitoring cache evictions.cache.evictionevent to the telemetry system, including updates to documentation, event constants, and theCacheTelemetryinterface to record these events. [1] [2] [3] [4]Documentation and Versioning Updates:
Cargo.tomland all documentation links, and added release notes to theCHANGELOG.mddescribing the new features. [1] [2] [3]Internal and Test Infrastructure:
max_capacityparameter, 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
cachetcrate, especially for advanced caching scenarios requiring per-entry expiration and eviction monitoring.