Skip to content

Latest commit

 

History

History
99 lines (60 loc) · 5.84 KB

File metadata and controls

99 lines (60 loc) · 5.84 KB

Polly.Caching.Serialization.Json

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.

NuGet version Build status Slack Status

What is Polly?

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

Installing Polly.Caching.Serialization.Json via NuGet

Install-Package Polly.Caching.Serialization.Json

Supported targets

Polly.Caching.Serialization.Json supports .NET Standard 1.1 and .NET Standard 2.0.

Versions and Dependencies

Polly.Caching.Serialization.Json >=v3.0 requires:

Polly.Caching.Serialization.Json >=v2.0 and <v3 requires:

How to use the Polly.Caching.Serialization.Json plugin

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.
); 

Release notes

For details of changes by release see the change log.

Acknowledgements

Instructions for Contributing

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!

License

Licensed under the terms of the New BSD License