1. Nuget instalē attiecīgo dll
Web API 2 : Instalēšanas pakotne Strathweb.CacheOutput.WebApi2 Web API 1: Install-Package Strathweb.CacheOutput
Divi. Jauna ActionFilterAttribute izveide un saistīto metožu ignorēšana
publiskā klase WebApiOutputCacheAttribute : ActionFilterAttribute { Kešatmiņas laiks/sek privāts int _timespan; Klienta kešatmiņas laiks/sek privāts int _clientTimeSpan; Vai to kešatmiņā saglabā anonīmi lietotāji privāts bool _anonymousOnly; Indeksa atslēgas kešatmiņa privāta stīgu _cachekey; Kešatmiņas noliktavas privāts statisks tikai lasāms ObjectCache WebApiCache = MemoryCache.Default;
public WebApiOutputCacheAttribute(int timespan, int clientTimeSpan, bool anonymousOnly) { _timespan = laika posms; _clientTimeSpan = clientTimeSpan; _anonymousOnly = anonīmsTikai; }
//是否缓存 privāts bool _isCacheable(HttpActionContext ac) { ja (_timespan > 0 & _clientTimeSpan > 0) { ja (_anonymousOnly) if (Thread.CurrentPrincipal.Identity.IsAuthenticated) atgriešanās nepatiesa; Ja (ac. Request.Method == HttpMethod.Get) atgriež true; } citādi { mest jaunu InvalidOperationException ("Nepareizi argumenti"); } atgriešanās nepatiesa; }
privāts CacheControlHeaderValue setClientCache() { var cachecontrol = jauns CacheControlHeaderValue(); kešatmiņas kontrole. MaxAge = TimeSpan.FromSeconds(_clientTimeSpan); kešatmiņas kontrole. MustRevalidate = true; atgriezt kešatmiņu; }
//Action调用前执行的方法 publiskā ignorēšana void OnActionExecuting(HttpActionContext ac) { if (ac != null) { ja (_isCacheable) apakšpunkts) { _cachekey = virkne. Join(":", jauna virkne[] { ac. Request.RequestUri.AbsolutePath, ac. Request.Headers.Accept.FirstOrDefault(). ToString() }); if (WebApiCache.Contains(_cachekey)) { var val = (virkne)WebApiCache.Get(_cachekey); if (val != null) { mačs. Atbilde = ac. Request.CreateResponse(); mačs. Response.Content = jauns StringContent(val); var contenttype = (MediaTypeHeaderValue)WebApiCache.Get(_cachekey + ":response-ct"); if (satura tips == null) contenttype = jauns MediaTypeHeaderValue(_cachekey. Split(':')[1]); mačs. Response.Content.Headers.ContentType = contenttype; mačs. Response.Headers.CacheControl = setClientCache(); atgriešanās; } } } } citādi { mest jaunu ArgumentNullException("actionContext"); } }
//Action调用后执行方法 publiskā ignorēšana void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { ja (!( WebApiCache.Contains(_cachekey))) { var body = actionExecutedContext.Response.Content.ReadAsStringAsync(). Rezultāts; WebApiCache.Add(_cachekey, pamatteksts, 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. Lai pievienotu filtru, kontrolierim ir jāpievieno kešatmiņa metodei Get
[WebApiOutputCache(120,60,false)] publiskā virkne GetShoppingCart() { atgriezties "Hello World"; } Sāciet, ievērojiet lūzuma punktu un novērojiet efektu. Viss process ir: startēšanas laikā inicializējiet kešatmiņas filtru un pēc tam izsauciet metodi Get ar pievienoto filtru, ievadiet metodi OnActionFulsing, nosakiet, vai ir atbilstoša kešatmiņa, ja tāda ir, atgrieziet rezultātu tieši, ja nē, izsauciet kontroliera darbību un pēc tam izsauciet metodi OnActionExecute, lai pievienotu attiecīgo kešatmiņas atslēgas un vērtības pāri un iestatītu kešatmiņas derīguma termiņu, lai atgrieztu rezultātu. |