Skip to content

OutputCache configuration - policy validation #52419

Open
@witskeeper

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

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

Alternative Designs

None

Risks

None

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewaresfeature-cachingIncludes: StackExchangeRedis and SqlServer distributed caches

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions