-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add get_or_insert_with
method
#20
Conversation
- Implement get_or_insert_with and get_or_try_insert_with methods to future::Cache. - Add unit test cases for these methods. - Add async-lock crate to the dependencies for the future feature.
- Implement get_or_insert_with and get_or_try_insert_with methods to sync::Cache. - Add unit test cases for these methods.
- Implement get_or_insert_with and get_or_try_insert_with methods to sync::SegmentedCache. - Add unit test cases for these methods.
Delay removing a waiter to avoid a race condition.
- Relax the restriction of the init closure (FnMut -> FnOnce). - Write the API docs.
Refactoring: Rename internal methods.
Add Send + Sync + 'static bounds to the error type.
Remove duplicate `remove_waiter` calls.
Fix some source code comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merging.
I don't know rust conventions, but Java's names in
These are performed atomically and if Caffeine's corre apis uses I don't know if any of that makes sense idiomatically for Rust. If so, you might consider reviewing our APIs for ideas before yours are finalized. |
This PR adds the
get_or_insert_with
andget_or_try_insert_with
methods tosync
andfuture
caches in Moka. These methods ensure the key exists in the cache by inserting the return value from the given closure/future if the key does not exist. They also guarantee the closure/future is evaluated only once even if they are called from multiple threads/async tasks for the same key at the same time.This feature was requested by #15.
Added
get_or_insert_with
andget_or_try_insert_with
methods to the following cache implementations in Moka:sync::Cache
sync::SegmentedCache
future::Cache
sync::value_initializer
andfuture::value_initializer
modules, containingcht::HashMap
for waiters.Fixes #15