Skip to content

Commit dec0b02

Browse files
authored
Merge pull request #487 from App-vNext/v610dev
Merge v6.1.0 to master
2 parents 9947e06 + 21130f8 commit dec0b02

File tree

52 files changed

+2502
-150
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2502
-150
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 6.1.0
2+
- Bug Fix: Context.PolicyKey behaviour in PolicyWrap (issue 463)
3+
- Bug Fix: CachePolicy behaviour with non-nullable types (issues 472, 475)
4+
- Enhancement: WaitAnd/RetryForever overloads where onRetry takes the retry number as a parameter (issue 451)
5+
- Enhancement: Overloads where onTimeout takes thrown exception as a parameter (issue 338)
6+
- Enhancement: Improved cache error message (issue 455)
7+
18
## 6.0.1
29
- Version 6 RTM, for integration to ASPNET Core 2.1 IHttpClientFactory
310

GitVersionConfig.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
next-version: 6.0.1
1+
next-version: 6.1.0

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ Policy
309309
});
310310
```
311311

312-
For more detail see: [Retry policy documentation](https://github.com/App-vNext/Polly/wiki/Retry) on wiki.
312+
If all retries fail, a retry policy rethrows the final exception back to the calling code.
313+
314+
For more depth see also: [Retry policy documentation on wiki](https://github.com/App-vNext/Polly/wiki/Retry).
313315

314316
### Circuit Breaker
315317

@@ -355,7 +357,10 @@ breaker.Isolate();
355357
breaker.Reset();
356358

357359
```
358-
For more detail see: [Circuit-Breaker documentation](https://github.com/App-vNext/Polly/wiki/Circuit-Breaker) on wiki.
360+
361+
Note that circuit-breaker policies [rethrow all exceptions](https://github.com/App-vNext/Polly/wiki/Circuit-Breaker#exception-handling), even handled ones. A circuit-breaker exists to measure faults and break the circuit when too many faults occur, but does not orchestrate retries. Combine a circuit-breaker with a retry policy as needed.
362+
363+
For more depth see also: [Circuit-Breaker documentation on wiki](https://github.com/App-vNext/Polly/wiki/Circuit-Breaker).
359364

360365
### Advanced Circuit Breaker
361366
```csharp
@@ -589,22 +594,27 @@ For more detail see: [Bulkhead policy documentation](https://github.com/App-vNex
589594
var memoryCacheProvider = new MemoryCacheProvider(MemoryCache.Default);
590595
var cachePolicy = Policy.Cache(memoryCacheProvider, TimeSpan.FromMinutes(5));
591596

597+
// For .NET Core examples see the CacheProviders linked to from https://github.com/App-vNext/Polly/wiki/Cache#working-with-cacheproviders :
598+
// - https://github.com/App-vNext/Polly.Caching.MemoryCache
599+
// - https://github.com/App-vNext/Polly.Caching.IDistributedCache
600+
592601
// Define a cache policy with absolute expiration at midnight tonight.
593602
var cachePolicy = Policy.Cache(memoryCacheProvider, new AbsoluteTtl(DateTimeOffset.Now.Date.AddDays(1));
594603

595604
// Define a cache policy with sliding expiration: items remain valid for another 5 minutes each time the cache item is used.
596605
var cachePolicy = Policy.Cache(memoryCacheProvider, new SlidingTtl(TimeSpan.FromMinutes(5));
597606

598-
// Execute through the cache as a read-through cache: check the cache first; if not found, execute underlying delegate and store the result in the cache.
599-
TResult result = cachePolicy.Execute(() => getFoo(), new Context("FooKey")); // "FooKey" is the cache key used in this execution.
600-
601607
// Define a cache Policy, and catch any cache provider errors for logging.
602608
var cachePolicy = Policy.Cache(myCacheProvider, TimeSpan.FromMinutes(5),
603609
(context, key, ex) => {
604610
logger.Error($"Cache provider, for key {key}, threw exception: {ex}."); // (for example)
605611
}
606612
);
607613

614+
// Execute through the cache as a read-through cache: check the cache first; if not found, execute underlying delegate and store the result in the cache.
615+
// The key to use for caching, for a particular execution, is specified by setting the OperationKey (before v6: ExecutionKey) on a Context instance passed to the execution. Use an overload of the form shown below (or a richer overload including the same elements).
616+
// Example: "FooKey" is the cache key that will be used in the below execution.
617+
TResult result = cachePolicy.Execute(context => getFoo(), new Context("FooKey"));
608618

609619
```
610620

@@ -772,7 +782,7 @@ var policy = Policy
772782
.WithPolicyKey("MyDataAccessPolicy");
773783

774784
int id = ... // customer id from somewhere
775-
var customerDetails = policy.Execute(() => GetCustomer(id),
785+
var customerDetails = policy.Execute(context => GetCustomer(id),
776786
new Context("GetCustomerDetails", new Dictionary<string, object>() {{"Type","Customer"},{"Id",id}}
777787
));
778788
```
@@ -980,6 +990,8 @@ For full detailed of supported targets by version, see [supported targets](https
980990
* [@benagain](https://github.com/benagain) - Bug fix: RelativeTtl in CachePolicy now always returns a ttl relative to time item is cached.
981991
* [@urig](https://github.com/urig) - Allow TimeoutPolicy to be configured with Timeout.InfiniteTimeSpan.
982992
* [@reisenberger](https://github.com/reisenberger) - Integration with [IHttpClientFactory](https://github.com/aspnet/HttpClientFactory/) for ASPNET Core 2.1.
993+
* [@freakazoid182](https://github.com/Freakazoid182) - WaitAnd/RetryForever overloads where onRetry takes the retry number as a parameter.
994+
* [@dustyhoppe](https://github.com/dustyhoppe) - Overloads where onTimeout takes thrown exception as a parameter
983995
984996
# Sample Projects
985997

src/Polly.NetStandard11/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
using System.Runtime.CompilerServices;
44

55
[assembly: AssemblyTitle("Polly")]
6-
[assembly: AssemblyInformationalVersion("6.0.1.0")]
7-
[assembly: AssemblyFileVersion("6.0.1.0")]
6+
[assembly: AssemblyInformationalVersion("6.1.0.0")]
7+
[assembly: AssemblyFileVersion("6.1.0.0")]
88
[assembly: AssemblyVersion("6.0.0.0")]
99
[assembly: CLSCompliant(true)]
1010

src/Polly.NetStandard20/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
using System.Runtime.CompilerServices;
44

55
[assembly: AssemblyTitle("Polly")]
6-
[assembly: AssemblyInformationalVersion("6.0.1.0")]
7-
[assembly: AssemblyFileVersion("6.0.1.0")]
6+
[assembly: AssemblyInformationalVersion("6.1.0.0")]
7+
[assembly: AssemblyFileVersion("6.1.0.0")]
88
[assembly: AssemblyVersion("6.0.0.0")]
99
[assembly: CLSCompliant(true)]
1010

src/Polly.Shared/Caching/CachePolicy.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ internal CachePolicy(
5555
[DebuggerStepThrough]
5656
internal override TResult ExecuteInternal<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
5757
{
58+
if (_syncCacheProvider == null) throw new InvalidOperationException("Please use the synchronous-defined policies when calling the synchronous Execute (and similar) methods.");
59+
5860
return CacheEngine.Implementation<TResult>(
5961
_syncCacheProvider.For<TResult>(),
6062
_ttlStrategy.For<TResult>(),

src/Polly.Shared/Caching/CachePolicyAsync.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ internal CachePolicy(
4747
[DebuggerStepThrough]
4848
internal override Task<TResult> ExecuteAsyncInternal<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
4949
{
50+
if (_asyncCacheProvider == null) throw new InvalidOperationException("Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods.");
51+
5052
return CacheEngine.ImplementationAsync<TResult>(
5153
_asyncCacheProvider.AsyncFor<TResult>(),
5254
_ttlStrategy.For<TResult>(),

src/Polly.Shared/Caching/GenericCacheProvider.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ internal class GenericCacheProvider<TCacheFormat> : ISyncCacheProvider<TCacheFor
1212

1313
internal GenericCacheProvider(ISyncCacheProvider nonGenericCacheProvider)
1414
{
15-
if (nonGenericCacheProvider == null) throw new ArgumentNullException(nameof(nonGenericCacheProvider));
16-
17-
_wrappedCacheProvider = nonGenericCacheProvider;
15+
_wrappedCacheProvider = nonGenericCacheProvider ?? throw new ArgumentNullException(nameof(nonGenericCacheProvider));
1816
}
1917

2018
TCacheFormat ISyncCacheProvider<TCacheFormat>.Get(string key)
2119
{
22-
return (TCacheFormat) _wrappedCacheProvider.Get(key);
20+
return (TCacheFormat) (_wrappedCacheProvider.Get(key) ?? default(TCacheFormat));
2321
}
2422

2523
void ISyncCacheProvider<TCacheFormat>.Put(string key, TCacheFormat value, Ttl ttl)

src/Polly.Shared/Caching/GenericCacheProviderAsync.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ internal class GenericCacheProviderAsync<TCacheFormat> : IAsyncCacheProvider<TCa
1414

1515
internal GenericCacheProviderAsync(IAsyncCacheProvider nonGenericCacheProvider)
1616
{
17-
if (nonGenericCacheProvider == null) throw new ArgumentNullException(nameof(nonGenericCacheProvider));
18-
19-
_wrappedCacheProvider = nonGenericCacheProvider;
17+
_wrappedCacheProvider = nonGenericCacheProvider ?? throw new ArgumentNullException(nameof(nonGenericCacheProvider));
2018
}
2119

2220
async Task<TCacheFormat> IAsyncCacheProvider<TCacheFormat>.GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
2321
{
24-
return (TCacheFormat) await _wrappedCacheProvider.GetAsync(key, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext);
22+
return (TCacheFormat) (await _wrappedCacheProvider.GetAsync(key, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext) ?? default(TCacheFormat));
2523
}
2624

2725
Task IAsyncCacheProvider<TCacheFormat>.PutAsync(string key, TCacheFormat value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)

src/Polly.Shared/Caching/SerializingCacheProvider.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ public class SerializingCacheProvider<TSerialized> : ISyncCacheProvider
2020
/// <exception cref="System.ArgumentNullException">serializer </exception>
2121
public SerializingCacheProvider(ISyncCacheProvider<TSerialized> wrappedCacheProvider, ICacheItemSerializer<object, TSerialized> serializer)
2222
{
23-
if (wrappedCacheProvider == null) throw new ArgumentNullException(nameof(wrappedCacheProvider));
24-
if (serializer == null) throw new ArgumentNullException(nameof(serializer));
25-
26-
_wrappedCacheProvider = wrappedCacheProvider;
27-
_serializer = serializer;
23+
_wrappedCacheProvider = wrappedCacheProvider ?? throw new ArgumentNullException(nameof(wrappedCacheProvider));
24+
_serializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
2825
}
2926

3027
/// <summary>
@@ -34,7 +31,8 @@ public SerializingCacheProvider(ISyncCacheProvider<TSerialized> wrappedCacheProv
3431
/// <returns>The value from cache; or null, if none was found.</returns>
3532
public object Get(string key)
3633
{
37-
return _serializer.Deserialize(_wrappedCacheProvider.Get(key));
34+
TSerialized objectToDeserialize = _wrappedCacheProvider.Get(key);
35+
return objectToDeserialize == null || objectToDeserialize.Equals(default(TSerialized)) ? null : _serializer.Deserialize(objectToDeserialize);
3836
}
3937

4038
/// <summary>
@@ -45,7 +43,8 @@ public object Get(string key)
4543
/// <param name="ttl">The time-to-live for the cache entry.</param>
4644
public void Put(string key, object value, Ttl ttl)
4745
{
48-
_wrappedCacheProvider.Put(key, _serializer.Serialize(value), ttl);
46+
if (value != null)
47+
_wrappedCacheProvider.Put(key, _serializer.Serialize(value), ttl);
4948
}
5049

5150
}
@@ -69,11 +68,8 @@ public class SerializingCacheProvider<TResult, TSerialized> : ISyncCacheProvider
6968
/// <exception cref="System.ArgumentNullException">serializer </exception>
7069
public SerializingCacheProvider(ISyncCacheProvider<TSerialized> wrappedCacheProvider, ICacheItemSerializer<TResult, TSerialized> serializer)
7170
{
72-
if (wrappedCacheProvider == null) throw new ArgumentNullException(nameof(wrappedCacheProvider));
73-
if (serializer == null) throw new ArgumentNullException(nameof(serializer));
74-
75-
_wrappedCacheProvider = wrappedCacheProvider;
76-
_serializer = serializer;
71+
_wrappedCacheProvider = wrappedCacheProvider ?? throw new ArgumentNullException(nameof(wrappedCacheProvider));
72+
_serializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
7773
}
7874

7975
/// <summary>
@@ -83,7 +79,8 @@ public SerializingCacheProvider(ISyncCacheProvider<TSerialized> wrappedCacheProv
8379
/// <returns>The value from cache; or null, if none was found.</returns>
8480
public TResult Get(string key)
8581
{
86-
return _serializer.Deserialize(_wrappedCacheProvider.Get(key));
82+
TSerialized objectToDeserialize = _wrappedCacheProvider.Get(key);
83+
return objectToDeserialize == null || objectToDeserialize.Equals(default(TSerialized)) ? default(TResult) : _serializer.Deserialize(objectToDeserialize);
8784
}
8885

8986
/// <summary>
@@ -94,7 +91,8 @@ public TResult Get(string key)
9491
/// <param name="ttl">The time-to-live for the cache entry.</param>
9592
public void Put(string key, TResult value, Ttl ttl)
9693
{
97-
_wrappedCacheProvider.Put(key, _serializer.Serialize(value), ttl);
94+
if (value != null && !value.Equals(default(TResult)))
95+
_wrappedCacheProvider.Put(key, _serializer.Serialize(value), ttl);
9896
}
9997

10098
}

0 commit comments

Comments
 (0)