CacheEngine
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Polly.Caching
{
internal static class CacheEngine
{
[System.Runtime.CompilerServices.NullableContext(1)]
[DebuggerDisableUserUnhandledExceptions]
internal static TResult Implementation<[System.Runtime.CompilerServices.Nullable(2)] TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy<TResult> ttlStrategy, Func<Context, string> cacheKeyStrategy, Func<Context, CancellationToken, TResult> action, Context context, 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, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
string text = cacheKeyStrategy(context);
if (text == null)
return action(context, cancellationToken);
bool flag;
TResult result;
try {
(bool, TResult) valueTuple = cacheProvider.TryGet(text);
flag = valueTuple.Item1;
result = valueTuple.Item2;
} catch (Exception arg) {
flag = false;
result = default(TResult);
onCacheGetError?.Invoke(context, text, arg);
}
if (flag) {
onCacheGet(context, text);
return result;
}
onCacheMiss(context, text);
TResult val = action(context, cancellationToken);
Ttl ttl = ttlStrategy.GetTtl(context, val);
if (ttl.Timespan > TimeSpan.Zero)
try {
cacheProvider.Put(text, val, ttl);
onCachePut(context, text);
return val;
} catch (Exception arg2) {
if (onCachePutError == null)
return val;
onCachePutError(context, text, arg2);
return val;
}
return val;
}
}
}