Skip to content

Implementing new cache providers

martincostello edited this page Sep 28, 2023 · 5 revisions

Cache: 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.

ISyncCacheProvider

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 any TResult
  • also by generic CachePolicy<TResult> policies governing executions .Execute<TResult>(... Func<TResult>) strongly-typed to TResult.

IAsyncCacheProvider

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

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).

Clone this wiki locally