Skip to content

[API Proposal]: HybridCache computed tags #129842

Open

Description

@jamiewinder

Background and motivation (edited by @svick)

At present, HybridCache allows us to specify tags when requesting an item from the cache:

var item = await cache.GetOrCreateAsync(
    key: "item-123", 
    factory: cancel => LoadItem("123", cancel),
    tags: ["a", "b", "c"]);

However it's not possible to compute the tags based on the result returned from the factory.

This would be really useful to be able to relate the cache entry to entities that aren't known until the cached item is actually loaded (a customer, a group, etc.). Today the only way to tag by such values is to load the data twice (once to discover the tags, once inside the factory) or to abandon  GetOrCreateAsync and hand-roll get/set with a separate  SetAsync , losing stampede protection.

The recently approved dynamic setting of HybridCacheEntryOptions (in #121530, e.g. using the following overload) makes this a simple addition.

public ValueTask<T> GetOrCreateAsync<T>(
    string key,
    Func<HybridCacheEntryOptions, CancellationToken, ValueTask<T>> factory,
    HybridCacheEntryOptions? options = null,
    IEnumerable<string>? tags = null,
    CancellationToken cancellationToken = default);

API Proposal (by @svick)

The proposal is to add a Tags collection to HybridCacheEntryOptions:

namespace Microsoft.Extensions.Caching.Hybrid;

public sealed partial class HybridCacheEntryOptions
{
    public IEnumerable<string> Tags { get; set; }
}

Semantics: For SetAsync and GetOrCreateAsync overloads without HybridCacheEntryOptions in the factory, effective tags are the union of the tags parameter and the options.Tags passed to the method. For GetOrCreateAsync overloads with HybridCacheEntryOptions in the factory, the factory receives options containing a union of the tags parameter and the options.Tags passed to the method, the effective tags are what's in the factory options on return from the factory.

API Usage (by @svick)

var item = await cache.GetOrCreateAsync(
    key: "item-123",
    factory: async (options, ct) =>
    {
        var loaded = await LoadItemAsync("123", ct);
        options.Tags = [loaded.CustomerId, loaded.GroupId];
        return loaded;
    });

// later: invalidate everything for a customer
await cache.RemoveByTagAsync(customerId);

Caller and factory tags combined:

await cache.GetOrCreateAsync(
    "item-123",
    async (options, ct) =>
    {
        var loaded = await LoadItemAsync("123", ct);
        options.Tags = options.Tags.Append(loaded.CustomerId); // discovered tag
        return loaded;
    },
    tags: ["catalog"]); // static tag known up front
// entry is tagged with { "catalog", <customerId> }

Alternative Designs (by @svick)

  • Have a separate HybridCacheEntryContext for the factory. HybridCacheEntryOptions would not have a Tags property (tags parameters on methods already exist). This would simplify the semantics for both GetOrCreateAsync and SetAsync, since there would be no more need for union of two sets of tags. This design is further described in the comment [API Proposal]: HybridCache: Setting expiration based on value in cache entry factory #121530 (comment).

    namespace Microsoft.Extensions.Caching.Hybrid;
    
    public sealed partial class HybridCacheEntryContext
    {
        public IEnumerable<string> Tags { get; set; }
    }
  • Another collection type for Tags, e.g. mutable IList<string>.

Activity

  1. added
    api-suggestion
    untriaged
    on Mar 12, 2025
  2. jodydonetti commented on Jan 7, 2026

    @jodydonetti
    Contributor

    Hi @jamiewinder

    As a (temporary?) fix you may look into using the FusionCache based implementation of HybridCache, see here.

    It also has a backplane to keep the cache coherent even in a multi-node environment, and that also works with the HybridCache impl.

    It's a 100% compatible drop-in impl replacement, no code changes required except for 1 line during setup.

    Hope this helps.

    NB: FusionCache creator here, just to be clear.

  3. jamiewinder commented on Jan 7, 2026

    @jamiewinder
    Author

    Thanks @jodydonetti - I'm already a convert! Truly excellent library.

  4. transferred this issue fromdotnet/extensionson Jun 25, 2026
  5. self-assigned this
    on Jun 25, 2026
  6. dotnet-policy-service commented on Jun 25, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-extensions-caching
    See info in area-owners.md if you want to be subscribed.

  7. removed
    untriaged
    on Jun 25, 2026
  8. added this to the Future milestone on Jun 30, 2026
  9. added
    api-ready-for-review
    and removed
    api-suggestion
    on Jul 3, 2026
  10. added
    api-suggestion
    and removed
    api-ready-for-review
    on Jul 22, 2026
  11. removed their assignment
    on Jul 22, 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

    Labels

    api-suggestionarea-Extensions-Caching

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions