Skip to content

Commit

Permalink
Merge pull request #13 from reisenberger/v300
Browse files Browse the repository at this point in the history
Version 3.0.0: For compatibility with (forthcoming) Polly v7.0.0
  • Loading branch information
joelhulen authored Feb 10, 2019
2 parents b7919d9 + 8618346 commit e26539c
Show file tree
Hide file tree
Showing 28 changed files with 577 additions and 121 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Polly.Caching.Distributed change log

## 3.0.0
- Allow caching of `default(TResult)`
- Compatible with Polly >= v7

## 2.0.1
- No functional changes
- Indicate compatibility with Polly < v7
Expand Down
2 changes: 1 addition & 1 deletion GitVersionConfig.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
next-version: 2.0.1
next-version: 3.0.0
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ Polly.Caching.Distributed supports .NET Standard 1.1 and .NET Standard 2.0.

## Versions and Dependencies

Polly.Caching.Distributed >=v3.0 requires:

+ [Polly](https://nuget.org/packages/polly) >= v7.0.0.
+ [Microsoft.Extensions.Caching.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Caching.Abstractions/) v2.0.2 or above.

Polly.Caching.Distributed >=v2.0 and <v3 requires:

+ [Polly](nuget.org/packages/polly) >= v6.0.1 and <v7.
+ [Polly](https://nuget.org/packages/polly) >= v6.0.1 and <v7.
+ [Microsoft.Extensions.Caching.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Caching.Abstractions/) v2.0.2 or above.

Polly.Caching.IDistributedCache <v2.0 requires:

+ [Polly](nuget.org/packages/polly) v5.4.0 or above.
+ [Polly](https://nuget.org/packages/polly) v5.4.0 or above.
+ [Microsoft.Extensions.Caching.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Caching.Abstractions/) v1.1.2 or above.


Expand Down Expand Up @@ -129,6 +134,7 @@ For details of changes by release see the [change log](CHANGELOG.md).
* [@seanfarrow](https://github.com/seanfarrow) and [@reisenberger](https://github.com/reisenberger) - Initial caching architecture in the main Polly repo
* [@reisenberger](https://github.com/reisenberger) - `IDistributedCache` implementation
* [@seanfarrow](https://github.com/seanfarrow) - v2.0 update to Signed packages only to correspond with Polly v6.0.1
* [@reisenberger](https://github.com/reisenberger) - Update to Polly v7.0.0

# Instructions for Contributing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
<DefineConstants>TRACE;DEBUG;NETCOREAPP1_1;NETCOREAPP1_1;PORTABLE</DefineConstants>
<DefineConstants>TRACE;DEBUG;NETCOREAPP1_1;PORTABLE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -18,9 +18,10 @@
<PackageReference Include="FluentAssertions" Version="4.19.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Moq" Version="4.7.145" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Polly" Version="6.0.1" />
<PackageReference Include="Polly" Version="7.0.0" />

</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="1.1.2" />
<PackageReference Include="Polly" Version="6.0.1" />
<PackageReference Include="Polly" Version="7.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<Import Project="..\Polly.Caching.Distributed.Shared\Polly.Caching.Distributed.Shared.projitems" Label="Shared" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Runtime.CompilerServices;

[assembly: AssemblyTitle("Polly.Caching.Distributed")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.1.0")]
[assembly: AssemblyInformationalVersion("2.0.1.0")]
[assembly: AssemblyVersion("3.0.0.0")]
[assembly: AssemblyFileVersion("3.0.0.0")]
[assembly: AssemblyInformationalVersion("3.0.0.0")]
[assembly: CLSCompliant(false)] // Because Microsoft.Extensions.Caching.Memory.IDistributedCache, on which Polly.Caching.IDistributedCache.NetStandard11 depends, is not CLSCompliant.

[assembly: InternalsVisibleTo("Polly.Caching.Distributed.NetStandard11.Specs")]
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>full</DebugType>
<DefineConstants>TRACE;DEBUG;NETCOREAPP1_1;NETCOREAPP1_1;PORTABLE</DefineConstants>
<DefineConstants>TRACE;DEBUG;NETCOREAPP2_0;PORTABLE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -16,11 +16,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="4.19.3" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Moq" Version="4.7.145" />
<PackageReference Include="Polly.Caching.Serialization.Json" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Polly" Version="6.0.1" />
<PackageReference Include="Polly" Version="7.0.0" />

</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="2.0.2" />
<PackageReference Include="Polly" Version="6.0.1" />
<PackageReference Include="Polly" Version="7.0.0" />
</ItemGroup>
<Import Project="..\Polly.Caching.Distributed.Shared\Polly.Caching.Distributed.Shared.projitems" Label="Shared" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Runtime.CompilerServices;

[assembly: AssemblyTitle("Polly.Caching.Distributed")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.1.0")]
[assembly: AssemblyInformationalVersion("2.0.1.0")]
[assembly: AssemblyVersion("3.0.0.0")]
[assembly: AssemblyFileVersion("3.0.0.0")]
[assembly: AssemblyInformationalVersion("3.0.0.0")]
[assembly: CLSCompliant(false)] // Because Microsoft.Extensions.Caching.Memory.IDistributedCache, on which Polly.Caching.IDistributedCache.NetStandard11 depends, is not CLSCompliant.

[assembly: InternalsVisibleTo("Polly.Caching.Distributed.NetStandard20.Specs")]
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace Polly.Caching.Distributed
/// </summary>
public class NetStandardIDistributedCacheByteArrayProvider : NetStandardIDistributedCacheProvider<byte[]>
{
private readonly byte[] Empty = new byte[0];

/// <summary>
/// Initializes a new instance of the <see cref="NetStandardIDistributedCacheByteArrayProvider"/> class.
/// </summary>
Expand All @@ -23,13 +21,16 @@ public NetStandardIDistributedCacheByteArrayProvider(Microsoft.Extensions.Cachin
/// Gets a value from cache.
/// </summary>
/// <param name="key">The cache key.</param>
/// <returns>The value from cache; or null, if none was found.</returns>
public override byte[] Get(String key)
/// <returns>
/// A tuple whose first element is a bool indicating whether the key was found in the cache,
/// and whose second element is the value from the cache.
/// </returns>
public override (bool, byte[]) TryGet(string key)
{
byte[] returned = _cache.Get(key);
return returned == null || returned.Length == 0 ? null : returned; // Because Polly CachePolicy expects providers to return "no value held" as null.
byte[] fromCache = _cache.Get(key);
return (fromCache != null, fromCache);
}

/// <summary>
/// Puts the specified value in the cache.
/// </summary>
Expand All @@ -38,26 +39,33 @@ public override byte[] Get(String key)
/// <param name="ttl">The time-to-live for the cache entry.</param>
public override void Put(string key, byte[] value, Ttl ttl)
{
_cache.Set(key, value ?? Empty, ttl.ToDistributedCacheEntryOptions());
_cache.Set(key, value, ttl.ToDistributedCacheEntryOptions());
}

/// <summary>
/// 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>
/// Gets a value from the memory cache as part of an asynchronous execution.
/// </summary>
/// <param name="key">The cache key.</param>
/// <param name="cancellationToken">The cancellation token. </param>
/// <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>
/// <returns>A <see cref="Task{TResult}" /> promising as Result the value from cache; or null, if none was found.</returns>
public override async Task<byte[]> GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
/// <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>
/// <returns>
/// A <see cref="Task{TResult}" /> promising as Result a tuple whose first element is a value indicating whether
/// the key was found in the cache, and whose second element is the value from the cache.
/// </returns>
public override async Task<(bool, byte[])> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
cancellationToken.ThrowIfCancellationRequested();
byte[] returned = await _cache.GetAsync(key);
return returned == null || returned.Length == 0 ? null : returned; // Because Polly CachePolicy expects providers to return "no value held" as null.

byte[] fromCache = await _cache.GetAsync(key
#if NETSTANDARD2_0
, cancellationToken
#endif
);
return (fromCache != null, fromCache);
}

/// <summary>
/// Puts the specified value in the 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>
/// </summary>
/// <param name="key">The cache key.</param>
/// <param name="value">The value to put into the cache.</param>
Expand All @@ -69,7 +77,11 @@ public override Task PutAsync(string key, byte[] value, Ttl ttl, CancellationTok
{
cancellationToken.ThrowIfCancellationRequested();

return _cache.SetAsync(key, value ?? Empty, ttl.ToDistributedCacheEntryOptions());
return _cache.SetAsync(key, value, ttl.ToDistributedCacheEntryOptions()
#if NETSTANDARD2_0
, cancellationToken
#endif
);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,23 @@ protected NetStandardIDistributedCacheProvider(
/// Gets a value from cache.
/// </summary>
/// <param name="key">The cache key.</param>
/// <returns>The value from cache; or null, if none was found.</returns>
public abstract TCache Get(string key);
/// <returns>
/// A tuple whose first element is a value indicating whether the key was found in the cache,
/// and whose second element is the value from the cache (default(TResult) if not found).
/// </returns>
public abstract (bool, TCache) TryGet(string key);

/// <summary>
/// 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>
/// Gets a value from the memory cache as part of an asynchronous execution.
/// </summary>
/// <param name="key">The cache key.</param>
/// <param name="cancellationToken">The cancellation token. </param>
/// <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>
/// <returns>A <see cref="Task{TResult}" /> promising as Result the value from cache; or null, if none was found.</returns>
public abstract Task<TCache> GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext);
/// <returns>
/// A <see cref="Task{TResult}" /> promising as Result a tuple whose first element is a value indicating whether
/// the key was found in the cache, and whose second element is the value from the cache (default(TResult) if not found).
/// </returns>
public abstract Task<(bool, TCache)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext);

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

/// <summary>
/// Puts the specified value in the 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>
/// </summary>
/// <param name="key">The cache key.</param>
/// <param name="value">The value to put into the cache.</param>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;

Expand All @@ -17,16 +16,19 @@ public class NetStandardIDistributedCacheStringProvider : NetStandardIDistribute
public NetStandardIDistributedCacheStringProvider(Microsoft.Extensions.Caching.Distributed.IDistributedCache iDistributedCache) : base(iDistributedCache)
{
}

/// <summary>
/// Gets a value from cache.
/// </summary>
/// <param name="key">The cache key.</param>
/// <returns>The value from cache; or null, if none was found.</returns>
public override string Get(String key)
/// <returns>
/// A tuple whose first element is a bool indicating whether the key was found in the cache,
/// and whose second element is the value from the cache.
/// </returns>
public override (bool, string) TryGet(string key)
{
string returned = _cache.GetString(key);
return returned == null || returned.Length == 0 ? null : returned;
string fromCache = _cache.GetString(key);
return (fromCache != null, fromCache);
}

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

/// <summary>
/// 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>
/// Gets a value from the memory cache as part of an asynchronous execution.
/// </summary>
/// <param name="key">The cache key.</param>
/// <param name="cancellationToken">The cancellation token. </param>
/// <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>
/// <returns>A <see cref="Task{TResult}" /> promising as Result the value from cache; or null, if none was found.</returns>
public override async Task<string> GetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
/// <returns>
/// A <see cref="Task{TResult}" /> promising as Result a tuple whose first element is a value indicating whether
/// the key was found in the cache, and whose second element is the value from the cache.
/// </returns>
public override async Task<(bool, string)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
cancellationToken.ThrowIfCancellationRequested();

string returned = await _cache.GetStringAsync(key);
return returned == null || returned.Length == 0 ? null : returned;
string fromCache = await _cache.GetStringAsync(key
#if NETSTANDARD2_0
, cancellationToken
#endif
);
return (fromCache != null, fromCache);
}

/// <summary>
/// Puts the specified value in the 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>
/// </summary>
/// <param name="key">The cache key.</param>
/// <param name="value">The value to put into the cache.</param>
Expand All @@ -69,7 +77,11 @@ public override Task PutAsync(string key, string value, Ttl ttl, CancellationTok
{
cancellationToken.ThrowIfCancellationRequested();

return _cache.SetStringAsync(key, value, ttl.ToDistributedCacheEntryOptions());
return _cache.SetStringAsync(key, value, ttl.ToDistributedCacheEntryOptions()
#if NETSTANDARD2_0
, cancellationToken
#endif
);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Text;

namespace Polly.Caching.Distributed.Specs.Integration
{
public class ByteArraySerializer : ICacheItemSerializer<string, byte[]>
{
public string Deserialize(byte[] objectToDeserialize)
{
return objectToDeserialize == null ? null : Encoding.UTF8.GetString(objectToDeserialize);
}

public byte[] Serialize(string objectToSerialize)
{
return objectToSerialize == null ? null : Encoding.UTF8.GetBytes(objectToSerialize);
}
}
}
Loading

0 comments on commit e26539c

Please sign in to comment.