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

CachePolicy<TResult>

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