Skip to content

Commit 5808190

Browse files
committed
Migrate memory cache provider implementations to Polly v7.0.0
1 parent 3071509 commit 5808190

File tree

6 files changed

+31
-54
lines changed

6 files changed

+31
-54
lines changed

src/Polly.Caching.Memory.NetStandard13/Polly.Caching.Memory.NetStandard13.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
</ItemGroup>
3030
<ItemGroup>
3131
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.2" />
32-
<PackageReference Include="Polly" Version="6.1.1" />
32+
<PackageReference Include="Polly" Version="7.0.0" />
33+
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
3334
</ItemGroup>
3435
<Import Project="..\Polly.Caching.Memory.Shared\Polly.Caching.Memory.Shared.projitems" Label="Shared" />
3536
</Project>

src/Polly.Caching.Memory.NetStandard20/Polly.Caching.Memory.NetStandard20.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
</ItemGroup>
3030
<ItemGroup>
3131
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.2" />
32-
<PackageReference Include="Polly" Version="6.1.1" />
32+
<PackageReference Include="Polly" Version="7.0.0" />
3333
</ItemGroup>
3434
<Import Project="..\Polly.Caching.Memory.Shared\Polly.Caching.Memory.Shared.projitems" Label="Shared" />
3535
</Project>

src/Polly.Caching.Memory.Shared/MemoryCacheProvider.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,23 @@ public class MemoryCacheProvider : ISyncCacheProvider, IAsyncCacheProvider
1919
/// <param name="memoryCache">The memory cache instance in which to store cached items.</param>
2020
public MemoryCacheProvider(IMemoryCache memoryCache)
2121
{
22-
if (memoryCache == null) throw new ArgumentNullException(nameof(memoryCache));
23-
_cache = memoryCache;
22+
_cache = memoryCache ?? throw new ArgumentNullException(nameof(memoryCache));
2423
}
2524

2625
/// <summary>
2726
/// Gets a value from cache.
2827
/// </summary>
2928
/// <param name="key">The cache key.</param>
30-
/// <returns>The value from cache; or null, if none was found.</returns>
31-
public object Get(String key)
29+
/// <returns>
30+
/// A tuple whose first element is a value indicating whether the key was found in the cache,
31+
/// and whose second element is the value from the cache (null if not found).
32+
/// </returns>
33+
public (bool, object) TryGet(string key)
3234
{
33-
object value;
34-
if (_cache.TryGetValue(key, out value))
35-
{
36-
return value;
37-
}
38-
return null;
35+
bool cacheHit = _cache.TryGetValue(key, out var value);
36+
return (cacheHit, value);
3937
}
40-
38+
4139
/// <summary>
4240
/// Puts the specified value in the cache.
4341
/// </summary>
@@ -59,7 +57,7 @@ public void Put(string key, object value, Ttl ttl)
5957
{
6058
options.AbsoluteExpiration = DateTimeOffset.MaxValue;
6159
}
62-
else
60+
else
6361
{
6462
options.AbsoluteExpirationRelativeToNow = ttl.Timespan < remaining ? ttl.Timespan : remaining;
6563
}
@@ -69,17 +67,20 @@ public void Put(string key, object value, Ttl ttl)
6967
}
7068

7169
/// <summary>
72-
/// 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>
70+
/// Gets a value from the cache asynchronously.
71+
/// <para><remarks>The implementation is synchronous as there is no advantage to an asynchronous implementation for an in-memory cache.</remarks></para>
7372
/// </summary>
7473
/// <param name="key">The cache key.</param>
75-
/// <param name="cancellationToken">The cancellation token. </param>
76-
/// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context. <para><remarks>For <see cref="MemoryCacheProvider"/>, this parameter is irrelevant and is ignored, as the implementation is synchronous.</remarks></para></param>
77-
/// <returns>A <see cref="Task{TResult}" /> promising as Result the value from cache; or null, if none was found.</returns>
78-
public Task<object> GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
74+
/// <param name="cancellationToken">The cancellation token.</param>
75+
/// <param name="continueOnCapturedContext">Whether async calls should continue on a captured synchronization context. <para><remarks>Note: if the underlying cache's async API does not support controlling whether to continue on a captured context, async Policy executions with continueOnCapturedContext == true cannot be guaranteed to remain on the captured context.</remarks></para></param>
76+
/// <returns>
77+
/// A <see cref="Task{TResult}" /> promising as Result a tuple whose first element is a value indicating whether
78+
/// the key was found in the cache, and whose second element is the value from the cache (null if not found).
79+
/// </returns>
80+
public Task<(bool, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
7981
{
8082
cancellationToken.ThrowIfCancellationRequested();
81-
return Task.FromResult(Get(key));
82-
// (With C#7.0, a ValueTask<> approach would be preferred, but some of our tfms do not support that. TO DO: Implement it, with preprocessor if/endif directives, for NetStandard)
83+
return Task.FromResult(TryGet(key));
8384
}
8485

8586
/// <summary>
@@ -97,7 +98,6 @@ public Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancel
9798
cancellationToken.ThrowIfCancellationRequested();
9899
Put(key, value, ttl);
99100
return Task.CompletedTask;
100-
// (With C#7.0, a ValueTask<> approach would be preferred, but some of our tfms do not support that. TO DO: Implement it, with preprocessor if/endif directives, for NetStandard)
101101
}
102102
}
103103
}

src/Polly.Caching.Memory.SharedSpecs/Integration/CacheProviderExtensions.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Polly.Caching.Memory.SharedSpecs/Polly.Caching.Memory.SharedSpecs.projitems

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<Import_RootNamespace>Polly.Caching.Memory.Specs</Import_RootNamespace>
1010
</PropertyGroup>
1111
<ItemGroup>
12-
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheProviderExtensions.cs" />
1312
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripspecsAsyncBase.cs" />
1413
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecsBase.cs" />
1514
<Compile Include="$(MSBuildThisFileDirectory)Integration\CacheRoundTripSpecsSyncBase.cs" />

src/Polly.Caching.Memory.SharedSpecs/Unit/MemoryCacheProviderSpecs.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,26 @@ public void Get_should_return_instance_previously_stored_in_cache()
4242

4343
string key = Guid.NewGuid().ToString();
4444
object value = new object();
45-
#if PORTABLE
46-
using (Microsoft.Extensions.Caching.Memory.ICacheEntry entry = memoryCache.CreateEntry(key)) {
45+
using (ICacheEntry entry = memoryCache.CreateEntry(key)) {
4746
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(10);
4847
entry.Value = value;
4948
}
50-
#else
51-
memoryCache[key] = value;
52-
#endif
5349

5450
MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);
55-
object got = provider.Get(key);
56-
got.Should().BeSameAs(value);
51+
(bool cacheHit, object payload) = provider.TryGet(key);
52+
cacheHit.Should().BeTrue();
53+
payload.Should().BeSameAs(value);
5754
}
5855

5956
[Fact]
60-
public void Get_should_return_null_on_unknown_key()
57+
public void Get_should_return_false_on_unknown_key()
6158
{
6259
IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions());
6360

6461
MemoryCacheProvider provider = new MemoryCacheProvider(memoryCache);
65-
object got = provider.Get(Guid.NewGuid().ToString());
66-
got.Should().BeNull();
62+
(bool cacheHit, object payload) = provider.TryGet(Guid.NewGuid().ToString());
63+
cacheHit.Should().BeFalse();
64+
payload.Should().BeNull();
6765
}
6866

6967
#endregion

0 commit comments

Comments
 (0)