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

Get-or-insert-with semantics / Dogpile Effect Mitigation #9

Open
Dessix opened this issue Mar 1, 2022 · 2 comments
Open

Get-or-insert-with semantics / Dogpile Effect Mitigation #9

Dessix opened this issue Mar 1, 2022 · 2 comments
Labels
feature-request Request a new feature help wanted Extra attention is needed

Comments

@Dessix
Copy link

Dessix commented Mar 1, 2022

Async Caches benefit from being able to ensure that - if a value is not currently cached - a future imminently fulfilling the entry will be passed to the other asynchronous readers. I haven't found a way to implement this pattern without adding another layer of internal mutability within the existing Stretto cache; is there an officially endorsed way to deduplicate cache fulfillment requests? If not, I'd like to request the feature.

@al8n al8n added the feature-request Request a new feature label Mar 1, 2022
@al8n
Copy link
Owner

al8n commented Mar 1, 2022

Hi Dessix, thank you suggestion. Currently, stretto does not provide a function get_or_insert like Hashmap or etc. I will add something like this in next release. Besides, would you mind to provide a mock example for your use cases for this feature request?

@Dessix
Copy link
Author

Dessix commented Mar 1, 2022

The important part is that it only calls the factory function to create the value when the value is missing- but it also prevents any subsequent calls from beginning construction while it is in the process of fulfilling the first.

A more concrete example is if I have an LRU cache to keys on a remote key/value store, and want to ensure that I only query the remote store once if my cache is missing the value- even if I suddenly receive many requests for it at once. Once it's fulfilled, all active requesting tasks can share the result.

c.get_or_try_fulfill_with(|k: &Key| async move {
  // some expensive, async operation to retrieve a value
  let response = myExpensiveRequest.await?;
  Ok(response)
})

@al8n al8n added the help wanted Extra attention is needed label May 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request a new feature help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants