Skip to content

Commit

Permalink
propagate null for error delegates
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jan 14, 2023
1 parent 7cf4b58 commit c3e00e5
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 222 deletions.
8 changes: 4 additions & 4 deletions src/Polly/Caching/AsyncCacheEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ internal static async Task<TResult> ImplementationAsync<TResult>(
Action<Context, string> onCacheGet,
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception> onCacheGetError,
Action<Context, string, Exception> onCachePutError)
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError)
{
cancellationToken.ThrowIfCancellationRequested();

Expand All @@ -35,7 +35,7 @@ internal static async Task<TResult> ImplementationAsync<TResult>(
{
cacheHit = false;
valueFromCache = default;
onCacheGetError(context, cacheKey, ex);
onCacheGetError?.Invoke(context, cacheKey, ex);
}
if (cacheHit)
{
Expand All @@ -59,7 +59,7 @@ internal static async Task<TResult> ImplementationAsync<TResult>(
}
catch (Exception ex)
{
onCachePutError(context, cacheKey, ex);
onCachePutError?.Invoke(context, cacheKey, ex);
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/Polly/Caching/AsyncCachePolicy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Polly.Caching;
#nullable enable
namespace Polly.Caching;

/// <summary>
/// A cache policy that can be applied to the results of delegate executions.
Expand All @@ -12,8 +13,8 @@ public class AsyncCachePolicy : AsyncPolicy
private readonly Action<Context, string> _onCacheGet;
private readonly Action<Context, string> _onCacheMiss;
private readonly Action<Context, string> _onCachePut;
private readonly Action<Context, string, Exception> _onCacheGetError;
private readonly Action<Context, string, Exception> _onCachePutError;
private readonly Action<Context, string, Exception>? _onCacheGetError;
private readonly Action<Context, string, Exception>? _onCachePutError;

internal AsyncCachePolicy(
IAsyncCacheProvider asyncCacheProvider,
Expand All @@ -22,8 +23,8 @@ internal AsyncCachePolicy(
Action<Context, string> onCacheGet,
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception> onCacheGetError,
Action<Context, string, Exception> onCachePutError)
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError)
{
_asyncCacheProvider = asyncCacheProvider;
_ttlStrategy = ttlStrategy;
Expand Down Expand Up @@ -77,8 +78,8 @@ public class AsyncCachePolicy<TResult> : AsyncPolicy<TResult>
private readonly Action<Context, string> _onCacheGet;
private readonly Action<Context, string> _onCacheMiss;
private readonly Action<Context, string> _onCachePut;
private readonly Action<Context, string, Exception> _onCacheGetError;
private readonly Action<Context, string, Exception> _onCachePutError;
private readonly Action<Context, string, Exception>? _onCacheGetError;
private readonly Action<Context, string, Exception>? _onCachePutError;

internal AsyncCachePolicy(
IAsyncCacheProvider<TResult> asyncCacheProvider,
Expand All @@ -87,8 +88,8 @@ internal AsyncCachePolicy(
Action<Context, string> onCacheGet,
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception> onCacheGetError,
Action<Context, string, Exception> onCachePutError)
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError)
{
_asyncCacheProvider = asyncCacheProvider;
_ttlStrategy = ttlStrategy;
Expand Down
40 changes: 12 additions & 28 deletions src/Polly/Caching/AsyncCacheSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITt
if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy));
if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy));

onCacheError ??= (_, _, _) => { };
Action<Context, string> emptyDelegate = (_, _) => { };

return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError);
Expand Down Expand Up @@ -110,7 +109,6 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITt
if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy));
if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy));

onCacheError ??= (_, _, _) => { };
Action<Context, string> emptyDelegate = (_, _) => { };

return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError);
Expand All @@ -134,16 +132,14 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITt
/// <exception cref="ArgumentNullException">onCacheGet</exception>
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
/// <exception cref="ArgumentNullException">onCachePut</exception>
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
/// <exception cref="ArgumentNullException">onCachePutError</exception>
public static AsyncCachePolicy CacheAsync(
IAsyncCacheProvider cacheProvider,
TimeSpan ttl,
Action<Context, string> onCacheGet,
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception> onCacheGetError,
Action<Context, string, Exception> onCachePutError) =>
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
CacheAsync(cacheProvider, new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);

