https://github.com/app-vnext/polly.caching.memorycache
MemoryCache plugin for the Polly Cache policy
https://github.com/app-vnext/polly.caching.memorycache
cache caching memorycache polly
Last synced: 10 months ago
JSON representation
MemoryCache plugin for the Polly Cache policy
- Host: GitHub
- URL: https://github.com/app-vnext/polly.caching.memorycache
- Owner: App-vNext
- License: other
- Created: 2016-10-15T18:28:28.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-06-25T09:52:22.000Z (over 5 years ago)
- Last Synced: 2025-04-14T09:18:42.908Z (10 months ago)
- Topics: cache, caching, memorycache, polly
- Language: C#
- Homepage:
- Size: 111 KB
- Stars: 44
- Watchers: 21
- Forks: 19
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Polly.Caching.Memory
This repo contains the MemoryCache plugin 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.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).
[](https://badge.fury.io/nu/Polly.Caching.Memory) [](https://ci.appveyor.com/project/joelhulen/polly-caching-Memory) [](http://www.pollytalk.org)
## What is Polly?
[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 is a member of the [.NET Foundation](https://www.dotnetfoundation.org/about)!
**Keep up to date with new feature announcements, tips & tricks, and other news through [www.thepollyproject.org](http://www.thepollyproject.org)**

# Installing Polly.Caching.Memory via NuGet
Install-Package Polly.Caching.Memory
# Supported targets
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
## Dependency compatibility with Polly
Polly.Caching.Memory >=v3.0.2 requires:
+ [Polly](https://nuget.org/packages/polly) >= v7.1.1.
Polly.Caching.Memory >=v3.0 amd <v3.0.2 requires:
+ [Polly](https://nuget.org/packages/polly) >= v7.0.0.
Polly.Caching.Memory >=v2.0.1 and <v3 requires:
+ [Polly](https://nuget.org/packages/polly) >= v6.1.1 and <v7.
Polly.Caching.Memory v2.0.0 requires:
+ [Polly](https://nuget.org/packages/polly) >= v6.0.1 and <=v6.1.0.
Polly.Caching.MemoryCache v1.* requires:
+ [Polly](https://nuget.org/packages/polly) >=v5.9.0 and <v6.
# How to use the Polly.Caching.Memory plugin
### Example: Direct creation of CachePolicy (no DI)
```csharp
// 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));
```
### Example: Configure CachePolicy via MemoryCacheProvider in StartUp, for DI
```csharp
// (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();
services.AddSingleton, Polly.Registry.PolicyRegistry>((serviceProvider) =>
{
PolicyRegistry registry = new PolicyRegistry();
registry.Add("myCachePolicy",
Policy.CacheAsync(
serviceProvider
.GetRequiredService()
.AsyncFor(),
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 policyRegistry)
{
var _cachePolicy = policyRegistry.Get>("myCachePolicy");
// ...
}
```
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).
## Note
`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(...)`, some overloads require a generic `ISyncCacheProvider` or `IAsyncCacheProvider`.
In this case, use the extensions methods `MemoryCacheProvider.For()` or `MemoryCacheProvider.AsyncFor()`, as shown in the ASP.NET Core example above, to obtain a generic `ISyncCacheProvider` or `IAsyncCacheProvider`.
# Release notes
For details of changes by release see the [change log](CHANGELOG.md).
# Acknowledgements
* [@reisenberger](https://github.com/reisenberger) - MemoryCache implementation
* [@seanfarrow](https://github.com/seanfarrow) and [@reisenberger](https://github.com/reisenberger) - Initial caching architecture in the main Polly repo
* [@kesmy](https://github.com/kesmy) - original structuring of the build for msbuild15, in the main Polly repo
* [@seanfarrow](https://github.com/seanfarrow) - v2.0 update to Signed packages only to correspond with Polly v6.0.1
* [@reisenberger](https://github.com/reisenberger) - Update to Polly v7.0.0
# Instructions for Contributing
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!
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).
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!
# License
Licensed under the terms of the [New BSD License](http://opensource.org/licenses/BSD-3-Clause)