Skip to content

Commit 6a0b21e

Browse files
authored
propagate null for error delegates (#1023)
1 parent 7cf4b58 commit 6a0b21e

8 files changed

Lines changed: 112 additions & 222 deletions

src/Polly/Caching/AsyncCacheEngine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ internal static async Task<TResult> ImplementationAsync<TResult>(
1414
Action<Context, string> onCacheGet,
1515
Action<Context, string> onCacheMiss,
1616
Action<Context, string> onCachePut,
17-
Action<Context, string, Exception> onCacheGetError,
18-
Action<Context, string, Exception> onCachePutError)
17+
Action<Context, string, Exception>? onCacheGetError,
18+
Action<Context, string, Exception>? onCachePutError)
1919
{
2020
cancellationToken.ThrowIfCancellationRequested();
2121

@@ -35,7 +35,7 @@ internal static async Task<TResult> ImplementationAsync<TResult>(
3535
{
3636
cacheHit = false;
3737
valueFromCache = default;
38-
onCacheGetError(context, cacheKey, ex);
38+
onCacheGetError?.Invoke(context, cacheKey, ex);
3939
}
4040
if (cacheHit)
4141
{
@@ -59,7 +59,7 @@ internal static async Task<TResult> ImplementationAsync<TResult>(
5959
}
6060
catch (Exception ex)
6161
{
62-
onCachePutError(context, cacheKey, ex);
62+
onCachePutError?.Invoke(context, cacheKey, ex);
6363
}
6464
}
6565

src/Polly/Caching/AsyncCachePolicy.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
namespace Polly.Caching;
1+
#nullable enable
2+
namespace Polly.Caching;
23

34
/// <summary>
45
/// A cache policy that can be applied to the results of delegate executions.
@@ -12,8 +13,8 @@ public class AsyncCachePolicy : AsyncPolicy
1213
private readonly Action<Context, string> _onCacheGet;
1314
private readonly Action<Context, string> _onCacheMiss;
1415
private readonly Action<Context, string> _onCachePut;
15-
private readonly Action<Context, string, Exception> _onCacheGetError;
16-
private readonly Action<Context, string, Exception> _onCachePutError;
16+
private readonly Action<Context, string, Exception>? _onCacheGetError;
17+
private readonly Action<Context, string, Exception>? _onCachePutError;
1718

1819
internal AsyncCachePolicy(
1920
IAsyncCacheProvider asyncCacheProvider,
@@ -22,8 +23,8 @@ internal AsyncCachePolicy(
2223
Action<Context, string> onCacheGet,
2324
Action<Context, string> onCacheMiss,
2425
Action<Context, string> onCachePut,
25-
Action<Context, string, Exception> onCacheGetError,
26-
Action<Context, string, Exception> onCachePutError)
26+
Action<Context, string, Exception>? onCacheGetError,
27+
Action<Context, string, Exception>? onCachePutError)
2728
{
2829
_asyncCacheProvider = asyncCacheProvider;
2930
_ttlStrategy = ttlStrategy;
@@ -77,8 +78,8 @@ public class AsyncCachePolicy<TResult> : AsyncPolicy<TResult>
7778
private readonly Action<Context, string> _onCacheGet;
7879
private readonly Action<Context, string> _onCacheMiss;
7980
private readonly Action<Context, string> _onCachePut;
80-
private readonly Action<Context, string, Exception> _onCacheGetError;
81-
private readonly Action<Context, string, Exception> _onCachePutError;
81+
private readonly Action<Context, string, Exception>? _onCacheGetError;
82+
private readonly Action<Context, string, Exception>? _onCachePutError;
8283

8384
internal AsyncCachePolicy(
8485
IAsyncCacheProvider<TResult> asyncCacheProvider,
@@ -87,8 +88,8 @@ internal AsyncCachePolicy(
8788
Action<Context, string> onCacheGet,
8889
Action<Context, string> onCacheMiss,
8990
Action<Context, string> onCachePut,
90-
Action<Context, string, Exception> onCacheGetError,
91-
Action<Context, string, Exception> onCachePutError)
91+
Action<Context, string, Exception>? onCacheGetError,
92+
Action<Context, string, Exception>? onCachePutError)
9293
{
9394
_asyncCacheProvider = asyncCacheProvider;
9495
_ttlStrategy = ttlStrategy;

src/Polly/Caching/AsyncCacheSyntax.cs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITt
6868
if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy));
6969
if (cacheKeyStrategy == null) throw new ArgumentNullException(nameof(cacheKeyStrategy));
7070

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

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

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

116114
return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, emptyDelegate, emptyDelegate, emptyDelegate, onCacheError, onCacheError);
@@ -134,16 +132,14 @@ public static AsyncCachePolicy CacheAsync(IAsyncCacheProvider cacheProvider, ITt
134132
/// <exception cref="ArgumentNullException">onCacheGet</exception>
135133
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
136134
/// <exception cref="ArgumentNullException">onCachePut</exception>
137-
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
138-
/// <exception cref="ArgumentNullException">onCachePutError</exception>
139135
public static AsyncCachePolicy CacheAsync(
140136
IAsyncCacheProvider cacheProvider,
141137
TimeSpan ttl,
142138
Action<Context, string> onCacheGet,
143139
Action<Context, string> onCacheMiss,
144140
Action<Context, string> onCachePut,
145-
Action<Context, string, Exception> onCacheGetError,
146-
Action<Context, string, Exception> onCachePutError) =>
141+
Action<Context, string, Exception>? onCacheGetError,
142+
Action<Context, string, Exception>? onCachePutError) =>
147143
CacheAsync(cacheProvider, new RelativeTtl(ttl), DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
148144

149145
/// <summary>
@@ -165,16 +161,14 @@ public static AsyncCachePolicy CacheAsync(
165161
/// <exception cref="ArgumentNullException">onCacheGet</exception>
166162
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
167163
/// <exception cref="ArgumentNullException">onCachePut</exception>
168-
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
169-
/// <exception cref="ArgumentNullException">onCachePutError</exception>
170164
public static AsyncCachePolicy CacheAsync(
171165
IAsyncCacheProvider cacheProvider,
172166
ITtlStrategy ttlStrategy,
173167
Action<Context, string> onCacheGet,
174168
Action<Context, string> onCacheMiss,
175169
Action<Context, string> onCachePut,
176-
Action<Context, string, Exception> onCacheGetError,
177-
Action<Context, string, Exception> onCachePutError) =>
170+
Action<Context, string, Exception>? onCacheGetError,
171+
Action<Context, string, Exception>? onCachePutError) =>
178172
CacheAsync(cacheProvider, ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
179173

180174
/// <summary>
@@ -197,17 +191,15 @@ public static AsyncCachePolicy CacheAsync(
197191
/// <exception cref="ArgumentNullException">onCacheGet</exception>
198192
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
199193
/// <exception cref="ArgumentNullException">onCachePut</exception>
200-
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
201-
/// <exception cref="ArgumentNullException">onCachePutError</exception>
202194
public static AsyncCachePolicy CacheAsync(
203195
IAsyncCacheProvider cacheProvider,
204196
TimeSpan ttl,
205197
ICacheKeyStrategy cacheKeyStrategy,
206198
Action<Context, string> onCacheGet,
207199
Action<Context, string> onCacheMiss,
208200
Action<Context, string> onCachePut,
209-
Action<Context, string, Exception> onCacheGetError,
210-
Action<Context, string, Exception> onCachePutError) =>
201+
Action<Context, string, Exception>? onCacheGetError,
202+
Action<Context, string, Exception>? onCachePutError) =>
211203
CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
212204

213205
/// <summary>
@@ -231,17 +223,15 @@ public static AsyncCachePolicy CacheAsync(
231223
/// <exception cref="ArgumentNullException">onCacheGet</exception>
232224
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
233225
/// <exception cref="ArgumentNullException">onCachePut</exception>
234-
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
235-
/// <exception cref="ArgumentNullException">onCachePutError</exception>
236226
public static AsyncCachePolicy CacheAsync(
237227
IAsyncCacheProvider cacheProvider,
238228
ITtlStrategy ttlStrategy,
239229
ICacheKeyStrategy cacheKeyStrategy,
240230
Action<Context, string> onCacheGet,
241231
Action<Context, string> onCacheMiss,
242232
Action<Context, string> onCachePut,
243-
Action<Context, string, Exception> onCacheGetError,
244-
Action<Context, string, Exception> onCachePutError) =>
233+
Action<Context, string, Exception>? onCacheGetError,
234+
Action<Context, string, Exception>? onCachePutError) =>
245235
CacheAsync(cacheProvider, ttlStrategy, cacheKeyStrategy.GetCacheKey, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
246236

247237
/// <summary>
@@ -264,17 +254,15 @@ public static AsyncCachePolicy CacheAsync(
264254
/// <exception cref="ArgumentNullException">onCacheGet</exception>
265255
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
266256
/// <exception cref="ArgumentNullException">onCachePut</exception>
267-
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
268-
/// <exception cref="ArgumentNullException">onCachePutError</exception>
269257
public static AsyncCachePolicy CacheAsync(
270258
IAsyncCacheProvider cacheProvider,
271259
TimeSpan ttl,
272260
Func<Context, string> cacheKeyStrategy,
273261
Action<Context, string> onCacheGet,
274262
Action<Context, string> onCacheMiss,
275263
Action<Context, string> onCachePut,
276-
Action<Context, string, Exception> onCacheGetError,
277-
Action<Context, string, Exception> onCachePutError) =>
264+
Action<Context, string, Exception>? onCacheGetError,
265+
Action<Context, string, Exception>? onCachePutError) =>
278266
CacheAsync(cacheProvider, new RelativeTtl(ttl), cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
279267

280268
/// <summary>
@@ -298,17 +286,15 @@ public static AsyncCachePolicy CacheAsync(
298286
/// <exception cref="ArgumentNullException">onCacheGet</exception>
299287
/// <exception cref="ArgumentNullException">onCacheMiss</exception>
300288
/// <exception cref="ArgumentNullException">onCachePut</exception>
301-
/// <exception cref="ArgumentNullException">onCacheGetError</exception>
302-
/// <exception cref="ArgumentNullException">onCachePutError</exception>
303289
public static AsyncCachePolicy CacheAsync(
304290
IAsyncCacheProvider cacheProvider,
305291
ITtlStrategy ttlStrategy,
306292
Func<Context, string> cacheKeyStrategy,
307293
Action<Context, string> onCacheGet,
308294
Action<Context, string> onCacheMiss,
309295
Action<Context, string> onCachePut,
310-
Action<Context, string, Exception> onCacheGetError,
311-
Action<Context, string, Exception> onCachePutError)
296+
Action<Context, string, Exception>? onCacheGetError,
297+
Action<Context, string, Exception>? onCachePutError)
312298
{
313299
if (cacheProvider == null) throw new ArgumentNullException(nameof(cacheProvider));
314300
if (ttlStrategy == null) throw new ArgumentNullException(nameof(ttlStrategy));
@@ -317,8 +303,6 @@ public static AsyncCachePolicy CacheAsync(
317303
if (onCacheGet == null) throw new ArgumentNullException(nameof(onCacheGet));
318304
if (onCacheMiss == null) throw new ArgumentNullException(nameof(onCacheMiss));
319305
if (onCachePut == null) throw new ArgumentNullException(nameof(onCachePut));
320-
if (onCacheGetError == null) throw new ArgumentNullException(nameof(onCacheGetError));
321-
if (onCachePutError == null) throw new ArgumentNullException(nameof(onCachePutError));
322306

323307
return new AsyncCachePolicy(cacheProvider, ttlStrategy, cacheKeyStrategy, onCacheGet, onCacheMiss, onCachePut, onCacheGetError, onCachePutError);
324308
}

0 commit comments

Comments
 (0)