forked from dotnet/yarp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIYarpOutputCachePolicyProvider.cs
More file actions
48 lines (38 loc) · 1.99 KB
/
IYarpOutputCachePolicyProvider.cs
File metadata and controls
48 lines (38 loc) · 1.99 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
// 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;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.Extensions.Options;
namespace Yarp.ReverseProxy.Configuration;
// TODO: update or remove this once AspNetCore provides a mechanism to validate the OutputCache policies https://github.com/dotnet/aspnetcore/issues/52419
internal interface IYarpOutputCachePolicyProvider
{
ValueTask<object?> GetPolicyAsync(string policyName);
}
internal sealed class YarpOutputCachePolicyProvider : IYarpOutputCachePolicyProvider
{
// Workaround for https://github.com/dotnet/yarp/issues/2598 to make YARP work with NativeAOT on .NET 8. This is not needed on .NET 9+.
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
private static readonly Type s_OutputCacheOptionsType = typeof(OutputCacheOptions);
private readonly OutputCacheOptions _outputCacheOptions;
private readonly IDictionary _policyMap;
public YarpOutputCachePolicyProvider(IOptions<OutputCacheOptions> outputCacheOptions)
{
_outputCacheOptions = outputCacheOptions?.Value ?? throw new ArgumentNullException(nameof(outputCacheOptions));
var property = s_OutputCacheOptionsType.GetProperty("NamedPolicies", BindingFlags.Instance | BindingFlags.NonPublic);
if (property == null || !typeof(IDictionary).IsAssignableFrom(property.PropertyType))
{
throw new NotSupportedException("This version of YARP is incompatible with the current version of ASP.NET Core.");
}
_policyMap = (property.GetValue(_outputCacheOptions, null) as IDictionary) ?? new Dictionary<string, object>();
}
public ValueTask<object?> GetPolicyAsync(string policyName)
{
return ValueTask.FromResult(_policyMap[policyName]);
}
}