// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IPolicyRegistry <string> policyRegistry, IAsyncCacheProvider memoryCache) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } Func <Context, HttpResponseMessage, Ttl> ttlFilter = (context, result) => new Ttl(result.IsSuccessStatusCode ? TimeSpan.FromSeconds(30) : TimeSpan.Zero); AsyncCachePolicy <HttpResponseMessage> policy = Policy.CacheAsync(memoryCache.AsyncFor <HttpResponseMessage>(), new ResultTtl <HttpResponseMessage>(ttlFilter)); policyRegistry.Add("cache", policy); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public CacheManagement(ILogger logger, IPolicyRegistry <string> registry, IAsyncCacheProvider cacheProvider, IMemoryCacheExtended cache) { this.logger = logger; this.registry = registry; this.cacheProvider = cacheProvider.AsyncFor <CacheFormat>(); this.cache = cache; }
/// <summary> /// <para>Builds a <see cref="Policy" /> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para> /// <para>Before executing a delegate, checks whether the <paramref name="cacheProvider" /> holds a value for the cache key determined by applying the <paramref name="cacheKeyStrategy"/> to the execution <see cref="Context"/>. /// If the <paramref name="cacheProvider" /> provides a value, returns that value and does not execute the governed delegate. If the <paramref name="cacheProvider" /> does not provide a value, executes the governed delegate, stores the value with the <paramref name="cacheProvider" />, then returns the value. /// </para> /// </summary> /// <param name="cacheProvider">The cache provider.</param> /// <param name="ttl">Duration (ttl) for which to cache values.</param> /// <param name="cacheKeyStrategy">The cache key strategy.</param> /// <param name="onCacheError">Delegate to call if an exception is thrown when attempting to get a value from or put a value into the cache, passing the execution context, the cache key, and the exception.</param> /// <returns>The policy instance.</returns> /// <exception cref="ArgumentNullException">cacheKeyStrategy</exception> /// <exception cref="ArgumentNullException">cacheProvider</exception> public static CachePolicy <TResult> CacheAsync <TResult>(IAsyncCacheProvider cacheProvider, TimeSpan ttl, Func <Context, string> cacheKeyStrategy, Action <Context, string, Exception> onCacheError = null) { if (cacheProvider == null) { throw new ArgumentNullException(nameof(cacheProvider)); } return(CacheAsync <TResult>(cacheProvider.AsyncFor <TResult>(), new RelativeTtl(ttl), cacheKeyStrategy, onCacheError)); }
/// <summary> /// <para>Builds a <see cref="Policy"/> that will function like a result cache for delegate executions returning a <typeparamref name="TResult"/>.</para> /// <para>Before executing a delegate, checks whether the <paramref name="cacheProvider"/> holds a value for the cache key specified by <see cref="M:Context.ExecutionKey"/>. /// If the <paramref name="cacheProvider"/> provides a value, returns that value and does not execute the governed delegate. If the <paramref name="cacheProvider"/> does not provide a value, executes the governed delegate, stores the value with the <paramref name="cacheProvider"/>, then returns the value. /// </para> /// </summary> /// <param name="cacheProvider">The cache provider.</param> /// <param name="ttlStrategy">A strategy for specifying ttl for values to be cached.</param> /// <param name="onCacheError">Delegate to call if an exception is thrown when attempting to get a value from or put a value into the cache, passing the execution context, the cache key, and the exception.</param> /// <returns>The policy instance.</returns> /// <exception cref="ArgumentNullException">cacheProvider</exception> /// <exception cref="ArgumentNullException">ttlStrategy</exception> public static CachePolicy <TResult> CacheAsync <TResult>(IAsyncCacheProvider cacheProvider, ITtlStrategy ttlStrategy, Action <Context, string, Exception> onCacheError = null) { if (cacheProvider == null) { throw new ArgumentNullException(nameof(cacheProvider)); } return(CacheAsync <TResult>(cacheProvider.AsyncFor <TResult>(), ttlStrategy, DefaultCacheKeyStrategy.Instance.GetCacheKey, onCacheError)); }
/// <summary> /// Executes the specified action asynchronously within the cache policy and returns the result. /// </summary> /// <typeparam name="TResult">The type of the result.</typeparam> /// <param name="action">The action to perform.</param> /// <param name="context">Execution context that is passed to the exception policy; defines the cache key to use in cache lookup.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <param name="continueOnCapturedContext">Whether to continue on a captured synchronization context.</param> /// <returns>The value returned by the action, or the cache.</returns> public override Task <TResult> ExecuteAsync <TResult>(Func <Context, CancellationToken, Task <TResult> > action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { return(CacheEngine.ImplementationAsync <TResult>( _asyncCacheProvider.AsyncFor <TResult>(), _ttlStrategy, _cacheKeyStrategy, action, context, cancellationToken, continueOnCapturedContext, _onCacheGet, _onCacheMiss, _onCachePut, _onCacheGetError, _onCachePutError)); }
internal override Task <TResult> ExecuteAsyncInternal <TResult>(Func <Context, CancellationToken, Task <TResult> > action, Context context, CancellationToken cancellationToken, bool continueOnCapturedContext) { if (_asyncCacheProvider == null) { throw new InvalidOperationException("Please use asynchronous-defined policies when calling asynchronous ExecuteAsync (and similar) methods."); } return(CacheEngine.ImplementationAsync <TResult>( _asyncCacheProvider.AsyncFor <TResult>(), _ttlStrategy.For <TResult>(), _cacheKeyStrategy, action, context, cancellationToken, continueOnCapturedContext, _onCacheGet, _onCacheMiss, _onCachePut, _onCacheGetError, _onCachePutError)); }