PollyCaching 1.0.1
dotnet add package PollyCaching --version 1.0.1
NuGet\Install-Package PollyCaching -Version 1.0.1
<PackageReference Include="PollyCaching" Version="1.0.1" />
<PackageVersion Include="PollyCaching" Version="1.0.1" />
<PackageReference Include="PollyCaching" />
paket add PollyCaching --version 1.0.1
#r "nuget: PollyCaching, 1.0.1"
#:package PollyCaching@1.0.1
#addin nuget:?package=PollyCaching&version=1.0.1
#tool nuget:?package=PollyCaching&version=1.0.1
Polly.Contrib.Caching
<img src="icon.png" width="100" align="right" />
A caching resilience strategy for Polly v8 pipelines. Restores the beloved Polly v7 caching policy for the new DelayGenerator-based API.
Polly v8 dropped the built-in caching policy. This package brings it back as a proper ResilienceStrategy<T> that plugs into any ResiliencePipelineBuilder<TResult>.
Install
dotnet add package Polly.Contrib.Caching
Usage
With IMemoryCache (recommended)
using Microsoft.Extensions.Caching.Memory;
using Polly.Contrib.Caching;
var cache = new MemoryCache(new MemoryCacheOptions());
var pipeline = new ResiliencePipelineBuilder<HttpResponseMessage>()
.AddCaching(
cache,
cacheKeyProvider: ctx => ctx.Properties.TryGetValue(new ResiliencePropertyKey<string>("url"), out var url) ? url : null,
ttl: TimeSpan.FromMinutes(5))
.AddRetry(new RetryStrategyOptions<HttpResponseMessage>
{
MaxRetryAttempts = 3,
})
.Build();
With CachingStrategyOptions
var pipeline = new ResiliencePipelineBuilder<string>()
.AddCaching(new CachingStrategyOptions<string>
{
CacheProvider = new MemoryCacheProvider<string>(cache),
CacheKeyProvider = ctx => ctx.OperationKey,
Ttl = TimeSpan.FromMinutes(10),
})
.Build();
Custom ICacheProvider
Implement ICacheProvider<TResult> to use any cache store — distributed cache, Redis, etc.
public sealed class RedisCacheProvider<TResult>(IConnectionMultiplexer redis) : ICacheProvider<TResult>
{
public bool TryGet(string key, out TResult? value) { /* ... */ }
public void Set(string key, TResult? value, TimeSpan ttl) { /* ... */ }
}
Behaviour
| Scenario | Outcome |
|---|---|
| Cache hit | Returns cached result; downstream not called |
| Cache miss | Calls downstream; caches result on success |
| Downstream throws | Exception propagates; nothing cached |
CacheKeyProvider returns null |
Caching skipped; downstream always called |
Composition with other strategies
Place AddCaching before retry/circuit-breaker so a cached result short-circuits the entire pipeline:
var pipeline = new ResiliencePipelineBuilder<string>()
.AddCaching(cache, ctx => ctx.OperationKey) // 1. check cache first
.AddRetry(...) // 2. retry on failure
.AddCircuitBreaker(...) // 3. protect downstream
.Build();
Support
If Polly.Contrib.Caching saves you time restoring the v7 caching policy, consider supporting the project:
💼 Need .NET resilience help? Visit solidqualitysolutions.com for consulting and architecture services.
Related packages
| Package | Description | |
|---|---|---|
| PollyChaos | Chaos engineering — inject faults & latency (Simmy for v8) | |
| PollyMediatR | Polly v8 pipelines for MediatR request handlers | |
| PollyEFCore | Polly v8 resilience for EF Core queries and SaveChanges | |
| PollyBackoff | Backoff delay strategies | |
| PollyHealthChecks | ASP.NET Core health checks for Polly v8 circuit breakers | |
| PollyOpenAI | Polly v8 resilience for OpenAI and Azure OpenAI — retry on 429, Retry-After, circuit breaker | |
| PollyRedis | Polly v8 resilience for StackExchange.Redis — retry, circuit breaker, timeout | |
| PollySignalR | Polly v8 exponential back-off reconnect policy for SignalR HubConnection | |
| PollyGrpc | Polly v8 resilience (retry, CB, timeout) for gRPC .NET clients via Interceptor | |
| PollyKafka | Polly v8 resilience (retry, CB, timeout) for Confluent.Kafka producers and consumers | |
| PollyAzureServiceBus | Polly v8 resilience (retry, CB, timeout) for Azure Service Bus senders and receivers | |
| PollyBulkhead | Bulkhead isolation | |
| PollyRateLimiter | Rate limiting strategies | |
| PollyOpenTelemetry | OpenTelemetry metrics & tracing |
| PollyRabbitMQ | Polly v8 resilience for RabbitMQ.Client channels |
Also by the same author
| Package | Description |
|---|---|
| AutoLog.Generator | Compile-time high-performance logging — [Log(Level, Message)] generates LoggerMessage.Define. AOT-safe. |
| AutoHttpClient.Generator | Compile-time typed HTTP client — [HttpClient] on an interface generates a strongly-typed client. AOT-safe Refit alternative. |
| AutoDispatch.Generator | Compile-time CQRS dispatcher — [Handler] generates a strongly-typed IDispatcher. MediatR alternative. |
| AutoWire | Compile-time DI auto-registration — [Scoped]/[Singleton]/[Transient] generates IServiceCollection registration code. |
| AutoMap.Generator | Compile-time object mapping — [Map(typeof(Dto))] generates ToDto() extension methods. AutoMapper alternative. |
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.Caching.Memory (>= 8.0.1)
- Polly.Core (>= 8.7.0)
-
net8.0
- Microsoft.Extensions.Caching.Memory (>= 8.0.1)
- Polly.Core (>= 8.7.0)
-
net9.0
- Microsoft.Extensions.Caching.Memory (>= 8.0.1)
- Polly.Core (>= 8.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.0.1: Fix package icon (was showing an incorrect/placeholder image). 1.0.1: Initial NuGet release. Caching resilience strategy for Polly v8 resilience pipelines. Supports net6.0, net8.0, and net9.0.