Skip to content
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

Feature Request: Get or Insert Future #15

Closed
Dessix opened this issue May 21, 2021 · 5 comments · Fixed by #20
Closed

Feature Request: Get or Insert Future #15

Dessix opened this issue May 21, 2021 · 5 comments · Fixed by #20
Assignees
Labels
enhancement New feature or request

Comments

@Dessix
Copy link

Dessix commented May 21, 2021

The ability to get-or-insert atomically would be nice for preventing multiple requests for the same resource from being created to populate the cache. Otherwise, there's a race condition between get and insert where another task could attempt to get as well, and also see an empty record to populate.

@tatsuya6502 tatsuya6502 added the enhancement New feature or request label May 21, 2021
@tatsuya6502
Copy link
Member

Thanks for your suggestion. I think this would be a good feature to add. I will work on it once I finish working on another feature (#12).

Which Moka Cache implementation are you going to use? moka::sync::Cache or moka::future::Cache?

@Dessix
Copy link
Author

Dessix commented May 27, 2021

Futures- thanks ^^

@tatsuya6502
Copy link
Member

Hi @Dessix

I created a topic branch and added the following methods to the future cache:

async fn get_or_insert_with(&self, key: K, init: impl Future<Output = V>) -> V
  • This method takes a key and a future to initialize the value.
  • If the key does not exist in the cache and no other get_or_insert_with or get_or_try_insert_with method is already running for the key, it awaits for the future, inserts the value returned from the future to the cache, and returns the value to the caller.
  • If other method is already running (the future is being awaited), it waits for the other method to complete and returns the value.
async fn get_or_try_insert_with<F>(&self, key: K, init: F) -> Result<V, Arc<Box<dyn Error>>>
where
    F: Future<Output = Result<V, Box<dyn Error>>>
  • This method is similar to get_or_insert_with but the future resolves into Result<V, Box<dyn std::error::Error>> instead of V.
  • A value is inserted to the cache only when the future resolves into Ok(V).

Please check the unit tests out as they will show you how to use them (test1 and test2). Let me know what you think.

@tatsuya6502 tatsuya6502 self-assigned this Jul 8, 2021
@tatsuya6502
Copy link
Member

Finished developing and #20 has been merged into the master branch. I am going to release Moka v0.5.0 with this feature in a day or so.

@tatsuya6502
Copy link
Member

Published Moka v0.5.0 to crates.io.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants