HybridCache: Enable setting expiration based on value in factory
Is there an existing issue for this?
- [X] I have searched the existing issues
Is your feature request related to a problem? Please describe the problem.
My HybridCache factory is fetching data from an HTTP API. The fetch response includes a maxAge header, from which I want to set cache expiration.
There's no way of getting the value from the factory method to the expiration setting.
Describe the solution you'd like
Some way to enable this
Additional context
No response
we also need such feature. maybe change "HybridCacheEntryOptions? options" in method signature to "Func<T, HybridCacheEntryOptions?>" ?
This should not be "hard" to do but brings a lot of value since one can set expiration depending on the factory response.
Hi, folks. I am just highlighting that it was also requested in the discussions/forum here: https://github.com/dotnet/aspnetcore/discussions/57021
The demand is noted. We can try to look at what we can add here.
Without this feature we have to continue with our MemoryCache and IDistirbutedCache combination
@palcarlsen if you have this specific need, you can meanwhile use FusionCache, which supports this in the form of adaptive caching.
You can use it now directly and in the future, if you want, switch to use the HybridCache adapter and then, again if desired, change the implementation to the Microsoft one when this feature may become available.
Hope this helps.
Any update on when this can be expected to be available? I rather use the abstract that HybridCache gives (which I expected to support this already cause IMemoryCache does also) instead of using FusionCache (how amazing it may be)
(if someone could unassign me, that'd be awesome, thx)
Done all the ones I have permissions to do so on.
@lindeberg For the time being have you tried using something like this?
var data = await _hybridCache.GetOrCreateAsync(key, _ => ValueTask.FromResult(""), new HybridCacheEntryOptions { Flags = HybridCacheEntryFlags.DisableUnderlyingData }); // Does not call the factory so it will return null if not found in cache
// Some logic to assign a value to data
_hybridCache.SetAsync(key, data, new HybridCacheEntryOptions
{
Expiration = CalculateExpiration(data) // your logic to calculate the expiration.
}).GetAwaiter().GetResult();
Not the cleanest solution, but you can probably create a version of GetOrCreateAsync to hide this from the rest of the code.
It's not possible @endizhupani-cel , for a couple of reasons:
-
nullis a perfectly valid value, and can be the value in the cache (it's also a best practice to cachenulls) and by checking for it you are handling a cache hit withnullas a cache miss - by using 2 separate method calls you are losing stampede protection
I also tried multiple ways to achieve with HybridCache what in FusionCache is adaptive caching, but I got to 4 min barriers:
- each option in
HybridCacheEntryOptionsisinit-only - the entire
HybridCacheEntryOptionsclass issealed - the signature for the factory does not allow changing it
- even by being able to change an existing instance somehow (eg: reflection etc), I think something internally would break because the implementation counts on those instances to be readonly (e.g.: look here)
Hope this helps.
Is there any update on when this will be available? Thank you