Repository navigation
Description
Background and motivation
Say I have a method that returns a collection of properties associated with a given account. Today, I can key the result in the cache with something like "account:{accountId}:properties" so that if I delete the account, I can invalidate it in the cache.
However, if I delete a property, it shouldn't then show up on this cached list as it doesn't exist anymore. Presumably, this is where I could use tags. When I create the cache entry via GetOrCreateAsync, I have the option to supply a collection of tag strings as one of the arguments, but.. and critically, I have to know those values ahead of time. The create factory has no reference to the entry itself and when I know the values, I've already committed the cache entry and cannot supplement with the values.
API Proposal
public abstract ValueTask<T> GetOrCreateAsync<TState, T>(
string key,
Func<HybridCacheEntryContext, CancellationToken, ValueTask<T>> factory,
HybridCacheEntryOptions? options = null,
CancellationToken cancellationToken = default);
public class HybridCacheEntryContext
{
public List<string> Tags { get; set; } = new();
}API Usage
return await cache.GetOrAddCache($"account:{accountId}:properties", async (entry, ct) => {
var properties = await GetPropertiesFromDataSource();
entry.Tags = properties.Select(p => $"property:{p.Id}").ToList();
return properties;
});This in turn would mean that when I remove one of the properties in the future, I can simply call await cache.RemoveByTagAsync($"property:{propertyId}"); and know that my list of properties referencing this key is also invalidated in a way I cannot do today.
Alternative Designs
I've tentatively implemented this as an extension method, requiring no other changes to the API:
public static class HybridCacheExtensions
{
public static ValueTask<T> GetOrCreateWithTagsAsync<T>(
this HybridCache cache,
string key,
Func<HybridCacheEntryContext, CancellationToken, ValueTask<T>> factory,
HybridCacheEntryOptions? options = null,
CancellationToken cancellationToken = default)
{
var context = new HybridCacheEntryContext();
return cache.GetOrCreateAsync(
key,
context,
factory,
options,
tags: context.Tags,
cancellationToken);
}
}
public sealed class HybridCacheEntryContext
{
public List<string> Tags { get; } = [];
}Risks
It might be ideal to refactor this to accommodate use of the HybridCacheEntryContext so it's not just dangling for the purpose of this overload. I'm not familiar with the HybridCache implementation, but presumably this ask could just be additive.
Activity
MihuBot commentedon Oct 7, 2025
I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):
jodydonetti commentedon Jan 7, 2026
Hi @WhitWaldo
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.