Skip to content
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

Fix warning CA1062#DefaultCacheKeyStrategy #2218

Merged
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
12 changes: 9 additions & 3 deletions src/Polly/Caching/DefaultCacheKeyStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ namespace Polly.Caching;
/// <summary>
/// The default cache key strategy for <see cref="CachePolicy"/>. Returns the property <see cref="Context.OperationKey"/>.
/// </summary>
#pragma warning disable CA1062 // Validate arguments of public methods
public class DefaultCacheKeyStrategy : ICacheKeyStrategy
{
/// <summary>
/// Gets the cache key from the given execution context.
/// </summary>
/// <param name="context">The execution context.</param>
/// <returns>The cache key.</returns>
public string GetCacheKey(Context context) =>
context.OperationKey;
public string GetCacheKey(Context context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}

return context.OperationKey;
}

/// <summary>
/// Gets an instance of the <see cref="DefaultCacheKeyStrategy"/>.
Expand Down
8 changes: 8 additions & 0 deletions test/Polly.Specs/Caching/DefaultCacheKeyStrategySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

public class DefaultCacheKeyStrategySpecs
{
[Fact]
public void Should_throw_when_context_is_null()
{
Context context = null!;
Action action = () => DefaultCacheKeyStrategy.Instance.GetCacheKey(context);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("context");
}

[Fact]
public void Should_return_Context_OperationKey_as_cache_key()
{
Expand Down