Dedicated method with state as an input#122
Closed
fsdvh wants to merge 9 commits into
Closed
Conversation
Add non-blocking API for sync cache
Expose shard_index method
* Add non-blocking methods * Do not leak lcs * Dedicated result * More * Simplify * Cleanup + some docs * Docs * Use a feature flag * Posion lock * Remove feature and more tests * Update src/sync.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Simplify signature * Simplify * clippy * More docs * Update README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/sync.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * AI suggestions * Update src/sync.rs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Dedicated method with state as an input * Shared state methods * More docs
arthurprs
added a commit
that referenced
this pull request
Jun 24, 2026
BREAKING (0.7.0). Collapses the growing per-operation variant cross-product. - `Lifecycle::RequestState` now requires `Default`; `begin_request` and `end_request` are removed. State is built via `Default` and finalized by its own `Drop` (which releases evicted items outside the shard lock). - The `*_with_lifecycle` methods now take `&mut L::RequestState` (returning `()` / `Result<(), _>`) instead of returning the state, so one state can be threaded through several ops to batch eviction work or inspect evicted items. This supersedes the `_with_state` proposal in #122. - Drops the now-vestigial `&L` plumbing from the placeholder machinery and the unused `Cache::lifecycle` field; shards keep their own clones for the hooks.
Owner
|
This is a good point and highlights that the API is limited. I'm leaning towards changing the lifecycles APIs to take &mut as you have here (See #124). That's a breaking change but justified. |
Contributor
Author
I really like your proposed changes. Happy to close my PR in favour of yours! |
Contributor
Author
|
Closing in favour of #124 |
arthurprs
added a commit
that referenced
this pull request
Jun 25, 2026
…124) * Reshape lifecycle API: Default-built RequestState threaded by &mut BREAKING (0.7.0). Collapses the growing per-operation variant cross-product. - `Lifecycle::RequestState` now requires `Default`; `begin_request` and `end_request` are removed. State is built via `Default` and finalized by its own `Drop` (which releases evicted items outside the shard lock). - The `*_with_lifecycle` methods now take `&mut L::RequestState` (returning `()` / `Result<(), _>`) instead of returning the state, so one state can be threaded through several ops to batch eviction work or inspect evicted items. This supersedes the `_with_state` proposal in #122. - Drops the now-vestigial `&L` plumbing from the placeholder machinery and the unused `Cache::lifecycle` field; shards keep their own clones for the hooks. * Drop RequestState outside the lock in set_capacity set_capacity created and dropped its own RequestState inside the shard method, so when called via Cache::set_capacity (shard.write().set_capacity) the evicted items were released while the write lock was still held — contradicting the documented Drop-after-lock contract. Take &mut lcs from the caller instead, so it drops after the lock guard. Also use Default::default() consistently instead of L::RequestState::default(). * Document lcs/lock drop ordering in set_capacity
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.
Summary
Adds
insert_with_stateandtry_insert_with_state— variants of the existing_with_lifecyclemethods that accept a caller-owned&mut L::RequestStateinstead of creating one internally. The existing_with_lifecyclemethods are refactored to delegate to these, eliminating duplicated shard-lookup and insert logic.Motivation
The
_with_lifecyclemethods give callers control over when evicted items are dropped (outside the shard lock). However, they each create their own lifecycle request, so callers who need to batch multiple insertions under a singlebegin_request/end_requestpair had no supported way to do so.The new
_with_statemethods fill this gap:Changes
insert_with_state(&self, key, value, lcs: &mut L::RequestState)— blocking insert into an existing lifecycle request.try_insert_with_state(&self, key, value, lcs: &mut L::RequestState) -> Result<(), (Key, Val)>— non-blocking variant; returns inputs on lock contention.insert_with_lifecycleandtry_insert_with_lifecyclenow delegate to the new methods.with_weighter,replace_with_lifecycle,insert_with_lifecycle,get_or_insert_with, andget_or_insert_async.