Add tag trough OutputCacheAttribute
See original GitHub issueNOTE: Commandeering @kellberg’s original issue to turn it into an API proposal since folks are already engaged here.
Background and Motivation
Output Caching can be controlled via various APIs including via policy setup in DI, extension methods on the endpoint builder, and attributes on MVC actions and endpoint handler delegates. Cache entries can also be “tagged” which makes it possible to evict sets of cache entries. Unfortunately, the tagging mechanism is not exposed when configuring output caching via attributes.
The proposal is that we fix this short coming with the [OutputCache]
attritribute by allowing the following usage:
app.MapGet("/api/points-of-interest", [OutputCache(Tags = new [] { "geospatial" })]() => {});
Proposed API
namespace Microsoft.AspNetCore.OutputCaching;
public sealed class OutputCacheAttribute : Attribute
{
+ public string[]? Tags { get; init; }
}
Usage Examples
Minimal APIs
app.MapGet("/api/points-of-interest", [OutputCache(Tags = new [] { "geospatial" })]() => { ... });
MVC
[OutputCache(Tags = new [] { "geospatial" })]
public PointOfInterest[] GetPointsOfInterest() { }
Alternative Designs
We already have multiple ways of setting output cache values, this just adds a missing one to the [OutputCache(...)]
attribute. However I did consider whether we should use the singular Tag
as the property, however other properties on the attribute are already pluralized.
Risks
None that I can think of.
Original issue content from OP.
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.
I would like to add an output cache tag for cache eviction but the OutputCacheAttribute doesn’t have that option.
Describe the solution you’d like
Add a Tag parameter to OutputCacheAttribute and update BuildPolicy to add the tag to the IOutputCachePolicy.
Then you can add the tag through the attribute: [OutputCache(Tags = “test”)]
Issue Analytics
- State:
- Created 9 months ago
- Comments:8 (8 by maintainers)
@captainsafia do you know if small changes like this need to use the formal API review template?
Note to @kellberg in my implementation I used the plural
Tags
and it tags an array like many of the other properties on the attribute.Closing this issue now since the code has been added to main, it will appear in .NET 8.0.