Skip to content

Commit 4f83e62

Browse files
committed
Migrate distributed cache provider implementations to Polly v7.0.0.
1 parent 1b20e94 commit 4f83e62

10 files changed

Lines changed: 165 additions & 105 deletions

src/Polly.Caching.Distributed.NetStandard11.Specs/Polly.Caching.Distributed.NetStandard11.Specs.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
<PackageReference Include="FluentAssertions" Version="4.19.3" />
1919
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
2020
<PackageReference Include="Moq" Version="4.7.145" />
21+
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
2122
<PackageReference Include="xunit" Version="2.2.0" />
2223
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
23-
<PackageReference Include="Polly" Version="6.0.1" />
24+
<PackageReference Include="Polly" Version="7.0.0" />
2425

2526
</ItemGroup>
2627
<ItemGroup>

src/Polly.Caching.Distributed.NetStandard11/Polly.Caching.Distributed.NetStandard11.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
</ItemGroup>
3232
<ItemGroup>
3333
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="1.1.2" />
34-
<PackageReference Include="Polly" Version="6.0.1" />
34+
<PackageReference Include="Polly" Version="7.0.0" />
35+
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
3536
</ItemGroup>
3637
<Import Project="..\Polly.Caching.Distributed.Shared\Polly.Caching.Distributed.Shared.projitems" Label="Shared" />
3738
</Project>

src/Polly.Caching.Distributed.NetStandard20.Specs/Polly.Caching.Distributed.NetStandard20.Specs.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.2" />
2020
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
2121
<PackageReference Include="Moq" Version="4.7.145" />
22-
<PackageReference Include="Polly.Caching.Serialization.Json" Version="2.0.0" />
22+
<PackageReference Include="Polly.Caching.Serialization.Json" Version="3.0.0" />
2323
<PackageReference Include="xunit" Version="2.2.0" />
2424
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
25-
<PackageReference Include="Polly" Version="6.0.1" />
25+
<PackageReference Include="Polly" Version="7.0.0" />
2626

2727
</ItemGroup>
2828
<ItemGroup>

src/Polly.Caching.Distributed.NetStandard20/Polly.Caching.Distributed.NetStandard20.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</ItemGroup>
3232
<ItemGroup>
3333
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.0.2" />
34-
<PackageReference Include="Polly" Version="6.0.1" />
34+
<PackageReference Include="Polly" Version="7.0.0" />
3535
</ItemGroup>
3636
<Import Project="..\Polly.Caching.Distributed.Shared\Polly.Caching.Distributed.Shared.projitems" Label="Shared" />
3737
</Project>

src/Polly.Caching.Distributed.Shared/NetStandardIDistributedCacheByteArrayProvider.cs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ namespace Polly.Caching.Distributed
99
/// </summary>
1010
public class NetStandardIDistributedCacheByteArrayProvider : NetStandardIDistributedCacheProvider<byte[]>
1111
{
12-
private readonly byte[] Empty = new byte[0];
13-
1412
/// <summary>
1513
/// Initializes a new instance of the <see cref="NetStandardIDistributedCacheByteArrayProvider"/> class.
1614
/// </summary>
@@ -23,13 +21,16 @@ public NetStandardIDistributedCacheByteArrayProvider(Microsoft.Extensions.Cachin
2321
/// Gets a value from cache.
2422
/// </summary>
2523
/// <param name="key">The cache key.</param>
26-
/// <returns>The value from cache; or null, if none was found.</returns>
27-
public override byte[] Get(String key)
24+
/// <returns>
25+
/// A tuple whose first element is a bool indicating whether the key was found in the cache,
26+
/// and whose second element is the value from the cache.
27+
/// </returns>
28+
public override (bool, byte[]) TryGet(string key)
2829
{
29-
byte[] returned = _cache.Get(key);
30-
return returned == null || returned.Length == 0 ? null : returned; // Because Polly CachePolicy expects providers to return "no value held" as null.
30+
byte[] fromCache = _cache.Get(key);
31+
return (fromCache != null, fromCache);
3132
}
32-
33+
3334
/// <summary>
3435
/// Puts the specified value in the cache.
3536
/// </summary>
@@ -38,26 +39,33 @@ public override byte[] Get(String key)
3839
/// <param name="ttl">The time-to-live for the cache entry.</param>
3940
public override void Put(string key, byte[] value, Ttl ttl)
4041
{
41-
_cache.Set(key, value ?? Empty, ttl.ToDistributedCacheEntryOptions());
42+
_cache.Set(key, value, ttl.ToDistributedCacheEntryOptions());
4243
}
4344

4445
/// <summary>
45-
/// Gets a value from the memory cache as part of an asynchronous execution. <para><remarks>The implementation is synchronous as there is no advantage to an asynchronous implementation for an in-memory cache.</remarks></para>
46+
/// Gets a value from the memory cache as part of an asynchronous execution.
4647
/// </summary>
4748
/// <param name="key">The cache key.</param>
4849
/// <param name="cancellationToken">The cancellation token. </param>
49-
/// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context. <para><remarks>For <see cref="NetStandardIDistributedCacheByteArrayProvider"/>, this parameter is irrelevant and is ignored, as the Microsoft.Extensions.Caching.Distributed.IDistributedCache interface does not support it.</remarks></para></param>
50-
/// <returns>A <see cref="Task{TResult}" /> promising as Result the value from cache; or null, if none was found.</returns>
51-
public override async Task<byte[]> GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
50+
/// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context. <para><remarks>For <see cref="NetStandardIDistributedCacheProvider{TCache}"/>, this parameter is irrelevant and is ignored, as the Microsoft.Extensions.Caching.Distributed.IDistributedCache interface does not support it.</remarks></para></param>
51+
/// <returns>
52+
/// A <see cref="Task{TResult}" /> promising as Result a tuple whose first element is a value indicating whether
53+
/// the key was found in the cache, and whose second element is the value from the cache.
54+
/// </returns>
55+
public override async Task<(bool, byte[])> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
5256
{
5357
cancellationToken.ThrowIfCancellationRequested();
54-
byte[] returned = await _cache.GetAsync(key);
55-
return returned == null || returned.Length == 0 ? null : returned; // Because Polly CachePolicy expects providers to return "no value held" as null.
58+
59+
byte[] fromCache = await _cache.GetAsync(key
60+
#if NETSTANDARD2_0
61+
, cancellationToken
62+
#endif
63+
);
64+
return (fromCache != null, fromCache);
5665
}
5766

5867
/// <summary>
5968
/// Puts the specified value in the cache as part of an asynchronous execution.
60-
/// <para><remarks>The implementation is synchronous as there is no advantage to an asynchronous implementation for an in-memory cache.</remarks></para>
6169
/// </summary>
6270
/// <param name="key">The cache key.</param>
6371
/// <param name="value">The value to put into the cache.</param>
@@ -69,7 +77,11 @@ public override Task PutAsync(string key, byte[] value, Ttl ttl, CancellationTok
6977
{
7078
cancellationToken.ThrowIfCancellationRequested();
7179

72-
return _cache.SetAsync(key, value ?? Empty, ttl.ToDistributedCacheEntryOptions());
80+
return _cache.SetAsync(key, value, ttl.ToDistributedCacheEntryOptions()
81+
#if NETSTANDARD2_0
82+
, cancellationToken
83+
#endif
84+
);
7385
}
7486

7587

src/Polly.Caching.Distributed.Shared/NetStandardIDistributedCacheProvider.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,23 @@ protected NetStandardIDistributedCacheProvider(
3030
/// Gets a value from cache.
3131
/// </summary>
3232
/// <param name="key">The cache key.</param>
33-
/// <returns>The value from cache; or null, if none was found.</returns>
34-
public abstract TCache Get(string key);
33+
/// <returns>
34+
/// A tuple whose first element is a value indicating whether the key was found in the cache,
35+
/// and whose second element is the value from the cache (default(TResult) if not found).
36+
/// </returns>
37+
public abstract (bool, TCache) TryGet(string key);
3538

3639
/// <summary>
37-
/// Gets a value from the memory cache as part of an asynchronous execution. <para><remarks>The implementation is synchronous as there is no advantage to an asynchronous implementation for an in-memory cache.</remarks></para>
40+
/// Gets a value from the memory cache as part of an asynchronous execution.
3841
/// </summary>
3942
/// <param name="key">The cache key.</param>
4043
/// <param name="cancellationToken">The cancellation token. </param>
4144
/// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context. <para><remarks>For <see cref="NetStandardIDistributedCacheProvider{TCache}"/>, this parameter is irrelevant and is ignored, as the Microsoft.Extensions.Caching.Distributed.IDistributedCache interface does not support it.</remarks></para></param>
42-
/// <returns>A <see cref="Task{TResult}" /> promising as Result the value from cache; or null, if none was found.</returns>
43-
public abstract Task<TCache> GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext);
45+
/// <returns>
46+
/// A <see cref="Task{TResult}" /> promising as Result a tuple whose first element is a value indicating whether
47+
/// the key was found in the cache, and whose second element is the value from the cache (default(TResult) if not found).
48+
/// </returns>
49+
public abstract Task<(bool, TCache)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext);
4450

4551
/// <summary>
4652
/// Puts the specified value in the cache.
@@ -52,7 +58,6 @@ protected NetStandardIDistributedCacheProvider(
5258

5359
/// <summary>
5460
/// Puts the specified value in the cache as part of an asynchronous execution.
55-
/// <para><remarks>The implementation is synchronous as there is no advantage to an asynchronous implementation for an in-memory cache.</remarks></para>
5661
/// </summary>
5762
/// <param name="key">The cache key.</param>
5863
/// <param name="value">The value to put into the cache.</param>

src/Polly.Caching.Distributed.Shared/NetStandardIDistributedCacheStringProvider.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Threading;
1+
using System.Threading;
32
using System.Threading.Tasks;
43
using Microsoft.Extensions.Caching.Distributed;
54

@@ -17,16 +16,19 @@ public class NetStandardIDistributedCacheStringProvider : NetStandardIDistribute
1716
public NetStandardIDistributedCacheStringProvider(Microsoft.Extensions.Caching.Distributed.IDistributedCache iDistributedCache) : base(iDistributedCache)
1817
{
1918
}
20-
19+
2120
/// <summary>
2221
/// Gets a value from cache.
2322
/// </summary>
2423
/// <param name="key">The cache key.</param>
25-
/// <returns>The value from cache; or null, if none was found.</returns>
26-
public override string Get(String key)
24+
/// <returns>
25+
/// A tuple whose first element is a bool indicating whether the key was found in the cache,
26+
/// and whose second element is the value from the cache.
27+
/// </returns>
28+
public override (bool, string) TryGet(string key)
2729
{
28-
string returned = _cache.GetString(key);
29-
return returned == null || returned.Length == 0 ? null : returned;
30+
string fromCache = _cache.GetString(key);
31+
return (fromCache != null, fromCache);
3032
}
3133

3234
/// <summary>
@@ -41,23 +43,29 @@ public override void Put(string key, string value, Ttl ttl)
4143
}
4244

4345
/// <summary>
44-
/// Gets a value from the memory cache as part of an asynchronous execution. <para><remarks>The implementation is synchronous as there is no advantage to an asynchronous implementation for an in-memory cache.</remarks></para>
46+
/// Gets a value from the memory cache as part of an asynchronous execution.
4547
/// </summary>
4648
/// <param name="key">The cache key.</param>
4749
/// <param name="cancellationToken">The cancellation token. </param>
4850
/// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context. <para><remarks>For <see cref="NetStandardIDistributedCacheProvider{TCache}"/>, this parameter is irrelevant and is ignored, as the Microsoft.Extensions.Caching.Distributed.IDistributedCache interface does not support it.</remarks></para></param>
49-
/// <returns>A <see cref="Task{TResult}" /> promising as Result the value from cache; or null, if none was found.</returns>
50-
public override async Task<string> GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
51+
/// <returns>
52+
/// A <see cref="Task{TResult}" /> promising as Result a tuple whose first element is a value indicating whether
53+
/// the key was found in the cache, and whose second element is the value from the cache.
54+
/// </returns>
55+
public override async Task<(bool, string)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
5156
{
5257
cancellationToken.ThrowIfCancellationRequested();
5358

54-
string returned = await _cache.GetStringAsync(key);
55-
return returned == null || returned.Length == 0 ? null : returned;
59+
string fromCache = await _cache.GetStringAsync(key
60+
#if NETSTANDARD2_0
61+
, cancellationToken
62+
#endif
63+
);
64+
return (fromCache != null, fromCache);
5665
}
5766

5867
/// <summary>
5968
/// Puts the specified value in the cache as part of an asynchronous execution.
60-
/// <para><remarks>The implementation is synchronous as there is no advantage to an asynchronous implementation for an in-memory cache.</remarks></para>
6169
/// </summary>
6270
/// <param name="key">The cache key.</param>
6371
/// <param name="value">The value to put into the cache.</param>
@@ -69,7 +77,11 @@ public override Task PutAsync(string key, string value, Ttl ttl, CancellationTok
6977
{
7078
cancellationToken.ThrowIfCancellationRequested();
7179

72-
return _cache.SetStringAsync(key, value, ttl.ToDistributedCacheEntryOptions());
80+
return _cache.SetStringAsync(key, value, ttl.ToDistributedCacheEntryOptions()
81+
#if NETSTANDARD2_0
82+
, cancellationToken
83+
#endif
84+
);
7385
}
7486

7587
}

0 commit comments

Comments
 (0)