Skip to content
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
49 changes: 42 additions & 7 deletions src/Polly/Caching/AsyncCacheSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#nullable enable
namespace Polly;

#pragma warning disable CA1062 // Validate arguments of public methods
public partial class Policy
{
/// <summary>
Expand Down Expand Up @@ -46,8 +45,15 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITt
/// <returns>The policy instance.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheProvider"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="cacheKeyStrategy"/> is <see langword="null"/>.</exception>
public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null) =>
CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, TimeSpan ttl, ICacheKeyStrategy cacheKeyStrategy, Action<Context, string, Exception>? onCacheError = null)
{
if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

return CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheError);
}

/// <summary>
/// <para>Builds an <see cref="AsyncPolicy" /> that will function like a result cache for delegate executions returning a result.</para>
Expand Down Expand Up @@ -222,8 +228,22 @@ public static AsyncCachePolicy CacheAsync(
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
Action<Context, string, Exception>? onCachePutError)
{
if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

return CacheAsync(cacheProvider,
new RelativeTtl(ttl),
cacheKeyStrategy.GetCacheKey,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);
}

/// <summary>
/// <para>Builds an <see cref="AsyncPolicy" /> that will function like a result cache for delegate executions returning a result.</para>
Expand Down Expand Up @@ -254,8 +274,23 @@ public static AsyncCachePolicy CacheAsync(
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
Action<Context, string, Exception>? onCachePutError)
{
if (cacheKeyStrategy is null)
{
throw new ArgumentNullException(nameof(cacheKeyStrategy));
}

return CacheAsync(
cacheProvider,
ttlStrategy,
cacheKeyStrategy.GetCacheKey,
onCacheGet,
onCacheMiss,
onCachePut,
onCacheGetError,
onCachePutError);
}

/// <summary>
/// <para>Builds an <see cref="AsyncPolicy" /> that will function like a result cache for delegate executions returning a result.</para>
Expand Down
232 changes: 225 additions & 7 deletions test/Polly.Specs/Caching/CacheAsyncSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,244 @@ public class CacheAsyncSpecs : IDisposable
public void Should_throw_when_cache_provider_is_null()
{
IAsyncCacheProvider cacheProvider = null!;
Action action = () => Policy.CacheAsync(cacheProvider, TimeSpan.MaxValue);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("cacheProvider");
var ttl = TimeSpan.MaxValue;
ITtlStrategy ttlStrategy = new ContextualTtl();
ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
Func<Context, string> cacheKeyStrategyFunc = (_) => string.Empty;
Action<Context, string> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string CacheProviderExpected = "cacheProvider";

Action action = () => Policy.CacheAsync(cacheProvider, ttl, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategyFunc, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategy, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategyFunc, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheProviderExpected);
}

[Fact]
public void Should_throw_when_ttl_strategy_is_null()
{
IAsyncCacheProvider cacheProvider = new StubCacheProvider();
ITtlStrategy ttlStrategy = null!;
Action action = () => Policy.CacheAsync(cacheProvider, ttlStrategy);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("ttlStrategy");
ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
Func<Context, string> cacheKeyStrategyFunc = (_) => string.Empty;
Action<Context, string> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string TtlStrategyExpected = "ttlStrategy";

Action action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCache, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(TtlStrategyExpected);
}

[Fact]
public void Should_throw_when_cache_key_strategy_is_null()
{
IAsyncCacheProvider cacheProvider = new StubCacheProvider();
Func<Context, string> cacheKeyStrategy = null!;
Action action = () => Policy.CacheAsync(cacheProvider, TimeSpan.MaxValue, cacheKeyStrategy);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be("cacheKeyStrategy");
var ttl = TimeSpan.MaxValue;
ITtlStrategy ttlStrategy = new ContextualTtl();
ICacheKeyStrategy cacheKeyStrategy = null!;
Func<Context, string> cacheKeyStrategyFunc = null!;
Action<Context, string> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string CacheKeyStrategyExpected = "cacheKeyStrategy";

Action action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategyFunc, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync(
cacheProvider,
ttl,
cacheKeyStrategy,
onCache,
onCache,
onCache,
onCacheError,
onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync(
cacheProvider,
ttlStrategy,
cacheKeyStrategy,
onCache,
onCache,
onCache,
onCacheError,
onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync(
cacheProvider,
ttl,
cacheKeyStrategyFunc,
onCache,
onCache,
onCache,
onCacheError,
onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);

action = () => Policy.CacheAsync(
cacheProvider,
ttlStrategy,
cacheKeyStrategyFunc,
onCache,
onCache,
onCache,
onCacheError,
onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(CacheKeyStrategyExpected);
}

[Fact]
public void Should_throw_when_on_cache_get_is_null()
{
IAsyncCacheProvider cacheProvider = new StubCacheProvider();
var ttl = TimeSpan.MaxValue;
ITtlStrategy ttlStrategy = new ContextualTtl();
ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
Func<Context, string> cacheKeyStrategyFunc = (_) => string.Empty;
Action<Context, string> onCacheGet = null!;
Action<Context, string> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string OnCacheGetExpected = "onCacheGet";

Action action = () => Policy.CacheAsync(cacheProvider, ttl, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategy, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategyFunc, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCacheGet, onCache, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheGetExpected);
}

[Fact]
public void Should_throw_when_on_cache_miss_is_null()
{
IAsyncCacheProvider cacheProvider = new StubCacheProvider();
var ttl = TimeSpan.MaxValue;
ITtlStrategy ttlStrategy = new ContextualTtl();
ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
Func<Context, string> cacheKeyStrategyFunc = (_) => string.Empty;
Action<Context, string> onCacheMiss = null!;
Action<Context, string> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string OnCacheMissExpected = "onCacheMiss";

Action action = () => Policy.CacheAsync(cacheProvider, ttl, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategy, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategyFunc, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCache, onCacheMiss, onCache, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCacheMissExpected);
}

[Fact]
public void Should_throw_when_on_cache_put_is_null()
{
IAsyncCacheProvider cacheProvider = new StubCacheProvider();
var ttl = TimeSpan.MaxValue;
ITtlStrategy ttlStrategy = new ContextualTtl();
ICacheKeyStrategy cacheKeyStrategy = new StubCacheKeyStrategy(context => context.OperationKey + context["id"]);
Func<Context, string> cacheKeyStrategyFunc = (_) => string.Empty;
Action<Context, string> onCachePut = null!;
Action<Context, string> onCache = (_, _) => { };
Action<Context, string, Exception>? onCacheError = null;
const string OnCachePutExpected = "onCachePut";

Action action = () => Policy.CacheAsync(cacheProvider, ttl, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategy, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);

action = () => Policy.CacheAsync(cacheProvider, ttl, cacheKeyStrategyFunc, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);

action = () => Policy.CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategyFunc, onCache, onCache, onCachePut, onCacheError, onCacheError);
action.Should().Throw<ArgumentNullException>().And.ParamName.Should().Be(OnCachePutExpected);
}

#endregion
Expand Down