question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add tag trough OutputCacheAttribute

See original GitHub issue

NOTE: 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:closed
  • Created 9 months ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
mitchdennycommented, Jan 3, 2023

@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.

1reaction
mitchdennycommented, Jan 11, 2023

Closing this issue now since the code has been added to main, it will appear in .NET 8.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Tag with [OutputCache] on Controllers instead ...
You can declare policy when adding services for output cache and pass policy name to the attribute: builder.Services.
Read more >
Output caching middleware in ASP.NET Core
Learn how to configure and use output caching middleware in ASP. ... the [OutputCache] attribute, as shown in the following examples:.
Read more >
OutputCache Attribute in MVC Application
OutputCache Attribute in ASP.NET MVC is used to cache the content returned by a controller action method. OutputCache Attribute in MVC.
Read more >
Exploring the new output caching middleware
To use the output caching middleware on top of your endpoint, you first need to register the OutputCache into your application.
Read more >
.NET 7 new Output Cache Middleware and It's Customization
At the end of this article you can add and use OutputCache middle ware and create our own Custom OutputCache policy based on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found