Open
Description
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 microsoft/reverse-proxy#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
Alternative Designs
None
Risks
None