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

CachingService.TryGetValue returning wrong value #176

Open
Coder3333 opened this issue Apr 27, 2022 · 2 comments
Open

CachingService.TryGetValue returning wrong value #176

Coder3333 opened this issue Apr 27, 2022 · 2 comments

Comments

@Coder3333
Copy link

I expect CachingService.TryGetValue to return the value that I stored in the cache, but instead, it returns the Lazy that was used to generate the value. I would expect a call to GetValueFromLazy in this method to make sure the right object is returned. You will see this behavior if you use GetOrCreate to initially store the value, and then follow up with TryGetValue to read the value.

Also, because this re-uses T when calling CacheProvider.TryGetValue, it never finds the value. You would need to use CacheProvider.TryGetValue to be able to fetch the value.

Here is the TryGetValue method from https://github.com/alastairtree/LazyCache/blob/master/LazyCache/CachingService.cs.

` public virtual bool TryGetValue(string key, out T value)
{
ValidateKey(key);

        return CacheProvider.TryGetValue(key, out value);
    }

`

@Coder3333 Coder3333 added the bug label Apr 27, 2022
@alastairtree
Copy link
Owner

Yeah this looks like a bug to me - thanks for submitting it. It should return the correctly typed object and unwrap the lazy using GetValueFromLazy in the same way the GetOrAdd does.

I assume this test is also broken somehow - https://github.com/alastairtree/LazyCache/blob/master/LazyCache.UnitTests/CachingServiceMemoryCacheProviderTests.cs#L1155

Would you be willing to submit a PR for the broken test and a fix?

@phadtrapong
Copy link

I have raise a PR to try fix this, Please feel free to share your feedback.

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

3 participants