<PackageReference Include="Polly" Version="8.5.1" />

AsyncSerializingCacheProvider<TSerialized>

Defines an IAsyncCacheProvider which serializes objects of any type 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)] TSerialized> : IAsyncCacheProvider { private readonly IAsyncCacheProvider<TSerialized> _wrappedCacheProvider; private readonly ICacheItemSerializer<object, TSerialized> _serializer; public AsyncSerializingCacheProvider(IAsyncCacheProvider<TSerialized> wrappedCacheProvider, ICacheItemSerializer<object, 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, object)> TryGetAsync(string key, CancellationToken cancellationToken, bool continueOnCapturedContext) { <TryGetAsync>d__3 stateMachine = default(<TryGetAsync>d__3); stateMachine.<>t__builder = AsyncTaskMethodBuilder<(bool, object)>.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)] object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext) { return _wrappedCacheProvider.PutAsync(key, _serializer.Serialize(value), ttl, cancellationToken, continueOnCapturedContext); } } }