1. Nuget, ilgili DLL'yi yükler
Web API 2 : Strathweb.CacheOutput.WebApi2 Paketini Yükleme Web API 1 : Strathweb.CacheOutput Paketini Yükle.
İki. Yeni bir ActionFilterAttribute oluşturun ve ilgili yöntemleri geçersiz kılın
public class WebApiOutputCacheAttribute : ActionFilterAttribute { Önbellek süresi/saniye özel _timespan; İstemci önbellek süresi/saniyesi özel int _clientTimeSpan; Anonim kullanıcılar tarafından önbelleğe alınıp alınmadığı er Bool _anonymousOnly; İndeks anahtarını önbelle özel string _cachekey; Depo depoları özel statik okunabilir ObjectCache WebApiCache = MemoryCache.Default;
public WebApiOutputCacheAttribute(int timespan, int clientTimeSpan, bool anonymousOnly) { _timespan = zaman aralığı; _clientTimeSpan = clientTimeSpan; _anonymousOnly = anonymousOnly; }
//是否缓存 private bool _isCacheable(HttpActionContext ac) { eğer (_timespan > 0 && _clientTimeSpan > 0) { if (_anonymousOnly) if (Thread.CurrentPrincipal.Identity.IsAuthenticated) yanlış döndür; if (ac. Request.Method == HttpMethod.Get) true döndür; } else { yeni InvalidOperationException("Yanlış Argümanlar") atıyor; } yanlış döndür; }
private CacheControlHeaderValue setClientCache() { var cachecontrol = yeni CacheControlHeaderValue(); Önbellek kontrolü. MaxAge = TimeSpan.FromSeconds(_clientTimeSpan); Önbellek kontrolü. MustRevalidate = doğru; return cachecontrol; }
//Action调用前执行的方法 public override void OnActionExecuting(HttpActionContext ac) { eğer (ac != null) { if (_isCacheable(ac)) { _cachekey = ip. Join(":", new string[] { ac. Request.RequestUri.AbsolutePath, ac. Request.Headers.Accept.FirstOrDefault(). ToString() }); if (WebApiCache.Contains(_cachekey)) { var val = (string)WebApiCache.Get(_cachekey); if (val != null) { AC. Tepki = ac. Request.CreateResponse(); AC. Response.Content = yeni StringContent(val); var contenttype = (MediaTypeHeaderValue)WebApiCache.Get(_cachekey + ":response-ct"); if (contenttype == null) contenttype = yeni MediaTypeHeaderValue(_cachekey. Split(':')[1]); AC. Response.Content.Headers.ContentType = contenttype; AC. Response.Headers.CacheControl = setClientCache(); Dönüş; } } } } else { yeni ArgumentNullException("actionContext") atıyor; } }
//Action调用后执行方法 public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { if (!( WebApiCache.Contains(_cachekey))) { var body = actionExecutedContext.Response.Content.ReadAsStringAsync(). Sonuç; WebApiCache.Add(_cachekey, gövde, DateTime.Now.AddSeconds(_timespan)); WebApiCache.Add(_cachekey + ":response-ct", actionExecutedContext.Response.Content.Headers.ContentType, DateTime.Now.AddSeconds(_timespan)); } if (_isCacheable(actionExecutedContext.ActionContext)) actionExecutedContext.ActionContext.Response.Headers.CacheControl = setClientCache(); }
}
3. Kontrolcü, filtreyi eklemek için önbelleği Get yöntemine eklemesi gerekir
[WebApiOutputCache(120,60,yanlış)] public string GetShoppingCart() { geri dönen "Hello World"; } Başlayın, kırılma noktasını gözlemleyin ve etkiyi gözlemleyin. Tüm süreç şudur: önbellek filtresini başlatırken başlatmak ve ardından filtre eklenerek Get yöntemini çağırmak, OnActionExecuting yöntemini girmek, ilgili önbellek olup olmadığını belirlemek, varsa sonucu doğrudan döndürmek, yoksa denetleyicinin Eylemini çağırmak ve ardından OnActionExecuted metodunu çağırmak için ilgili önbellek anahtar-değer çiftini eklemek ve önbellek son kullanma süresini sonucu döndürecek şekilde ayarlamak. |