diff --git a/src/Polly/Caching/AsyncCacheEngine.cs b/src/Polly/Caching/AsyncCacheEngine.cs index 1e4657e4c84..4ef9dcc1c09 100644 --- a/src/Polly/Caching/AsyncCacheEngine.cs +++ b/src/Polly/Caching/AsyncCacheEngine.cs @@ -14,8 +14,8 @@ internal static async Task ImplementationAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { cancellationToken.ThrowIfCancellationRequested(); @@ -35,7 +35,7 @@ internal static async Task ImplementationAsync( { cacheHit = false; valueFromCache = default; - onCacheGetError(context, cacheKey, ex); + onCacheGetError?.Invoke(context, cacheKey, ex); } if (cacheHit) { @@ -59,7 +59,7 @@ internal static async Task ImplementationAsync( } catch (Exception ex) { - onCachePutError(context, cacheKey, ex); + onCachePutError?.Invoke(context, cacheKey, ex); } } diff --git a/src/Polly/Caching/AsyncCachePolicy.cs b/src/Polly/Caching/AsyncCachePolicy.cs index c4fccfa9809..a79aeafe486 100644 --- a/src/Polly/Caching/AsyncCachePolicy.cs +++ b/src/Polly/Caching/AsyncCachePolicy.cs @@ -1,4 +1,5 @@ -namespace Polly.Caching; +#nullable enable +namespace Polly.Caching; /// /// A cache policy that can be applied to the results of delegate executions. @@ -12,8 +13,8 @@ public class AsyncCachePolicy : AsyncPolicy private readonly Action _onCacheGet; private readonly Action _onCacheMiss; private readonly Action _onCachePut; - private readonly Action _onCacheGetError; - private readonly Action _onCachePutError; + private readonly Action? _onCacheGetError; + private readonly Action? _onCachePutError; internal AsyncCachePolicy( IAsyncCacheProvider asyncCacheProvider, @@ -22,8 +23,8 @@ internal AsyncCachePolicy( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { _asyncCacheProvider = asyncCacheProvider; _ttlStrategy = ttlStrategy; @@ -77,8 +78,8 @@ public class AsyncCachePolicy : AsyncPolicy private readonly Action _onCacheGet; private readonly Action _onCacheMiss; private readonly Action _onCachePut; - private readonly Action _onCacheGetError; - private readonly Action _onCachePutError; + private readonly Action? _onCacheGetError; + private readonly Action? _onCachePutError; internal AsyncCachePolicy( IAsyncCacheProvider asyncCacheProvider, @@ -87,8 +88,8 @@ internal AsyncCachePolicy( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { _asyncCacheProvider = asyncCacheProvider; _ttlStrategy = ttlStrategy; diff --git a/src/Polly/Caching/AsyncCacheSyntax.cs b/src/Polly/Caching/AsyncCacheSyntax.cs index ec7a9e9375a..7353ad46939 100644 --- a/src/Polly/Caching/AsyncCacheSyntax.cs +++ b/src/Polly/Caching/AsyncCacheSyntax.cs @@ -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 emptyDelegate = (_, _) => { }; return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); @@ -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 emptyDelegate = (_, _) => { }; return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); @@ -134,16 +132,14 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITt /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, TimeSpan ttl, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -165,16 +161,14 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -197,8 +191,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, TimeSpan ttl, @@ -206,8 +198,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -231,8 +223,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -240,8 +230,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -264,8 +254,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, TimeSpan ttl, @@ -273,8 +261,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -298,8 +286,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -307,8 +293,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); @@ -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); } diff --git a/src/Polly/Caching/AsyncCacheTResultSyntax.cs b/src/Polly/Caching/AsyncCacheTResultSyntax.cs index e0d859986a1..da92d36c82a 100644 --- a/src/Polly/Caching/AsyncCacheTResultSyntax.cs +++ b/src/Polly/Caching/AsyncCacheTResultSyntax.cs @@ -140,16 +140,14 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, TimeSpan ttl, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -175,16 +173,14 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -211,8 +207,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, TimeSpan ttl, @@ -220,8 +214,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -249,8 +243,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -258,8 +250,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -286,8 +278,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, TimeSpan ttl, @@ -295,8 +285,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -324,8 +314,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -333,8 +321,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -417,8 +405,6 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider< /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - onCacheError ??= (_, _, _) => { }; - Action emptyDelegate = (_, _) => { }; return CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); @@ -440,8 +426,6 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider< /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - onCacheError ??= (_, _, _) => { }; - Action emptyDelegate = (_, _) => { }; return CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); @@ -479,8 +463,6 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider< /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - onCacheError ??= (_, _, _) => { }; - Action emptyDelegate = (_, _) => { }; return CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); @@ -502,8 +484,6 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider< /// cacheKeyStrategy public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - onCacheError ??= (_, _, _) => { }; - Action emptyDelegate = (_, _) => { }; return CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); @@ -527,16 +507,14 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider< /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, TimeSpan ttl, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); @@ -559,16 +537,14 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, ttlStrategy.For(), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -590,16 +566,14 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -622,8 +596,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, TimeSpan ttl, @@ -631,8 +603,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); @@ -657,8 +629,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -666,8 +636,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, ttlStrategy.For(), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -691,8 +661,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -700,8 +668,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -724,8 +692,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, TimeSpan ttl, @@ -733,8 +699,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); @@ -759,8 +725,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -768,8 +732,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => CacheAsync(cacheProvider, ttlStrategy.For(), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -793,8 +757,6 @@ public static AsyncCachePolicy CacheAsync( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static AsyncCachePolicy CacheAsync( IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -802,8 +764,8 @@ public static AsyncCachePolicy CacheAsync( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); @@ -812,8 +774,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); } diff --git a/src/Polly/Caching/CacheEngine.cs b/src/Polly/Caching/CacheEngine.cs index 493b5207d4a..ca2a1a469e1 100644 --- a/src/Polly/Caching/CacheEngine.cs +++ b/src/Polly/Caching/CacheEngine.cs @@ -13,8 +13,8 @@ internal static TResult Implementation( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { cancellationToken.ThrowIfCancellationRequested(); @@ -34,7 +34,7 @@ internal static TResult Implementation( { cacheHit = false; valueFromCache = default; - onCacheGetError(context, cacheKey, ex); + onCacheGetError?.Invoke(context, cacheKey, ex); } if (cacheHit) { @@ -58,7 +58,7 @@ internal static TResult Implementation( } catch (Exception ex) { - onCachePutError(context, cacheKey, ex); + onCachePutError?.Invoke(context, cacheKey, ex); } } diff --git a/src/Polly/Caching/CachePolicy.cs b/src/Polly/Caching/CachePolicy.cs index e775ce62a0f..762bcf9273d 100644 --- a/src/Polly/Caching/CachePolicy.cs +++ b/src/Polly/Caching/CachePolicy.cs @@ -1,4 +1,5 @@ -namespace Polly.Caching; +#nullable enable +namespace Polly.Caching; /// /// A cache policy that can be applied to the results of delegate executions. @@ -12,8 +13,8 @@ public class CachePolicy : Policy, ICachePolicy private readonly Action _onCacheGet; private readonly Action _onCacheMiss; private readonly Action _onCachePut; - private readonly Action _onCacheGetError; - private readonly Action _onCachePutError; + private readonly Action? _onCacheGetError; + private readonly Action? _onCachePutError; internal CachePolicy( ISyncCacheProvider syncCacheProvider, @@ -22,8 +23,8 @@ internal CachePolicy( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { _syncCacheProvider = syncCacheProvider; _ttlStrategy = ttlStrategy; @@ -70,8 +71,8 @@ public class CachePolicy : Policy, ICachePolicy private readonly Action _onCacheGet; private readonly Action _onCacheMiss; private readonly Action _onCachePut; - private readonly Action _onCacheGetError; - private readonly Action _onCachePutError; + private readonly Action? _onCacheGetError; + private readonly Action? _onCachePutError; internal CachePolicy( ISyncCacheProvider syncCacheProvider, @@ -80,8 +81,8 @@ internal CachePolicy( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { _syncCacheProvider = syncCacheProvider; _ttlStrategy = ttlStrategy; diff --git a/src/Polly/Caching/CacheSyntax.cs b/src/Polly/Caching/CacheSyntax.cs index b02af77a3b2..d104ed2d98a 100644 --- a/src/Polly/Caching/CacheSyntax.cs +++ b/src/Polly/Caching/CacheSyntax.cs @@ -68,7 +68,6 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy t if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); - onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); @@ -110,7 +109,6 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy t if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy)); - onCacheError ??= (_, _, _) => { }; Action emptyDelegate = (_, _) => { }; return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError); @@ -136,16 +134,14 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy t /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, TimeSpan ttl, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -169,16 +165,14 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -203,8 +197,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, TimeSpan ttl, @@ -212,8 +204,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -239,8 +231,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -248,8 +238,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -274,8 +264,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, TimeSpan ttl, @@ -283,8 +271,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -310,8 +298,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -319,8 +305,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); @@ -329,8 +315,6 @@ public static CachePolicy Cache( 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 CachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); } diff --git a/src/Polly/Caching/CacheTResultSyntax.cs b/src/Polly/Caching/CacheTResultSyntax.cs index de9c80606ef..9e3ed6666d0 100644 --- a/src/Polly/Caching/CacheTResultSyntax.cs +++ b/src/Polly/Caching/CacheTResultSyntax.cs @@ -140,16 +140,14 @@ public static CachePolicy Cache(ISyncCacheProvider cacheProvid /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, TimeSpan ttl, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -175,16 +173,14 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -211,8 +207,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, TimeSpan ttl, @@ -220,8 +214,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -249,8 +243,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -258,8 +250,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -286,8 +278,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, TimeSpan ttl, @@ -295,8 +285,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -324,8 +314,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -333,8 +321,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); @@ -417,8 +405,6 @@ public static CachePolicy Cache(ISyncCacheProvider ca /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - onCacheError ??= (_, _, _) => { }; - Action emptyDelegate = (_, _) => { }; return Cache(cacheProvider, ttlStrategy.For(), cacheKeyStrategy.GetCacheKey, @@ -441,8 +427,6 @@ public static CachePolicy Cache(ISyncCacheProvider ca /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, ICacheKeyStrategy cacheKeyStrategy, Action? onCacheError = null) { - onCacheError ??= (_, _, _) => { }; - Action emptyDelegate = (_, _) => { }; return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, @@ -481,8 +465,6 @@ public static CachePolicy Cache(ISyncCacheProvider ca /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - onCacheError ??= (_, _, _) => { }; - Action emptyDelegate = (_, _) => { }; return Cache(cacheProvider, ttlStrategy.For(), cacheKeyStrategy, @@ -505,8 +487,6 @@ public static CachePolicy Cache(ISyncCacheProvider ca /// cacheKeyStrategy public static CachePolicy Cache(ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Func cacheKeyStrategy, Action? onCacheError = null) { - onCacheError ??= (_, _, _) => { }; - Action emptyDelegate = (_, _) => { }; return Cache(cacheProvider, ttlStrategy, cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, @@ -531,16 +511,14 @@ public static CachePolicy Cache(ISyncCacheProvider ca /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, TimeSpan ttl, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); @@ -563,16 +541,14 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -594,16 +570,14 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -626,8 +600,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, TimeSpan ttl, @@ -635,8 +607,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); @@ -661,8 +633,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -670,8 +640,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -695,8 +665,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -704,8 +672,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); /// @@ -728,8 +696,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, TimeSpan ttl, @@ -737,8 +703,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); @@ -763,8 +729,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -772,8 +736,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) => + Action? onCacheGetError, + Action? onCachePutError) => Cache(cacheProvider, ttlStrategy.For(), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); @@ -798,8 +762,6 @@ public static CachePolicy Cache( /// onCacheGet /// onCacheMiss /// onCachePut - /// onCacheGetError - /// onCachePutError public static CachePolicy Cache( ISyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, @@ -807,8 +769,8 @@ public static CachePolicy Cache( Action onCacheGet, Action onCacheMiss, Action onCachePut, - Action onCacheGetError, - Action onCachePutError) + Action? onCacheGetError, + Action? onCachePutError) { if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider)); if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy)); @@ -817,8 +779,6 @@ public static CachePolicy Cache( 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 CachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError); }