Skip to content

[API Proposal]: HybridCache - Allow tags to be set as part of create factory #6897

Closed as duplicate

Description

@WhitWaldo

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

  1. added
    api-suggestion
    on Oct 7, 2025
  2. MihuBot commented on Oct 7, 2025

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  3. jodydonetti commented on Jan 7, 2026

    @jodydonetti
    Contributor

    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.

  4. locked and limited conversation to collaborators on Aug 23, 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-caching-hybrid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions