-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implementing new cache providers
ℹ️ This documentation describes the previous Polly v7 API. If you are using the new v8 API, please refer to pollydocs.org.
Polly CachePolicy
allows any third-party cache provider to be plugged into the CachePolicy
, via simple interfaces ISyncCacheProvider
and IAsyncCacheProvider
.
Both interfaces are simple to fulfil, to implement a new cache provider.
To Polly v6:
public interface ISyncCacheProvider
{
object Get(String key);
void Put(string key, object value, Ttl ttl);
}
public interface ISyncCacheProvider<TResult>
{
TResult Get(String key);
void Put(string key, TResult value, Ttl ttl);
}
From Polly v7:
public interface ISyncCacheProvider
{
(bool, object) TryGet(String key);
void Put(string key, object value, Ttl ttl);
}
public interface ISyncCacheProvider<TResult>
{
(bool, TResult) TryGet(String key);
void Put(string key, TResult value, Ttl ttl);
}
If the cache provider can only cache objects of a particular type (for example, some cache providers typically cache all items as strings), implement just the generic interface ISyncCacheProvider<string>
.
If the cache provider can cache objects of any type, implement the non-generic ISyncCacheProvider
. This provider will be able to be used:
- in non-generic
CachePolicy
instances, on which executions with the generic.Execute<TResult>()
method overload may be made for anyTResult
- also by generic
CachePolicy<TResult>
policies governing executions.Execute<TResult>(... Func<TResult>)
strongly-typed toTResult
.
To Polly v6:
public interface IAsyncCacheProvider
{
Task<object> GetAsync(String key, CancellationToken cancellationToken, bool continueOnCapturedContext);
Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext);
}
public interface IAsyncCacheProvider<TResult>
{
Task<TResult> GetAsync(String key, CancellationToken cancellationToken, bool continueOnCapturedContext);
Task PutAsync(string key, TResult value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext);
}
From Polly v7:
public interface IAsyncCacheProvider
{
Task<(bool, object)> TryGetAsync(String key, CancellationToken cancellationToken, bool continueOnCapturedContext);
Task PutAsync(string key, object value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext);
}
public interface IAsyncCacheProvider<TResult>
{
Task<(bool, TResult)> TryGetAsync(String key, CancellationToken cancellationToken, bool continueOnCapturedContext);
Task PutAsync(string key, TResult value, Ttl ttl, CancellationToken cancellationToken, bool continueOnCapturedContext);
}
Any await
calls in the implementation should be decorated .ConfigureAwait(continueOnCapturedContext)
.
The same comments regarding generic and non-generic versions as for ISyncCacheProvider
apply.
Community contributions of new third-party cache provider for Polly will be very welcome. Let us know, and we can make new providers available to the wider Polly community (with full credit to the original contributors).
- Home
- Polly RoadMap
- Contributing
- Transient fault handling and proactive resilience engineering
- Supported targets
- Retry
- Circuit Breaker
- Advanced Circuit Breaker
- Timeout
- Bulkhead
- Cache
- Rate-Limit
- Fallback
- PolicyWrap
- NoOp
- PolicyRegistry
- Polly and HttpClientFactory
- Asynchronous action execution
- Handling InnerExceptions and AggregateExceptions
- Statefulness of policies
- Keys and Context Data
- Non generic and generic policies
- Polly and interfaces
- Some policy patterns
- Debugging with Polly in Visual Studio
- Unit-testing with Polly
- Polly concept and architecture
- Polly v6 breaking changes
- Polly v7 breaking changes
- DISCUSSION PROPOSAL- Polly eventing and metrics architecture