PollyCaching 1.0.1

dotnet add package PollyCaching --version 1.0.1
                    
NuGet\Install-Package PollyCaching -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PollyCaching" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PollyCaching" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="PollyCaching" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PollyCaching --version 1.0.1
                    
#r "nuget: PollyCaching, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PollyCaching@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PollyCaching&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=PollyCaching&version=1.0.1
                    
Install as a Cake Tool

Polly.Contrib.Caching

<img src="icon.png" width="100" align="right" />

NuGet NuGet Downloads CI

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

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:

Sponsor

💼 Need .NET resilience help? Visit solidqualitysolutions.com for consulting and architecture services.

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 Downloads ASP.NET Core health checks for Polly v8 circuit breakers
PollyOpenAI Downloads Polly v8 resilience for OpenAI and Azure OpenAI — retry on 429, Retry-After, circuit breaker
PollyRedis Downloads Polly v8 resilience for StackExchange.Redis — retry, circuit breaker, timeout
PollySignalR Downloads 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

🌐 swevo.github.io

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 63 7/7/2026
1.0.0 83 6/23/2026

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.