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

SerializingCacheProvider<TResult, TSerialized>

public class SerializingCacheProvider<TResult, TSerialized> : ISyncCacheProvider<TResult>
Defines an ISyncCacheProvider<T> which serializes objects of type TResult in and out of an underlying cache which caches as type TSerialized. For use with synchronous CachePolicy.
using System; namespace Polly.Caching { public class SerializingCacheProvider<TResult, TSerialized> : ISyncCacheProvider<TResult> { private readonly ISyncCacheProvider<TSerialized> _wrappedCacheProvider; private readonly ICacheItemSerializer<TResult, TSerialized> _serializer; public SerializingCacheProvider(ISyncCacheProvider<TSerialized> wrappedCacheProvider, ICacheItemSerializer<TResult, TSerialized> serializer) { if (wrappedCacheProvider == null) throw new ArgumentNullException("wrappedCacheProvider"); if (serializer == null) throw new ArgumentNullException("serializer"); _wrappedCacheProvider = wrappedCacheProvider; _serializer = serializer; } public TResult Get(string key) { return _serializer.Deserialize(_wrappedCacheProvider.Get(key)); } public void Put(string key, TResult value, Ttl ttl) { _wrappedCacheProvider.Put(key, _serializer.Serialize(value), ttl); } } }