-
Notifications
You must be signed in to change notification settings - Fork 882
[Container] Add OutputCache configuration from file #2883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c8a6604
f1e6dbb
c1d0f1b
2236aba
f1c7107
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.OutputCaching; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Yarp.ReverseProxy.Configuration; | ||
|
||
/// <summary> | ||
/// Configuration for <see cref="OutputCacheOptions"/> | ||
/// </summary> | ||
public sealed record OutputCacheConfig | ||
{ | ||
/// <inheritdoc cref="OutputCacheOptions.SizeLimit"/> | ||
public long SizeLimit { get; set; } = 100 * 1024 * 1024; | ||
|
||
/// <inheritdoc cref="OutputCacheOptions.MaximumBodySize"/> | ||
public long MaximumBodySize { get; set; } = 64 * 1024 * 1024; | ||
|
||
/// <inheritdoc cref="OutputCacheOptions.DefaultExpirationTimeSpan"/> | ||
public TimeSpan DefaultExpirationTimeSpan { get; set; } = TimeSpan.FromSeconds(60); | ||
|
||
/// <inheritdoc cref="OutputCacheOptions.UseCaseSensitivePaths"/> | ||
public bool UseCaseSensitivePaths { get; set; } | ||
|
||
/// <summary> | ||
/// Policies that will be added with <see cref="OutputCacheOptions.AddBasePolicy(Action{OutputCachePolicyBuilder}, bool)"/> | ||
/// </summary> | ||
public IDictionary<string, NamedCacheConfig> NamedPolicies { get; set; } = new Dictionary<string, NamedCacheConfig>(StringComparer.OrdinalIgnoreCase); | ||
} | ||
|
||
/// <summary> | ||
/// Configuration for <see cref="OutputCachePolicyBuilder"/> | ||
/// </summary> | ||
public sealed record NamedCacheConfig | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should either move this to a more nested namespace, or name this |
||
{ | ||
/// <summary> | ||
/// Flag to exclude or not the default policy | ||
/// </summary> | ||
public bool ExcludeDefaultPolicy { get; set; } | ||
|
||
/// <inheritdoc cref="OutputCachePolicyBuilder.Expire(TimeSpan)"/> | ||
public TimeSpan? ExpirationTimeSpan { get; set; } | ||
|
||
/// <inheritdoc cref="OutputCachePolicyBuilder.NoCache"/> | ||
public bool NoCache { get; set; } | ||
|
||
/// <inheritdoc cref="OutputCachePolicyBuilder.SetVaryByQuery(string[])"/> | ||
public string[]? VaryByQueryKeys { get; set; } | ||
|
||
/// <inheritdoc cref="OutputCachePolicyBuilder.SetVaryByHeader(string[])"/> | ||
public string[]? VaryByHeaders { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Collections of extensions to configure OutputCache | ||
/// </summary> | ||
public static class OutputCacheConfigExtensions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think we've mostly stuck with one type per file so far There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Meh, we have modern IDE that supports multiple types per file. I think when the types are small and related, I find it easier to keep it together, if the file isn't big of course. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type specifically should likely be in |
||
{ | ||
/// <summary> | ||
/// Add and configure OuputCache | ||
/// </summary> | ||
public static IServiceCollection AddOutputCache(this IServiceCollection services, IConfiguration config) | ||
{ | ||
if (config == null) | ||
{ | ||
return services; | ||
} | ||
|
||
var outputCacheConfig = config.Get<OutputCacheConfig>(); | ||
|
||
if (outputCacheConfig != null) | ||
{ | ||
services.AddOutputCache(outputCacheConfig); | ||
} | ||
|
||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Add and configure OuputCache | ||
/// </summary> | ||
public static IServiceCollection AddOutputCache(this IServiceCollection services, OutputCacheConfig config) | ||
{ | ||
return services.AddOutputCache(options => | ||
{ | ||
options.SizeLimit = config.SizeLimit; | ||
options.MaximumBodySize = config.MaximumBodySize; | ||
options.DefaultExpirationTimeSpan = config.DefaultExpirationTimeSpan; | ||
options.UseCaseSensitivePaths = config.UseCaseSensitivePaths; | ||
|
||
foreach (var policy in config.NamedPolicies) | ||
{ | ||
options.AddPolicy(policy.Key, | ||
builder => PolicyBuilder(builder, policy.Value), | ||
policy.Value.ExcludeDefaultPolicy); | ||
} | ||
}); | ||
} | ||
|
||
private static void PolicyBuilder(OutputCachePolicyBuilder builder, NamedCacheConfig policy) | ||
{ | ||
if (policy.ExpirationTimeSpan.HasValue) | ||
builder.Expire(policy.ExpirationTimeSpan.Value); | ||
|
||
if (policy.NoCache) | ||
builder.NoCache(); | ||
|
||
if (policy.VaryByQueryKeys != null) | ||
builder.SetVaryByQuery(policy.VaryByQueryKeys); | ||
|
||
if (policy.VaryByHeaders != null) | ||
builder.SetVaryByHeader(policy.VaryByHeaders); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace Yarp.ReverseProxy.Configuration; | ||
|
||
public class OutputCacheConfigTests | ||
{ | ||
[Fact] | ||
public async Task All_Options_Added() | ||
{ | ||
var config = new OutputCacheConfig(); | ||
config.DefaultExpirationTimeSpan = TimeSpan.FromSeconds(1); | ||
config.MaximumBodySize = 10; | ||
config.SizeLimit = 20; | ||
config.UseCaseSensitivePaths = true; | ||
config.NamedPolicies.Add("test1", new NamedCacheConfig { ExpirationTimeSpan = TimeSpan.FromSeconds(5), ExcludeDefaultPolicy = true }); | ||
config.NamedPolicies.Add("test2", new NamedCacheConfig { ExpirationTimeSpan = TimeSpan.FromSeconds(15), ExcludeDefaultPolicy = false }); | ||
config.NamedPolicies.Add("test3", new NamedCacheConfig { ExpirationTimeSpan = TimeSpan.FromSeconds(3), ExcludeDefaultPolicy = true, VaryByHeaders = new[] { "X-SomeHeader" } }); | ||
|
||
var builder = WebApplication.CreateBuilder(); | ||
builder.Services.AddOutputCache(config) | ||
.AddReverseProxy(); | ||
|
||
var app = builder.Build(); | ||
|
||
var policies = app.Services.GetRequiredService<IYarpOutputCachePolicyProvider>(); | ||
var test1 = await policies.GetPolicyAsync("test1"); | ||
var test2 = await policies.GetPolicyAsync("test2"); | ||
var test3 = await policies.GetPolicyAsync("test3"); | ||
|
||
Assert.NotNull(test1); | ||
Assert.NotNull(test2); | ||
Assert.NotNull(test3); | ||
} | ||
|
||
[Fact] | ||
public async Task All_Options_Added_Json() | ||
{ | ||
var json = | ||
""" | ||
{ | ||
"OutputCache": { | ||
"DefaultExpirationTimeSpan": "00:05:00", | ||
"MaximumBodySize": 10, | ||
"SizeLimit": 20, | ||
"UseCaseSensitivePaths": true, | ||
"NamedPolicies": { | ||
"test1": { | ||
"ExpirationTimeSpan": "00:05:00", | ||
"ExcludeDefaultPolicy": true | ||
}, | ||
"test2": { | ||
"ExpirationTimeSpan": "00:15:00", | ||
"ExcludeDefaultPolicy": false | ||
}, | ||
"test3": { | ||
"ExpirationTimeSpan": "00:03:00", | ||
"ExcludeDefaultPolicy": true, | ||
"VaryByHeaders": [ "X-SomeHeader" ] | ||
} | ||
} | ||
} | ||
} | ||
"""; | ||
var configBuilder = new ConfigurationBuilder(); | ||
var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)); | ||
var config = configBuilder.AddJsonStream(stream).Build(); | ||
|
||
var builder = WebApplication.CreateBuilder(); | ||
builder.Services.AddOutputCache(config.GetSection("OutputCache")) | ||
.AddReverseProxy(); | ||
|
||
var app = builder.Build(); | ||
|
||
var policies = app.Services.GetRequiredService<IYarpOutputCachePolicyProvider>(); | ||
var test1 = await policies.GetPolicyAsync("test1"); | ||
var test2 = await policies.GetPolicyAsync("test2"); | ||
var test3 = await policies.GetPolicyAsync("test3"); | ||
|
||
Assert.NotNull(test1); | ||
Assert.NotNull(test2); | ||
Assert.NotNull(test3); | ||
Comment on lines
+89
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I take it there's no good way to assert that the relevant properties were applied correctly? |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be in the container assembly instead?
Long-term this is something that should likely be in ASP.NET instead of YARP.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's something I asked myself too, but I want to be able to reference these types in Aspire later...
I coud just duplicate the code for now in Aspire, that's a possibility. It would avoid the need of publishing a nuget package for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose it's a question of whether we see this being valuable to YARP users outside of Aspire.
I don't have a problem with it shipping from YARP initially, we could always take the breaking change eventually if ASP.NET starts to include it.
Good point re: package update, if we're treating this as more experimental, then I think it'd be more flexible for us if we copy-paste it in Aspire instead, and keep it just in the container here for now.