This repo contains a Json plugin for the Polly Cache policy using Newtonsoft.Json. It targets .NET Standard 1.1 and .NET Standard 2.0.
Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, Cache-aside and Fallback in a fluent and thread-safe manner. Polly targets .NET Standard 1.1 and .NET Standard 2.0.
Polly is a member of the .NET Foundation!
Keep up to date with new feature announcements, tips & tricks, and other news through www.thepollyproject.org
Install-Package Polly.Caching.Serialization.Json
Polly.Caching.Serialization.Json supports .NET Standard 1.1 and .NET Standard 2.0.
Polly.Caching.Serialization.Json >=v3.0 requires:
- Polly >= v7.0.0.
- Newtonsoft.Json v11.0.2 or above
Polly.Caching.Serialization.Json >=v2.0 and <v3 requires:
- Polly >= v6.0.1 and <v7.
- Newtonsoft.Json v11.0.2 or above
The below example demonstrates how to configure the Polly.Caching.Serialization.Json
serialization plugin in combination with the Polly.Caching.IDistributedCache
cache provider plugin, such that you could effectively cache any type from a Polly Cache policy to Redis using Microsoft's Redis implementation of IDistributedCache
.
For some IDistributedCache distributedCache
instance (perhaps just configured and instantiated, perhaps provided to local code by Dependency Injection):
// Create a Newtonsoft.Json.JsonSerializerSettings defining any settings to use for serialization
var serializerSettings = new JsonSerializerSettings()
{
// Any configuration options
};
// Create a Polly cache policy for caching ProductDetails entities, using that IDistributedCache instance.
var productDetailsCachePolicy = Policy.CacheAsync<ProductDetails>(
distributedCache.AsAsyncCacheProvider<string>().WithSerializer<ProductDetails, string>(
new Polly.Caching.Serialization.Json.JsonSerializer<ProductDetails>(serializerSettings)
),
TimeSpan.FromMinutes(5) // for example
/* for deeper CachePolicy configuration options:
-- see https://github.com/App-vNext/Polly/wiki/Cache#syntax
-- and https://github.com/App-vNext/Polly.Caching.IDistributedCache */
);
The example demonstrates usage with the Polly.Caching.IDistributedCache
cache provider, but you may also use Polly.Caching.Serialization.Json with any other Polly.Caching.ISyncCacheProvider
or Polly.Caching.IAsyncCacheProvider
.
The configuration above is all that is necessary to configure JSON serialization as part of caching. Values will be serialized using the supplied JsonSerializerSettings
before being put to cache. When values are retrieved from cache, they will be deserialized by the same JsonSerializerSettings
before being returned by the policy.
Usage of the CachePolicy at call site is unchanged from when a serializer was not in the mix:
string productId = // ... from somewhere
ProductDetails productDetails = await productDetailsCachePolicy.ExecuteAsync(ctx => getProductDetails(productId),
new Context(productId) // productId will also be the cache key used in this execution.
);
For details of changes by release see the change log.
- @reisenberger - Polly Json serializer using Newtonsoft.Json
- @seanfarrow and @reisenberger - Caching and serialization architecture in the main Polly repo
- @reisenberger - Update to Polly v7.0.0
Please check out our Wiki for contributing guidelines. We are following the excellent GitHub Flow process, and would like to make sure you have all of the information needed to be a world-class contributor!
Since Polly is part of the .NET Foundation, we ask our contributors to abide by their Code of Conduct.
Also, we've stood up a Slack channel for easier real-time discussion of ideas and the general direction of Polly as a whole. Be sure to join the conversation today!
Licensed under the terms of the New BSD License