Repository navigation
Description
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
HybridCacheEntryContextfor the factory.HybridCacheEntryOptionswould not have aTagsproperty (tagsparameters on methods already exist). This would simplify the semantics for bothGetOrCreateAsyncandSetAsync, 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. mutableIList<string>.
Activity
jodydonetti commentedon Jan 7, 2026
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.
jamiewinder commentedon Jan 7, 2026
Thanks @jodydonetti - I'm already a convert! Truly excellent library.
dotnet-policy-service commentedon Jun 25, 2026
Tagging subscribers to this area: @dotnet/area-extensions-caching
See info in area-owners.md if you want to be subscribed.