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

Add TryGetValueAsync that evaluates GetValueFromAsyncLazy #168

Open
b-twis opened this issue Sep 23, 2021 · 1 comment
Open

Add TryGetValueAsync that evaluates GetValueFromAsyncLazy #168

b-twis opened this issue Sep 23, 2021 · 1 comment

Comments

@b-twis
Copy link

b-twis commented Sep 23, 2021

[Use the Thumbs Up reaction to vote for this feature, and please avoid adding comments like "+1" as they create noise for others watching the issue.]

Firstly, actively using your library in multiple places for quite some time and very happy with it.

In the recent inclusion of PR #142 when adding TryGetValue I dont feel like it fits the theme of the library as it does not perform GetValueFromAsyncLazy so it could give you back a factory (AsyncLazy) instead of the actual value

I read the comments and the end goal was something close to a cache.contains (and you can not out param an async method), but would be great it is also fit normal pattern of the other Gets.

I did something similar to below using a wrapper class to handle a similar issue elsewhere.

Here is an attempt of it in LazyCache.

public class CacheResponse<TV>
{
    public bool Found { get; set; }
    public string Key { get; set; }
    public TV Value { get; set; }
}

public virtual async Task<CacheResponse<T>> TryGetValueAsync<T>(string key)
{
    ValidateKey(key);
    
    if (CacheProvider.TryGetValue(key, out T item))
    {                
        return new CacheResponse<T>() { Found = true, Key = key, Value = await GetValueFromAsyncLazy<T>(item, out _) };
    } else
    {
        return new CacheResponse<T>() { Found = false, Key = key};
    }
}

Thoughts? I can make look at making a PR if you are interested.

Thanks,

Basil

@EnricoMassone
Copy link

Hello @b-twis I agree with you.

A few months ago I proposed a pull request already including the idea of a TryGetValueAsync

This is my pull request: #157

If you like we can discuss about this and propose a new pull request with the TryGetValueAsync improvement only

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants