Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Options;

namespace Microsoft.AspNetCore.OutputCaching;

/// <inheritdoc />
internal class DefaultOutputCachePolicyProvider : IOutputCachePolicyProvider
{
private readonly OutputCacheOptions _options;

/// <summary>
/// Creates a new instance of <see cref="DefaultOutputCachePolicyProvider"/>.
/// </summary>
/// <param name="options">The options configured for the application.</param>
public DefaultOutputCachePolicyProvider(IOptions<OutputCacheOptions> options)
{
ArgumentNullException.ThrowIfNull(options);

_options = options.Value;
}

/// <inheritdoc />
public ValueTask<IOutputCachePolicy?> GetPolicyAsync(string policyName)
{
ArgumentNullException.ThrowIfNull(policyName);

IOutputCachePolicy? policy = null;

if (_options.NamedPolicies is not null && _options.NamedPolicies.TryGetValue(policyName, out var value))
{
policy = value;
}

return ValueTask.FromResult(policy);
}
}
17 changes: 17 additions & 0 deletions src/Middleware/OutputCaching/src/IOutputCachePolicyProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.OutputCaching;

/// <summary>
/// A type which can provide an <see cref="IOutputCachePolicy"/> for a particular name.
/// </summary>
public interface IOutputCachePolicyProvider
{
/// <summary>
/// Gets a <see cref="IOutputCachePolicy"/> from the given <paramref name="policyName"/>.
/// </summary>
/// <param name="policyName">The policy name to retrieve.</param>
/// <returns>The named <see cref="IOutputCachePolicy"/>.</returns>
ValueTask<IOutputCachePolicy?> GetPolicyAsync(string policyName);
}
5 changes: 5 additions & 0 deletions src/Middleware/OutputCaching/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
#nullable enable
Microsoft.AspNetCore.OutputCaching.DefaultOutputCachePolicyProvider
Microsoft.AspNetCore.OutputCaching.DefaultOutputCachePolicyProvider.DefaultOutputCachePolicyProvider(Microsoft.Extensions.Options.IOptions<Microsoft.AspNetCore.OutputCaching.OutputCacheOptions!>! options) -> void
Microsoft.AspNetCore.OutputCaching.DefaultOutputCachePolicyProvider.GetPolicyAsync(string! policyName) -> System.Threading.Tasks.ValueTask<Microsoft.AspNetCore.OutputCaching.IOutputCachePolicy?>
Microsoft.AspNetCore.OutputCaching.IOutputCachePolicyProvider
Microsoft.AspNetCore.OutputCaching.IOutputCachePolicyProvider.GetPolicyAsync(string! policyName) -> System.Threading.Tasks.ValueTask<Microsoft.AspNetCore.OutputCaching.IOutputCachePolicy?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;

namespace Microsoft.AspNetCore.OutputCaching.Tests;

public class DefaultOutputCachePolicyProviderTests
{
[Theory]
[InlineData("")]
[InlineData("policyName")]
public async Task GetsNamedPolicy(string policyName)
{
// Arrange
var options = new OutputCacheOptions();
var policy = DefaultPolicy.Instance;
options.AddPolicy(policyName, policy);

var outputCacheOptions = Options.Create(options);
var policyProvider = new DefaultOutputCachePolicyProvider(outputCacheOptions);

// Act
var actualPolicy = await policyProvider.GetPolicyAsync(policyName);

// Assert
Assert.Same(policy, actualPolicy);
}
}