CachePolicy
A cache policy that can be applied to the results of delegate executions.
using Polly.Utilities;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Polly.Caching
{
public class CachePolicy : Policy
{
private readonly ISyncCacheProvider _syncCacheProvider;
private readonly ITtlStrategy _ttlStrategy;
private readonly Func<Context, string> _cacheKeyStrategy;
private readonly Action<Context, string> _onCacheGet;
private readonly Action<Context, string> _onCacheMiss;
private readonly Action<Context, string> _onCachePut;
private readonly Action<Context, string, Exception> _onCacheGetError;
private readonly Action<Context, string, Exception> _onCachePutError;
private readonly IAsyncCacheProvider _asyncCacheProvider;
internal CachePolicy(ISyncCacheProvider syncCacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError)
: base(delegate(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken) {
action(context, cancellationToken);
}, PredicateHelper.EmptyExceptionPredicates)
{
_syncCacheProvider = syncCacheProvider;
_ttlStrategy = ttlStrategy;
_cacheKeyStrategy = cacheKeyStrategy;
_onCacheGet = onCacheGet;
_onCachePut = onCachePut;
_onCacheMiss = onCacheMiss;
_onCacheGetError = onCacheGetError;
_onCachePutError = onCachePutError;
}
public override TResult Execute<TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
{
return CacheEngine.Implementation(_syncCacheProvider.For<TResult>(), _ttlStrategy, _cacheKeyStrategy, action, context, cancellationToken, _onCacheGet, _onCacheMiss, _onCachePut, _onCacheGetError, _onCachePutError);
}
internal CachePolicy(IAsyncCacheProvider asyncCacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError)
: base((Func<Context, CancellationToken, Task> func, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) => func(context, cancellationToken), PredicateHelper.EmptyExceptionPredicates)
{
_asyncCacheProvider = asyncCacheProvider;
_ttlStrategy = ttlStrategy;
_cacheKeyStrategy = cacheKeyStrategy;
_onCacheGet = onCacheGet;
_onCachePut = onCachePut;
_onCacheMiss = onCacheMiss;
_onCacheGetError = onCacheGetError;
_onCachePutError = onCachePutError;
}
public override Task<TResult> ExecuteAsync<TResult>(Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
return CacheEngine.ImplementationAsync(_asyncCacheProvider.AsyncFor<TResult>(), _ttlStrategy, _cacheKeyStrategy, action, context, cancellationToken, continueOnCapturedContext, _onCacheGet, _onCacheMiss, _onCachePut, _onCacheGetError, _onCachePutError);
}
}
}