/// <summary>
Expand All @@ -165,16 +161,14 @@ public static AsyncCachePolicy CacheAsync(
/// <exception cref="ArgumentNullException">onCacheGet</exception>
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
/// <exception cref="ArgumentNullException">onCachePut</exception>
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
/// <exception cref="ArgumentNullException">onCachePutError</exception>
public static AsyncCachePolicy CacheAsync(
IAsyncCacheProvider cacheProvider,
ITtlStrategy ttlStrategy,
Action<Context, string> onCacheGet,
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception> onCacheGetError,
Action<Context, string, Exception> onCachePutError) =>
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
CacheAsync(cacheProvider, ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);

/// <summary>
Expand All @@ -197,17 +191,15 @@ public static AsyncCachePolicy CacheAsync(
/// <exception cref="ArgumentNullException">onCacheGet</exception>
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
/// <exception cref="ArgumentNullException">onCachePut</exception>
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
/// <exception cref="ArgumentNullException">onCachePutError</exception>
public static AsyncCachePolicy CacheAsync(
IAsyncCacheProvider cacheProvider,
TimeSpan ttl,
ICacheKeyStrategy cacheKeyStrategy,
Action<Context, string> onCacheGet,
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception> onCacheGetError,
Action<Context, string, Exception> onCachePutError) =>
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);

/// <summary>
Expand All @@ -231,17 +223,15 @@ public static AsyncCachePolicy CacheAsync(
/// <exception cref="ArgumentNullException">onCacheGet</exception>
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
/// <exception cref="ArgumentNullException">onCachePut</exception>
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
/// <exception cref="ArgumentNullException">onCachePutError</exception>
public static AsyncCachePolicy CacheAsync(
IAsyncCacheProvider cacheProvider,
ITtlStrategy ttlStrategy,
ICacheKeyStrategy cacheKeyStrategy,
Action<Context, string> onCacheGet,
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception> onCacheGetError,
Action<Context, string, Exception> onCachePutError) =>
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);

/// <summary>
Expand All @@ -264,17 +254,15 @@ public static AsyncCachePolicy CacheAsync(
/// <exception cref="ArgumentNullException">onCacheGet</exception>
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
/// <exception cref="ArgumentNullException">onCachePut</exception>
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
/// <exception cref="ArgumentNullException">onCachePutError</exception>
public static AsyncCachePolicy CacheAsync(
IAsyncCacheProvider cacheProvider,
TimeSpan ttl,
Func<Context, string> cacheKeyStrategy,
Action<Context, string> onCacheGet,
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception> onCacheGetError,
Action<Context, string, Exception> onCachePutError) =>
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError) =>
CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);

/// <summary>
Expand All @@ -298,17 +286,15 @@ public static AsyncCachePolicy CacheAsync(
/// <exception cref="ArgumentNullException">onCacheGet</exception>
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
/// <exception cref="ArgumentNullException">onCachePut</exception>
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
/// <exception cref="ArgumentNullException">onCachePutError</exception>
public static AsyncCachePolicy CacheAsync(
IAsyncCacheProvider cacheProvider,
ITtlStrategy ttlStrategy,
Func<Context, string> cacheKeyStrategy,
Action<Context, string> onCacheGet,
Action<Context, string> onCacheMiss,
Action<Context, string> onCachePut,
Action<Context, string, Exception> onCacheGetError,
Action<Context, string, Exception> onCachePutError)
Action<Context, string, Exception>? onCacheGetError,
Action<Context, string, Exception>? onCachePutError)
{
if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider));
if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy));
Expand All @@ -317,8 +303,6 @@ public static AsyncCachePolicy CacheAsync(
if (onCacheGet == null) throw new ArgumentNullException(nameof(onCacheGet));
if (onCacheMiss == null) throw new ArgumentNullException(nameof(onCacheMiss));
if (onCachePut == null) throw new ArgumentNullException(nameof(onCachePut));
if (onCacheGetError == null) throw new ArgumentNullException(nameof(onCacheGetError));
if (onCachePutError == null) throw new ArgumentNullException(nameof(onCachePutError));

return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
}
Expand Down
Loading

0 comments on commit c3e00e5

Please sign in to comment.