1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Threading;5 usingSystem.Threading.Tasks;6 usingsystem.web;7 usingSystem.Web.Caching;8 usingSystem.Web.Http;9 usingSystem.Web.Http.Controllers;Ten usingSystem.Web.Http.Filters; One A namespaceWebApi - { -[AttributeUsage (AttributeTargets.Class |AttributeTargets.Method)] the Public classCacheattribute:actionfilterattribute - { - Public stringabsoluteexpiration {Get;Set; } - Public stringslidingexpiration {Get;Set; } + PublicCacheItemPriority Priority {Get;Set; } - + PublicCacheattribute () A { atPriority =Cacheitempriority.normal; - } - - Public Override voidonactionexecuting (httpactioncontext actioncontext) - { - varActiondescriptor = Actioncontext.actiondescriptor asReflectedhttpactiondescriptor; in if(NULL==actiondescriptor) - { to Base. OnActionExecuting (actioncontext); + } - varKey =NewCacheKey (Actiondescriptor.methodinfo, actioncontext.actionarguments); the varArray = HttpRuntime.Cache.Get (key. ToString ()) as Object[]; * if(NULL==Array) $ {Panax Notoginseng Base. OnActionExecuting (actioncontext); - return; the } + varValue = array. Any ()? array[0] :NULL; A varActionResult = value asIhttpactionresult; the if(NULL!=ActionResult) + { -Actioncontext.response =Actionresult.executeasync (Cancellationtoken.none). Result; $ return; $ } -Actioncontext.response =ActionDescriptor.ResultConverter.Convert (actioncontext.controllercontext, value); - } the } - Wuyi Public classCacheableactiondescriptor:reflectedhttpactiondescriptor the { - PrivateCacheattribute Cacheattribute {Get; } Wu - PublicCacheableactiondescriptor (Reflectedhttpactiondescriptor actiondescriptor, Cacheattribute CacheAttribute):Base(Actiondescriptor.controllerdescriptor, Actiondescriptor.methodinfo) About { $Cacheattribute =Cacheattribute; - } - - Public Overridetask<Object> Executeasync (httpcontrollercontext controllercontext, idictionary<string,Object>arguments, CancellationToken cancellationtoken) A { + varabsoluteexpiration =cache.noabsoluteexpiration; the varslidingexpiration =cache.noslidingexpiration; - varPriority =cacheattribute.priority; $ if(!string. Isnullorwhitespace (cacheattribute.absoluteexpiration)) the { theabsoluteexpiration = DateTime.Now +Timespan.parse (cacheattribute.absoluteexpiration); the } the if(!string. Isnullorwhitespace (cacheattribute.slidingexpiration)) - { inslidingexpiration =Timespan.parse (cacheattribute.slidingexpiration); the } the varCacheKey =NewCacheKey (MethodInfo, arguments); About varresult =Base. Executeasync (ControllerContext, arguments, CancellationToken). Result; theHttpRuntime.Cache.Insert (Cachekey.tostring (),New[] {result},NULL, absoluteexpiration, slidingexpiration, priority,NULL); the returnTask.fromresult (result); the } + } - the Public classCacheableactionselector:apicontrolleractionselectorBayi { the Public Overridehttpactiondescriptor selectaction (httpcontrollercontext controllercontext) the { - varActiondescriptor =Base. SelectAction (controllercontext); - varReflectedactiondescriptor = Actiondescriptor asReflectedhttpactiondescriptor; the if(NULL==reflectedactiondescriptor) the { the returnActiondescriptor; the } - varCacheattribute = Reflectedactiondescriptor.getcustomattributes<cacheattribute> (). FirstOrDefault ()?? theReflectedactiondescriptor.controllerdescriptor.getcustomattributes<cacheattribute>(). FirstOrDefault (); the if(NULL==Cacheattribute) the {94 returnActiondescriptor; the } the return NewCacheableactiondescriptor (Reflectedactiondescriptor, cacheattribute); the }98 } About}
Register Actionselector
1 usingSystem;2 usingsystem.web;3 usingSystem.Web.Http;4 usingSystem.Web.Http.Controllers;5 usingWebApi;6 7 namespacewebhost8 {9 Public classglobal:httpapplicationTen { One protected voidApplication_Start (Objectsender, EventArgs e) A { - //registering the ASP. NET Web API Route -GlobalConfiguration.Configuration.Routes.MapHttpRoute ("default","Api/{controller}/{id}",New{id =routeparameter.optional}); the - - //Register Cacheableactionselector -GlobalConfiguration.Configuration.Services.Replace (typeof(Ihttpactionselector),Newcacheableactionselector ()); + } - } +}
Using Cacheattribute
1 Public classDemocontroller:apicontroller2 {3[Cache (absoluteexpiration ="00:00:02", slidingexpiration ="", priority =Cacheitempriority.high)]4 PublicDateTime Get ()5 {6 returnDateTime.Now;7 }8}
The ASP. NET Web API implements caching through Actionfilter