Skip to content

HybridCache: Modify API to allow tags to be set as part of create factory #63944

@WhitWaldo

Description

@WhitWaldo

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.

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.

Describe the solution you'd like

I would like to request an overload to the create factory that includes a reference to the entry itself so I might supply the tags as part of the create operation, e.g., something like:

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.

Additional context

I appreciate the consideration!

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-area-labelUsed by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions