-
Notifications
You must be signed in to change notification settings - Fork 764
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
[API Proposal]: Add GetAsync
to HybridCache
#5688
Comments
I have another type of issue with not having a simple Get method. I would like to cache the access token so I don't need to obtain it every time I call some API. The problem is that I want to configure the absolute expiration of the cache entry based on the expiration of the token provided by the factory method.
|
I don't know if this is better for you but it probably is a bit less expensive as it does not use exceptions. And it could cache public static async Task<(bool Found, T? Result)> TryGetAsync<T>(this HybridCache cache, string key)
where T : class
{
var factoryCalled = false;
var result = await cache.GetOrCreateAsync(
key,
_ =>
{
factoryCalled = true;
return ValueTask.FromResult(default(T));
},
new HybridCacheEntryOptions
{
Expiration = TimeSpan.Zero,
LocalCacheExpiration = TimeSpan.Zero,
Flags = HybridCacheEntryFlags.DisableLocalCacheWrite | HybridCacheEntryFlags.DisableDistributedCacheWrite
});
return (!factoryCalled, result);
} |
Thanks for the suggestion. |
I'd support the first API suggestion. I just replaced my use of Apart from the options already discussed in this thread, I also tried to use the HybridCacheEntryOptions options = new() { Flags = HybridCacheEntryFlags.DisableUnderlyingData };
SomeClass value = await cache.GetOrCreateAsync(someKey, _ => new ValueTask(new SomeClass()), options, ...); The compiler strongly believes |
Background and motivation
There are instances, such as session validation, where a consumer only wants to confirm that an item exists in the cache and not create it. This is not currently (directly) possible with the
HybridCache
implementation.API Proposal
API Usage
Alternative Designs
I can make this work using the existing API but I'm not a fan of my solution so if anyone has a better workaround let me know.
More efficient option suggested by @nibdev
Risks
No response
The text was updated successfully, but these errors were encountered: