-
Notifications
You must be signed in to change notification settings - Fork 910
Expand file tree
/
Copy pathOutputCacheConfig.cs
More file actions
122 lines (100 loc) · 3.98 KB
/
OutputCacheConfig.cs
File metadata and controls
122 lines (100 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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 class 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.InvariantCultureIgnoreCase);
}
/// <summary>
/// Configuration for <see cref="OutputCachePolicyBuilder"/>
/// </summary>
public class NamedCacheConfig
{
/// <summary>
/// Flag to exclude or not the default policy
/// </summary>
public bool ExcludeDefaultPolicy { get; set; }
/// <inheritdoc cref="OutputCachePolicyBuilder.Expire(TimeSpan)"/>
public TimeSpan? Duration { 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
{
/// <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.Duration.HasValue)
builder.Expire(policy.Duration.Value);
if (policy.NoCache)
builder.NoCache();
if (policy.VaryByQueryKeys != null)
builder.SetVaryByQuery(policy.VaryByQueryKeys);
if (policy.VaryByHeaders != null)
builder.SetVaryByHeader(policy.VaryByHeaders);
}
}