-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implementing cache serializers
The Polly.Caching
namespace provides an ICacheItemSerializer<TResult, TSerialized>
interface.
Implementations of this interface allow you to serialize items for placing in the cache, and deserialize again on retrieving from cache.
The interface ICacheItemSerializer<TResult, TSerialized>
is very simple to fulfil:
public interface ICacheItemSerializer<TResult, TSerialized>
{
TSerialized Serialize(TResult objectToSerialize);
TResult Deserialize(TSerialized objectToDeserialize);
}
Some cache providers (such as Redis) store most items as specific types (eg string
or byte[]
), requiring you to serialize more complex types. For example, for Redis you might implement:
RedisCacheProvider<string>: ISyncCacheProvider<string>, IAsyncCacheProvider<string>
The above RedisCacheProvider<string>
(as is) would only be usable in CachePolicy<string>
. To use the cache for other result types, implement (for example using Newtonsoft.Json):
JsonSerializer<TResult, string> : ICacheItemSerializer<TResult, string>
{
string Serialize(TResult objectToSerialize);
TResult Deserialize(string objectToDeserialize);
}
An in-built extension method in Polly, .WithSerializer<TResult, TSerialized>()
, then allows you to bridge from the native TResult
of the execution to the serialized TSerialized
type.
ISyncCacheProvider<TSerialized>.WithSerializer<TResult, TSerialized>()
returns an ISyncCacheProvider<TResult>
(which is expected by the configuration syntax for CachePolicy<TResult>
), allowing you to bridge as follows:
ISyncCacheProvider<string> redisCacheProvider = ... // configured earlier
ICacheItemSerializer<TResult, string> jsonSerializer = ... // configured earlier
CachePolicy<TResult> cache = Policy.Cache<TResult>(
redisCacheProvider.WithSerializer<TResult, string>(jsonSerializer),
TimeSpan.FromMinutes(5));
The above examples are based around serializing to string
using Newtonsoft.Json as this is one of the most commonly understood serialization approaches. However, it is far from the only option. Redis, for example, can also store any kind of byte[]
array as a Redis 'string'. For a fuller overview of serialization options available, see the Microsoft Patterns-and-Practices article on Caching, section Serialization considerations.
Community contributions of new third-party serializers for Polly will be very welcome. Let us know, and we can make new providers available to the wider Polly community (with full credit to the original contributors).
- Home
- Polly RoadMap
- Contributing
- Transient fault handling and proactive resilience engineering
- Supported targets
- Retry
- Circuit Breaker
- Advanced Circuit Breaker
- Timeout
- Bulkhead
- Cache
- Rate-Limit
- Fallback
- PolicyWrap
- NoOp
- PolicyRegistry
- Polly and HttpClientFactory
- Asynchronous action execution
- Handling InnerExceptions and AggregateExceptions
- Statefulness of policies
- Keys and Context Data
- Non generic and generic policies
- Polly and interfaces
- Some policy patterns
- Debugging with Polly in Visual Studio
- Unit-testing with Polly
- Polly concept and architecture
- Polly v6 breaking changes
- Polly v7 breaking changes
- DISCUSSION PROPOSAL- Polly eventing and metrics architecture