Skip to content

HybridCache: Enable setting expiration based on value in factory #7097

Closed as duplicate

Description

@lindeberg

Is there an existing issue for this?

  • 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

Activity

  1. Gutse commented on Aug 6, 2024

    @Gutse

    we also need such feature. maybe change "HybridCacheEntryOptions? options" in method signature to "Func<T, HybridCacheEntryOptions?>" ?

  2. xnf commented on Nov 21, 2024

    @xnf

    This should not be "hard" to do but brings a lot of value since one can set expiration depending on the factory response.

  3. wilsonneto-swe commented on Dec 8, 2024

    @wilsonneto-swe

    Hi, folks.
    I am just highlighting that it was also requested in the discussions/forum here: dotnet/aspnetcore#57021

  4. mgravell commented on Dec 8, 2024

    @mgravell
    Contributor

    The demand is noted. We can try to look at what we can add here.

  5. palcarlsen commented on Mar 13, 2025

    @palcarlsen

    Without this feature we have to continue with our MemoryCache and IDistirbutedCache combination

  6. jodydonetti commented on Mar 13, 2025

    @jodydonetti
    Contributor

    @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.

  7. ThaDaVos commented on Jul 25, 2025

    @ThaDaVos

    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)

  8. mgravell commented on Jul 25, 2025

    @mgravell
    Contributor

    (if someone could unassign me, that'd be awesome, thx)

  9. martincostello commented on Jul 25, 2025

    @martincostello
    Member

    Done all the ones I have permissions to do so on.

  10. added this to the Backlog milestone on Aug 15, 2025
  11. endizhupani-cel commented on Sep 16, 2025

    @endizhupani-cel

    @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.

  12. jodydonetti commented on Sep 16, 2025

    @jodydonetti
    Contributor

    It's not possible @endizhupani-cel , for a couple of reasons:

    • 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)

    Hope this helps.

  13. MarGraz commented on Dec 18, 2025

    @MarGraz

    Is there any update on when this will be available? Thank you

  14. EricR-STCU commented on Feb 5, 2026

    @EricR-STCU

    Very interested in this feature. Any way to voice support / get it higher in the backlog?

  15. removed this from the Backlog milestone on Apr 23, 2026
  16. locked and limited conversation to collaborators on May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions