AsyncSerializingCacheProvider<TResult, TSerialized>
Defines an IAsyncCacheProvider<T> which serializes objects of type TResult in and out of an underlying cache which caches as type TSerialized. For use with asynchronous CachePolicy.
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Polly.Caching
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class AsyncSerializingCacheProvider<[System.Runtime.CompilerServices.Nullable(2)] TResult, [System.Runtime.CompilerServices.Nullable(2)] TSerialized> : IAsyncCacheProvider<TResult>
{
private readonly IAsyncCacheProvider<TSerialized> _wrappedCacheProvider;
private readonly ICacheItemSerializer<TResult, TSerialized> _serializer;
public AsyncSerializingCacheProvider(IAsyncCacheProvider<TSerialized> wrappedCacheProvider, ICacheItemSerializer<TResult, TSerialized> serializer)
{
if (wrappedCacheProvider == null)
throw new ArgumentNullException("wrappedCacheProvider");
_wrappedCacheProvider = wrappedCacheProvider;
if (serializer == null)
throw new ArgumentNullException("serializer");
_serializer = serializer;
}
[AsyncStateMachine(typeof(AsyncSerializingCacheProvider<, >.<TryGetAsync>d__3))]
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
1,
0,
2
})]
public Task<(bool, TResult)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
<TryGetAsync>d__3 stateMachine = default(<TryGetAsync>d__3);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<(bool, TResult)>.Create();
stateMachine.<>4__this = this;
stateMachine.key = key;
stateMachine.cancellationToken = cancellationToken;
stateMachine.continueOnCapturedContext = continueOnCapturedContext;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public Task PutAsync(string key, [System.Runtime.CompilerServices.Nullable(2)] TResult value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
return _wrappedCacheProvider.PutAsync(key, _serializer.Serialize(value), ttl, cancellationToken, continueOnCapturedContext);
}
}
}