You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HybridCache: Enable setting expiration based on value in factory #7097
we also need such feature. maybe change "HybridCacheEntryOptions? options" in method signature to "Func<T, HybridCacheEntryOptions?>" ?
Reacted by Edgar Zagorskis, Wilson Neto, Michael McNeil, Oleksii Korniienko, Martin Altenstedt, Mattias, Marco Graziotti, Adam Nash, john-hartley-bf and Ben Sampica
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.
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)
@lindeberg For the time being have you tried using something like this?
vardata=await_hybridCache.GetOrCreateAsync(key, _ =>ValueTask.FromResult(""),newHybridCacheEntryOptions{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,newHybridCacheEntryOptions{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.
null is a perfectly valid value, and can be the value in the cache (it's also a best practice to cache nulls) and by checking for it you are handling a cache hit with null as 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 HybridCacheEntryOptions is init-only
the entire HybridCacheEntryOptions class is sealed
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)
Activity
Gutse commentedon Aug 6, 2024
we also need such feature. maybe change "HybridCacheEntryOptions? options" in method signature to "Func<T, HybridCacheEntryOptions?>" ?
xnf commentedon Nov 21, 2024
This should not be "hard" to do but brings a lot of value since one can set expiration depending on the factory response.
wilsonneto-swe commentedon Dec 8, 2024
Hi, folks.
I am just highlighting that it was also requested in the discussions/forum here: dotnet/aspnetcore#57021
mgravell commentedon Dec 8, 2024
The demand is noted. We can try to look at what we can add here.
palcarlsen commentedon Mar 13, 2025
Without this feature we have to continue with our MemoryCache and IDistirbutedCache combination
jodydonetti commentedon Mar 13, 2025
@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.
ThaDaVos commentedon Jul 25, 2025
Any update on when this can be expected to be available? I rather use the abstract that
HybridCachegives (which I expected to support this already causeIMemoryCachedoes also) instead of using FusionCache (how amazing it may be)mgravell commentedon Jul 25, 2025
(if someone could unassign me, that'd be awesome, thx)
martincostello commentedon Jul 25, 2025
Done all the ones I have permissions to do so on.
endizhupani-cel commentedon Sep 16, 2025
@lindeberg For the time being have you tried using something like this?
Not the cleanest solution, but you can probably create a version of GetOrCreateAsync to hide this from the rest of the code.
jodydonetti commentedon Sep 16, 2025
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 missI also tried multiple ways to achieve with HybridCache what in FusionCache is adaptive caching, but I got to 4 min barriers:
HybridCacheEntryOptionsisinit-onlyHybridCacheEntryOptionsclass issealedHope this helps.
MarGraz commentedon Dec 18, 2025
Is there any update on when this will be available? Thank you
EricR-STCU commentedon Feb 5, 2026
Very interested in this feature. Any way to voice support / get it higher in the backlog?