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
{
public class AsyncSerializingCacheProvider<TResult, 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))]
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;
}
[AsyncStateMachine(typeof(AsyncSerializingCacheProvider<, >.<PutAsync>d__4))]
public Task PutAsync(string key, TResult value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
<PutAsync>d__4 stateMachine = default(<PutAsync>d__4);
stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create();
stateMachine.<>4__this = this;
stateMachine.key = key;
stateMachine.value = value;
stateMachine.ttl = ttl;
stateMachine.cancellationToken = cancellationToken;
stateMachine.continueOnCapturedContext = continueOnCapturedContext;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start<<PutAsync>d__4>(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
}
}