|
1 | 1 | # Polly.Caching.IDistributedCache |
2 | | -Plug-in for the Polly Cache policy supporting Microsoft.Extensions.Caching.Distributed.IDistributedCache. |
| 2 | + |
| 3 | +This repo contains the `Microsoft.Extensions.Caching.Distributed.IDistributedCache` plugin for the [Polly](https://github.com/App-vNext/Polly) [Cache policy](https://github.com/App-vNext/Polly/wiki/Cache). It targets .NET Standard 1.1. |
| 4 | + |
| 5 | +[](https://badge.fury.io/nu/Polly.Caching.IDistributedCache) [](https://ci.appveyor.com/project/joelhulen/polly-caching-IDistributedCache) [](http://www.pollytalk.org) |
| 6 | + |
| 7 | +## What is Polly? |
| 8 | + |
| 9 | +[Polly](https://github.com/App-vNext/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 4.0, .NET 4.5 and .NET Standard 1.1. |
| 10 | + |
| 11 | +Polly is a member of the [.NET Foundation](https://www.dotnetfoundation.org/about)! |
| 12 | + |
| 13 | +**Keep up to date with new feature announcements, tips & tricks, and other news through [www.thepollyproject.org](http://www.thepollyproject.org)** |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## What is Polly.Caching.IDistributedCache? |
| 18 | + |
| 19 | +This project, Polly.Caching.IDistributedCache, allows you to use Polly's `CachePolicy` with [implementations of](https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed) .Net Standard's [`IDistributedCache`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.caching.distributed.idistributedcache). |
| 20 | + |
| 21 | +# Installing Polly.Caching.IDistributedCache via NuGet |
| 22 | + |
| 23 | + Install-Package Polly.Caching.IDistributedCache |
| 24 | + |
| 25 | +You can install the Strongly Named version via: |
| 26 | + |
| 27 | + Install-Package Polly.Caching.IDistributedCache-Signed |
| 28 | + |
| 29 | +# Supported targets |
| 30 | + |
| 31 | +Polly.Caching.IDistributedCache supports .NetStandard 1.1 and above. |
| 32 | + |
| 33 | +## Dependencies |
| 34 | + |
| 35 | +Polly.Caching.IDistributedCache requires: |
| 36 | + |
| 37 | ++ [Polly](nuget.org/packages/polly) v5.4.0 or above. |
| 38 | ++ [Microsoft.Extensions.Caching.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Caching.Abstractions/) v1.1.2 or above. |
| 39 | + |
| 40 | +# How to use the Polly.Caching.IDistributedCache plugin |
| 41 | + |
| 42 | +These notes assume you are familiar with using the .Net Standard `IDistributedCache` implementations. For information, see: https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed |
| 43 | + |
| 44 | +Assuming you have an instance `IDistributedCache distributedCache` in hand (perhaps just configured and instantiated, perhaps provided to local code by Dependency Injection): |
| 45 | + |
| 46 | +```csharp |
| 47 | +// Create a Polly cache policy for caching string results, using that `IDistributedCache` instance. |
| 48 | +var cachePolicy = Policy.Cache<string>(distributedCache.AsSyncCacheProvider<string>, TimeSpan.FromMinutes(5)); |
| 49 | + |
| 50 | +// Create a Polly cache policy for caching byte[] results, using that `IDistributedCache` instance. |
| 51 | +var cachePolicy = Policy.Cache<byte[]>(distributedCache.AsSyncCacheProvider<byte[]>, TimeSpan.FromMinutes(5)); |
| 52 | + |
| 53 | +// Or similarly for async executions returning string results: |
| 54 | +var cachePolicy = Policy.CacheAsync<string>(distributedCache.AsAsyncCacheProvider<string>, TimeSpan.FromMinutes(5)); |
| 55 | + |
| 56 | +// Or similarly for async executions returning byte[] results: |
| 57 | +var cachePolicy = Policy.CacheAsync<byte[]>(distributedCache.AsAsyncCacheProvider<byte[]>, TimeSpan.FromMinutes(5)); |
| 58 | + |
| 59 | +// You can also use ASP.NET Core's DistributedCacheEntryOptions for specifying cache item time-to-live, as shown below. |
| 60 | +// All time-to-live functionality represented by DistributedCacheEntryOptions is supported. |
| 61 | +DistributedCacheEntryOptions entryOptions = // ... |
| 62 | +var cachePolicy = Policy.CacheAsync<byte[]>(distributedCache.AsAsyncCacheProvider<byte[]>, entryOptions.AsTtlStrategy()); |
| 63 | + |
| 64 | + |
| 65 | +``` |
| 66 | + |
| 67 | +Usage: |
| 68 | + |
| 69 | +```csharp |
| 70 | +string productId = // ... from somewhere |
| 71 | +string productDescription = await cachePolicy.ExecuteAsync(() => getProductDescription(productId), |
| 72 | + new Context(productId) // productId will also be the cache key used in this execution. |
| 73 | +); |
| 74 | +``` |
| 75 | + |
| 76 | +For many more configuration options and usage examples of the main Polly `CachePolicy`, see the [main Polly readme](https://github.com/App-vNext/Polly#cache) and [deep doco on the Polly wiki](https://github.com/App-vNext/Polly/wiki/Cache). Additional overloads allow attaching delegates for cache errors, cache hits/misses etc, for logging and telemetry. |
| 77 | + |
| 78 | +`CachePolicy` can of course also be combined with other policies in a [`PolicyWrap`](https://github.com/App-vNext/Polly/wiki/PolicyWrap). |
| 79 | + |
| 80 | +# Release notes |
| 81 | + |
| 82 | +For details of changes by release see the [change log](CHANGELOG.md). |
| 83 | + |
| 84 | + |
| 85 | +# Acknowledgements |
| 86 | + |
| 87 | +* [@seanfarrow](https://github.com/seanfarrow) and [@reisenberger](https://github.com/reisenberger) - Initial caching architecture in the main Polly repo |
| 88 | +* [@reisenberger](https://github.com/reisenberger) - `IDistributedCache` implementation |
| 89 | + |
| 90 | +# Instructions for Contributing |
| 91 | + |
| 92 | +Please check out our [Wiki](https://github.com/App-vNext/Polly/wiki/Git-Workflow) 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! |
| 93 | + |
| 94 | +Since Polly is part of the .NET Foundation, we ask our contributors to abide by their [Code of Conduct](https://www.dotnetfoundation.org/code-of-conduct). |
| 95 | + |
| 96 | +Also, we've stood up a [Slack](http://www.pollytalk.org) channel for easier real-time discussion of ideas and the general direction of Polly as a whole. Be sure to [join the conversation](http://www.pollytalk.org) today! |
| 97 | + |
| 98 | +# License |
| 99 | + |
| 100 | +Licensed under the terms of the [New BSD License](http://opensource.org/licenses/BSD-3-Clause) |
0 commit comments