dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.57k stars 10.05k forks source link

OutputCache configuration - policy validation #52419

Open witskeeper opened 1 year ago

witskeeper commented 1 year ago

Background and Motivation

The ASP.NET Core output caching middleware is great, but "limited" in terms of policy validation. Let's start with some code that you can write today in .NET 7:

builder.Services.AddOutputCache(options =>
{
    options.AddPolicy("customPolicy", builder =>  builder.Expire(TimeSpan.FromSeconds(20)));
});

There is no way to validate that customPolicy actually exists. This is useful when configuring multiple routes from configuration such as is the case for YARP. See https://github.com/microsoft/reverse-proxy/pull/2328

Proposed API

It would be preferred to something similar to IAuthorizationPolicyProvider implemented via DefaultAuthorizationPolicyProvider and ICorsPolicyProvider implemented via DefaultCorsPolicyProvider

namespace Microsoft.AspNetCore.OutputCaching;

+ public interface IOutputCachePolicyProvider
+ {
+     ValueTask<IOutputCachePolicy?> GetPolicyAsync(string policyName);
+ }
+
+ public class DefaultOutputCachePolicyProvider : IOutputCachePolicyProvider
+ {
+     private readonly OutputCacheOptions _options;
+     
+     public DefaultOutputCachePolicyProvider(IOptions<OutputCacheOptions> options)
+     {
+     
+     }
+     
+     public ValueTask<IOutputCachePolicy?> GetPolicyAsync(string policyName)
+     {
+         options.NamedPolicies[policyName];
+     }
+ }

OutputCacheOptions.NamedPolicies is internal hence this feature cannot be added in another library or the final application.

Usage Examples

See YARP: https://github.com/microsoft/reverse-proxy/blob/02435e0e2eb3e757fc928d9157cfcc7f2859910b/src/ReverseProxy/Configuration/RouteValidators/OutputCachePolicyValidator.cs#L27-L33

Alternative Designs

None

Risks

None

witskeeper commented 1 year ago

like #45684

ghost commented 1 year ago

Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:

halter73 commented 11 months ago

@sebastienros Have you seen this yet? Do you think it's a good idea?

sebastienros commented 11 months ago

My naive response would be to add a GetPolicy(string name) to the options, but the suggested design would offer more flexibility and allow new patterns to provide custom policies.

Note that the API suggestion seems to be a copy-paste of the RateLimiting one since it's referring to DefaultKeyType which is not part of OutputCache. The returned value should be IOutputCachePolicy.

Update: API suggestion has been fixed

halter73 commented 11 months ago

API Review Notes:

API Approved!

namespace Microsoft.AspNetCore.OutputCaching;

+ public interface IOutputCachePolicyProvider
+ {
+     ValueTask<IOutputCachePolicy?> GetPolicyAsync(string policyName);
+ }
onurkanbakirci commented 3 months ago

I'm working on it

onurkanbakirci commented 3 months ago

Done #57362

@witskeeper