<PackageReference Include="Polly" Version="5.6.1" />

CacheEngine

static class CacheEngine
using System; using System.Threading; using System.Threading.Tasks; namespace Polly.Caching { internal static class CacheEngine { internal static TResult Implementation<TResult>(ISyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Func<Context, CancellationToken, TResult> action, Context context, CancellationToken cancellationToken, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { cancellationToken.ThrowIfCancellationRequested(); string text = cacheKeyStrategy(context); if (text == null) return action(context, cancellationToken); TResult val; try { val = cacheProvider.Get(text); } catch (Exception arg) { val = default(TResult); onCacheGetError(context, text, arg); } if (val != null && !val.Equals(default(TResult))) { onCacheGet(context, text); return val; } onCacheMiss(context, text); TResult val2 = action(context, cancellationToken); Ttl ttl = ttlStrategy.GetTtl(context); if (ttl.Timespan > TimeSpan.Zero) try { cacheProvider.Put(text, val2, ttl); onCachePut(context, text); return val2; } catch (Exception arg2) { onCachePutError(context, text, arg2); return val2; } return val2; } internal static async Task<TResult> ImplementationAsync<TResult>(IAsyncCacheProvider<TResult> cacheProvider, ITtlStrategy ttlStrategy, Func<Context, string> cacheKeyStrategy, Func<Context, CancellationToken, Task<TResult>> action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext, Action<Context, string> onCacheGet, Action<Context, string> onCacheMiss, Action<Context, string> onCachePut, Action<Context, string, Exception> onCacheGetError, Action<Context, string, Exception> onCachePutError) { cancellationToken.ThrowIfCancellationRequested(); string cacheKey = cacheKeyStrategy(context); if (cacheKey == null) return await action(context, cancellationToken).ConfigureAwait(continueOnCapturedContext); TResult val; try { val = await cacheProvider.GetAsync(cacheKey, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); } catch (Exception arg) { val = default(TResult); onCacheGetError(context, cacheKey, arg); } if (val != null && !val.Equals(default(TResult))) { onCacheGet(context, cacheKey); return val; } onCacheMiss(context, cacheKey); TResult result = await action(context, cancellationToken).ConfigureAwait(continueOnCapturedContext); Ttl ttl = ttlStrategy.GetTtl(context); if (ttl.Timespan > TimeSpan.Zero) try { await cacheProvider.PutAsync(cacheKey, result, ttl, cancellationToken, continueOnCapturedContext).ConfigureAwait(continueOnCapturedContext); onCachePut(context, cacheKey); } catch (Exception arg2) { onCachePutError(context, cacheKey, arg2); } return result; } } }