This repo contains the MemoryCache plugin for the Polly Cache policy. The current version targets .NET Standard 1.3 (for .NET Core1.x), .NET Standard 2.0 (for .NET Core 2.x and .NET Framework 4.x), and .NET Standard 2.1 (for .NET Core 3.x).
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 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.Memory
Polly.Caching.Memory >= v3.0.2 supports .NET Standard 1.3, .NET Standard 2.0 and .NET Standard 2.1.
Polly.Caching.Memory >= v2.0 supports .NET Standard 1.3 and .NET Standard 2.0.
Polly.Caching.MemoryCache <v2.0 supports .NET4.0, .NET4.5 and .NetStandard 1.3
Polly.Caching.Memory >=v3.0.2 requires:
- Polly >= v7.1.1.
Polly.Caching.Memory >=v3.0 amd <v3.0.2 requires:
- Polly >= v7.0.0.
Polly.Caching.Memory >=v2.0.1 and <v3 requires:
- Polly >= v6.1.1 and <v7.
Polly.Caching.Memory v2.0.0 requires:
- Polly >= v6.0.1 and <=v6.1.0.
Polly.Caching.MemoryCache v1.* requires:
- Polly >=v5.9.0 and <v6.
// This approach creates a CachePolicy directly, with its own Microsoft.Extensions.Caching.Memory.MemoryCache instance:
Microsoft.Extensions.Caching.Memory.IMemoryCache memoryCache
= new Microsoft.Extensions.Caching.Memory.MemoryCache(new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions());
Polly.Caching.Memory.MemoryCacheProvider memoryCacheProvider
= new Polly.Caching.Memory.MemoryCacheProvider(memoryCache);
// Create a Polly cache policy using that Polly.Caching.Memory.MemoryCacheProvider instance.
var cachePolicy = Policy.Cache(memoryCacheProvider, TimeSpan.FromMinutes(5));
// (We pass a whole PolicyRegistry by dependency injection rather than the individual policy,
// on the assumption the app will probably use multiple policies.)
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddSingleton<Polly.Caching.IAsyncCacheProvider, Polly.Caching.Memory.MemoryCacheProvider>();
services.AddSingleton<Polly.Registry.IReadOnlyPolicyRegistry<string>, Polly.Registry.PolicyRegistry>((serviceProvider) =>
{
PolicyRegistry registry = new PolicyRegistry();
registry.Add("myCachePolicy",
Policy.CacheAsync<HttpResponseMessage>(
serviceProvider
.GetRequiredService<IAsyncCacheProvider>()
.AsyncFor<HttpResponseMessage>(),
TimeSpan.FromMinutes(5)));
return registry;
});
// ...
}
}
// At the point of use, inject the policyRegistry and retrieve the policy:
// (magic string "myCachePolicy" only hard-coded here to keep the example simple)
public MyController(IReadOnlyPolicyRegistry<string> policyRegistry)
{
var _cachePolicy = policyRegistry.Get<IAsyncPolicy<HttpResponseMessage>>("myCachePolicy");
// ...
}
For many more configuration options and usage examples of the main Polly CachePolicy
, see the main Polly readme and deep doco on the Polly wiki.
Polly.Caching.Memory.MemoryCacheProvider : ISyncCacheProvider, IAsyncCacheProvider
is non-generic as the underlying Microsoft.Extensions.Caching.Memory.IMemoryCache
is non-generic. However, when defining a generic Polly cache policy Policy.Cache/CacheAsync<TResult>(...)
, some overloads require a generic ISyncCacheProvider<TResult>
or IAsyncCacheProvider<TResult>
.
In this case, use the extensions methods MemoryCacheProvider.For<TResult>()
or MemoryCacheProvider.AsyncFor<TResult>()
, as shown in the ASP.NET Core example above, to obtain a generic ISyncCacheProvider<TResult>
or IAsyncCacheProvider<TResult>
.
For details of changes by release see the change log.
- @reisenberger - MemoryCache implementation
- @seanfarrow and @reisenberger - Initial caching architecture in the main Polly repo
- @kesmy - original structuring of the build for msbuild15, in the main Polly repo
- @seanfarrow - v2.0 update to Signed packages only to correspond with Polly v6.0.1
- @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