CachePolicy
A cache policy that can be applied to the results of delegate executions.
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Polly.Caching
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class CachePolicy : Policy, ICachePolicy, IsPolicy
{
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;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})]
private readonly Action<Context, string, Exception> _onCacheGetError;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})]
private readonly Action<Context, string, Exception> _onCachePutError;
internal CachePolicy(ISyncCacheProvider syncCacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})] Action<Context, string, Exception> onCacheGetError, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})] Action<Context, string, Exception> onCachePutError)
: base((PolicyBuilder)null)
{
_syncCacheProvider = syncCacheProvider;
_ttlStrategy = ttlStrategy;
_cacheKeyStrategy = cacheKeyStrategy;
_onCacheGet = onCacheGet;
_onCachePut = onCachePut;
_onCacheMiss = onCacheMiss;
_onCacheGetError = onCacheGetError;
_onCachePutError = onCachePutError;
}
protected override void Implementation(Action<Context, CancellationToken> action, Context context, CancellationToken cancellationToken)
{
if (action == null)
throw new ArgumentNullException("action");
action(context, cancellationToken);
}
[DebuggerStepThrough]
protected override TResult Implementation<[System.Runtime.CompilerServices.Nullable(0)] TResult>(Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken)
{
if (action == null)
throw new ArgumentNullException("action");
return CacheEngine.Implementation(_syncCacheProvider.For<TResult>(), _ttlStrategy.For<TResult>(), _cacheKeyStrategy, action, context, _onCacheGet, _onCacheMiss, _onCachePut, _onCacheGetError, _onCachePutError, cancellationToken);
}
}
}