public AuthenticationApiClientCachingDecorator(IAuthenticationApiClient inner) { _inner = inner; _accessTokenResponseCachePolicy = Policy.CacheAsync( _memoryCacheProvider.AsyncFor <AccessTokenResponse>(), new ResultTtl <AccessTokenResponse>(r => new Ttl(_expiresIn(r), false))); _userInfoCachePolicy = Policy.CacheAsync( _memoryCacheProvider.AsyncFor <UserInfo>(), new ContextualTtl()); }
private CachePolicy <HttpResponseMessage> CreateCachePolicy(MemoryCacheProvider provider) { return(Policy .CacheAsync( provider.AsyncFor <HttpResponseMessage>(), new ResultTtl <HttpResponseMessage>(message => new Ttl(message.Headers.CacheControl.MaxAge ?? TimeSpan.FromHours(24))) )); }
public void Configure(string cacheString = null) { //TIMEOUT var timeoutPolicy = Policy .TimeoutAsync <HttpResponseMessage>(60); //ECCEZIONI E RESPONSE var retryPolicy = Policy.HandleResult <HttpResponseMessage>(c => { if (c.StatusCode != HttpStatusCode.OK) { var exception = new ExternalApiLog() { Content = c.RequestMessage.Method.Method.Equals("GET") ? c.RequestMessage.RequestUri.Query : c.RequestMessage.Content.ReadAsStringAsync().Result, DataOraEsecuzione = DateTime.Now, Response = string.IsNullOrEmpty(c.Content.ReadAsStringAsync().Result) ? c.ReasonPhrase : c.Content.ReadAsStringAsync().Result, Servizio = c.RequestMessage.RequestUri.Host + c.RequestMessage.RequestUri.LocalPath, CodComando = _httpContext.HttpContext.Request.Headers["codiceSede"], IdOperatore = _httpContext.HttpContext.Request.Headers["IdUtente"] }; _writeLog.Save(exception); switch (c.StatusCode) { case HttpStatusCode.NotFound: throw new Exception(Costanti.ES.ServizioNonRaggiungibile); case HttpStatusCode.Forbidden: throw new Exception(Costanti.ES.AutorizzazioneNegata); case HttpStatusCode.UnprocessableEntity: throw new Exception(Costanti.ES.DatiMancanti); case HttpStatusCode.InternalServerError: throw new Exception(Costanti.ES.ErroreInternoAlServer); case HttpStatusCode.Created: throw new Exception(Costanti.ES.NonTuttiIDatiInviatiSonoStatiProcessati); case HttpStatusCode.UnsupportedMediaType: throw new Exception(Costanti.ES.OggettoNonValido); case 0: throw new Exception(c.ReasonPhrase); } } return(false); }).RetryAsync(3); //CACHE if (!string.IsNullOrEmpty(cacheString)) { var memoryCacheProvider = new MemoryCacheProvider(_memoryCache); var cachePolicy = Policy.CacheAsync(memoryCacheProvider.AsyncFor <HttpResponseMessage>(), TimeSpan.FromHours(8), c => cacheString); policies = Policy.WrapAsync(cachePolicy, retryPolicy, timeoutPolicy); } else { policies = Policy.WrapAsync(retryPolicy, timeoutPolicy); } }