{"id":18924606,"url":"https://github.com/app-vnext/polly.caching.idistributedcache","last_synced_at":"2025-04-15T12:32:37.103Z","repository":{"id":62355318,"uuid":"112965028","full_name":"App-vNext/Polly.Caching.IDistributedCache","owner":"App-vNext","description":"Plug-in for the Polly Cache policy supporting Microsoft.Extensions.Caching.Distributed.IDistributedCache.","archived":false,"fork":false,"pushed_at":"2024-05-07T16:16:21.000Z","size":82,"stargazers_count":32,"open_issues_count":5,"forks_count":11,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-13T09:55:52.789Z","etag":null,"topics":["caching","dotnet-core","dotnetcore","polly","resilience","resiliency-patterns"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/App-vNext.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-03T21:25:06.000Z","updated_at":"2024-12-16T04:05:25.000Z","dependencies_parsed_at":"2024-06-18T15:30:27.435Z","dependency_job_id":"7f0c5201-3437-4980-b23f-0eacf7e10d6f","html_url":"https://github.com/App-vNext/Polly.Caching.IDistributedCache","commit_stats":{"total_commits":43,"total_committers":4,"mean_commits":10.75,"dds":0.2325581395348837,"last_synced_commit":"ed8e03a2ea2b32cd203869ceb529ef5618cf2fe6"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/App-vNext%2FPolly.Caching.IDistributedCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/App-vNext%2FPolly.Caching.IDistributedCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/App-vNext%2FPolly.Caching.IDistributedCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/App-vNext%2FPolly.Caching.IDistributedCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/App-vNext","download_url":"https://codeload.github.com/App-vNext/Polly.Caching.IDistributedCache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249072477,"owners_count":21208194,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["caching","dotnet-core","dotnetcore","polly","resilience","resiliency-patterns"],"created_at":"2024-11-08T11:07:22.270Z","updated_at":"2025-04-15T12:32:36.880Z","avatar_url":"https://github.com/App-vNext.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Polly.Caching.Distributed\n\nThis repo contains the `Microsoft.Extensions.Caching.Distributed.IDistributedCache` provider for the [Polly](https://github.com/App-vNext/Polly) [Cache policy](https://github.com/App-vNext/Polly/wiki/Cache).  The current version targets .NET Standard 1.1, .NET Standard 2.0 and .NET Standard 2.1.\n\n[![NuGet version](https://badge.fury.io/nu/Polly.Caching.Distributed.svg)](https://badge.fury.io/nu/Polly.Caching.Distributed) [![Build status](https://ci.appveyor.com/api/projects/status/pgd89nfdr9u4ig8m?svg=true)](https://ci.appveyor.com/project/joelhulen/polly-caching-Distributed) [![Slack Status](http://www.pollytalk.org/badge.svg)](http://www.pollytalk.org)\n\n## What is Polly?\n\n[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. \n\nPolly is a member of the [.NET Foundation](https://www.dotnetfoundation.org/about).\n\n**Keep up to date with new feature announcements, tips \u0026 tricks, and other news through [www.thepollyproject.org](http://www.thepollyproject.org)**\n\n![](https://raw.github.com/App-vNext/Polly/master/Polly-Logo.png)\n\n## What is Polly.Caching.Distributed?\n\nThis project, Polly.Caching.Distributed, 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). \n\n# Installing Polly.Caching.Distributed via NuGet\n\n    Install-Package Polly.Caching.Distributed\n\n# Supported targets\n\nPolly.Caching.Distributed \u0026gt;= v3.0.1 supports .NET Standard 1.1, .NET Standard 2.0 and .NET Standard 2.1.\n\nPolly.Caching.Distributed \u0026lt; v3.0.1 supports .NET Standard 1.1 and .NET Standard 2.0.\n\n## Dependency compatibility with Polly\n\nPolly.Caching.Distributed \u0026gt;=v3.0.1 requires:\n\n+ [Polly](https://nuget.org/packages/polly) \u003e= v7.1.1.\n\nPolly.Caching.Distributed v3.0.0 requires:\n\n+ [Polly](https://nuget.org/packages/polly) \u003e= v7.0.0.\n\nPolly.Caching.Distributed \u0026gt;=v2.0 and \u0026lt;v3 requires:\n\n+ [Polly](https://nuget.org/packages/polly) \u003e= v6.0.1 and \u0026lt;v7.\n\nPolly.Caching.IDistributedCache \u0026lt;v2.0 requires:\n\n+ [Polly](https://nuget.org/packages/polly) v5.4.0 or above.\n\n\n# How to use the Polly.Caching.Distributed plugin\n\nThese 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 .  As described on that page, Microsoft provide a Redis implementation and an SQL server implementation for `IDistributedCache`.\n\nAssuming you have an instance `IDistributedCache distributedCache` in hand (perhaps just configured and instantiated, perhaps provided to local code by Dependency Injection):\n\n\n```csharp\n// Create a Polly cache policy for caching string results, using that IDistributedCache  instance.\nvar cachePolicy = Policy.Cache\u003cstring\u003e(distributedCache.AsSyncCacheProvider\u003cstring\u003e(), TimeSpan.FromMinutes(5));\n\n// Create a Polly cache policy for caching byte[] results, using that IDistributedCache  instance.\nvar cachePolicy = Policy.Cache\u003cbyte[]\u003e(distributedCache.AsSyncCacheProvider\u003cbyte[]\u003e(), TimeSpan.FromMinutes(5));\n\n// Or similarly for async executions returning string results:\nvar cachePolicy = Policy.CacheAsync\u003cstring\u003e(distributedCache.AsAsyncCacheProvider\u003cstring\u003e(), TimeSpan.FromMinutes(5));\n\n// Or similarly for async executions returning  byte[] results:\nvar cachePolicy = Policy.CacheAsync\u003cbyte[]\u003e(distributedCache.AsAsyncCacheProvider\u003cbyte[]\u003e(), TimeSpan.FromMinutes(5));\n\n// You can also use ASP.NET Core's DistributedCacheEntryOptions for specifying cache item time-to-live, as shown below. \n// All time-to-live functionality represented by DistributedCacheEntryOptions is supported.\nDistributedCacheEntryOptions entryOptions = // ...\nvar cachePolicy = Policy.CacheAsync\u003cbyte[]\u003e(distributedCache.AsAsyncCacheProvider\u003cbyte[]\u003e(), entryOptions.AsTtlStrategy());\n \n\n```\n\n## Configuration via DI in ASPNET Core:\n\n```csharp\n// In this example we choose to pass a whole PolicyRegistry by dependency injection rather than the individual policy, on the assumption the webapp will probably use multiple policies across the app.\n\n// For example: \npublic class Startup\n{\n    public void ConfigureServices(IServiceCollection services)\n    {\n        services.AddDistributedRedisCache(options =\u003e\n        {\n            options.Configuration = \"localhost\"; // or whatever\n            options.InstanceName = \"SampleInstance\";\n        });\n\n        services.AddSingleton\u003cPolly.Caching.IAsyncCacheProvider\u003cstring\u003e\u003e(serviceProvider =\u003e serviceProvider.GetRequiredService\u003cIDistributedCache\u003e().AsAsyncCacheProvider\u003cstring\u003e());\n\n        services.AddSingleton\u003cPolly.Registry.IReadOnlyPolicyRegistry\u003cstring\u003e, Polly.Registry.PolicyRegistry\u003e((serviceProvider) =\u003e\n        {\n            PolicyRegistry registry = new PolicyRegistry();\n            registry.Add(\"myCachePolicy\", Policy.CacheAsync\u003cstring\u003e(serviceProvider.GetRequiredService\u003cIAsyncCacheProvider\u003cstring\u003e\u003e(), TimeSpan.FromMinutes(5)));\n\n            return registry;\n            });\n\n        // ...\n    }\n}\n\n// In a controller, inject the policyRegistry and retrieve the policy:\n// (magic string \"myCachePolicy\" hard-coded here only to keep the example simple) \npublic MyController(IReadOnlyPolicyRegistry\u003cstring\u003e policyRegistry)\n{\n    var _cachePolicy = policyRegistry.Get\u003cIAsyncPolicy\u003cstring\u003e\u003e(\"myCachePolicy\"); \n    // ...\n}\n```\n\n## Automatically serializing more complex type\n\nThe raw cache provider `Polly.Caching.IDistributedCache` allows you to cache items of type `byte[]` or `string` as those are the native formats supported by [`Microsoft.Extensions.Caching.Distributed.IDistributedCache`](https://docs.microsoft.com/en-gb/dotnet/api/microsoft.extensions.caching.distributed.idistributedcache).  However, Polly also allows you to automatically serialize more complex types.\n\nThe package `Polly.Caching.Serialization.Json` ([github](https://github.com/App-vNext/Polly.Caching.Serialization.Json); [nuget](https://www.nuget.org/packages/Polly.Caching.Serialization.Json)) is a Polly [`ICacheItemSerializer\u003cTResult, string\u003e`](https://github.com/App-vNext/Polly/wiki/Implementing-cache-serializers#using-a-serializer-with-the-polly-cachepolicy) to serialize any type for use with `Polly.Caching.IDistributedCache`.  \n\nConfiguration in .NET Core:\n\n```csharp\npublic class Startup\n{\n    public void ConfigureServices(IServiceCollection services)\n    {\n        services.AddDistributedRedisCache(options =\u003e\n        {\n            options.Configuration = \"localhost\"; // or whatever\n            options.InstanceName = \"SampleInstance\";\n        });\n\n        // Obtain a Newtonsoft.Json.JsonSerializerSettings defining any settings to use for serialization\n        // (could alternatively be obtained from a factory by DI)\n        var serializerSettings = new JsonSerializerSettings()\n        {\n            // Any configuration options\n        };\n\n        // Register a Polly cache provider for caching ProductDetails entities, using the IDistributedCache instance and a Polly.Caching.Serialization.Json.JsonSerializer.\n        // (ICacheItemSerializer\u003cProductDetails, string\u003e could alternatively be obtained from a factory by DI)\n        services.AddSingleton\u003cPolly.Caching.IAsyncCacheProvider\u003cProductDetails\u003e\u003e(serviceProvider =\u003e\n            serviceProvider\n                .GetRequiredService\u003cIDistributedCache\u003e()\n                .AsAsyncCacheProvider\u003cstring\u003e()\n                .WithSerializer\u003cProductDetails, string\u003e(\n                    new Polly.Caching.Serialization.Json.JsonSerializer\u003cProductDetails\u003e(serializerSettings)\n                );\n\n        // Register a Polly cache policy for caching ProductDetails entities, using that IDistributedCache instance.\n        services.AddSingleton\u003cPolly.Registry.IReadOnlyPolicyRegistry\u003cstring\u003e, Polly.Registry.PolicyRegistry\u003e((serviceProvider) =\u003e\n        {\n            PolicyRegistry registry = new PolicyRegistry();\n            registry.Add(\"productsCachePolicy\", Policy.CacheAsync\u003cProductDetails\u003e(serviceProvider.GetRequiredService\u003cIAsyncCacheProvider\u003cProductDetails\u003e\u003e(), TimeSpan.FromMinutes(5)));\n\n            return registry;\n        });\n\n        // ...\n    }\n}\n\n// In a controller, inject the policyRegistry and retrieve the policy:\n// (magic string \"productsCachePolicy\" hard-coded here only to keep the example simple) \npublic MyController(IReadOnlyPolicyRegistry\u003cstring\u003e policyRegistry)\n{\n    var _cachePolicy = policyRegistry.Get\u003cIAsyncPolicy\u003cProductDetails\u003e\u003e(\"productsCachePolicy\"); \n    // ...\n}\n```\n\n## Usage at the point of consumption\n\n```csharp\nstring productId = // ... from somewhere\nstring productDescription = await _cachePolicy.ExecuteAsync(context =\u003e getProductDescription(productId), \n    new Context(productId) // productId will also be the cache key used in this execution.\n); \n```\n\nFor 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.\n\n`CachePolicy` can of course also be combined with other policies in a [`PolicyWrap`](https://github.com/App-vNext/Polly/wiki/PolicyWrap).\n\n# Release notes\n\nFor details of changes by release see the [change log](CHANGELOG.md).  \n\n\n# Acknowledgements\n\n* [@seanfarrow](https://github.com/seanfarrow) and [@reisenberger](https://github.com/reisenberger) - Initial caching architecture in the main Polly repo\n* [@reisenberger](https://github.com/reisenberger) - `IDistributedCache` implementation\n* [@seanfarrow](https://github.com/seanfarrow) - v2.0 update to Signed packages only to correspond with Polly v6.0.1\n* [@reisenberger](https://github.com/reisenberger) - Update to Polly v7.0.0\n\n# Instructions for Contributing\n\nPlease 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!\n\nSince 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).\n\nAlso, 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!\n\n# License\n\nLicensed under the terms of the [New BSD License](http://opensource.org/licenses/BSD-3-Clause)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapp-vnext%2Fpolly.caching.idistributedcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fapp-vnext%2Fpolly.caching.idistributedcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fapp-vnext%2Fpolly.caching.idistributedcache/lists"}