Preliminary look at the next Cachecow and OutputCache, feel or cacheoutput more in line with their own requirements, the use is very simple
Pm>install-package Strathweb.CacheOutput.WebApi2
Basic use
Cacheoutput characteristics
[Route ("get")] - - )] public ienumerable<string> Get () { return Newstring"Tom""Irving" }; }
take the parameter as key
[Route ("get")] - true )] publicstring Get (int ID) { return DateTime.Now.ToString (); }
ETag Header
Using Redis
The client uses Stackexchange.redis,Install-Package StackExchange.Redis
Registering a Redis connection in AUTOFAC
varBuilder =NewContainerbuilder (); //implementation of registering API containersBuilder. Registerapicontrollers (assembly.getexecutingassembly ()); //implementation of registering an MVC container//Builder. Registercontrollers (assembly.getexecutingassembly ()); //register a Redis connection in AUTOFAC and set it to SingletonBuilder. Register (R = { returnConnectionmultiplexer.connect (Dbsetting.redis); }). Asself (). SingleInstance (); varcontainer =Builder. Build (); GlobalConfiguration.Configuration.DependencyResolver=NewAutofacwebapidependencyresolver (container);
By constructing the injection, you can
/// <summary> ///Redis Service/// </summary> Public classRedisservice:iredisservice {Private Static ReadOnlyLogger Logger =Logmanager.getcurrentclasslogger (); /// <summary> ///Redis Service/// </summary> Private ReadOnlyConnectionmultiplexer _connectionmultiplexer; /// <summary> ///constructor Function/// </summary> /// <param name= "Connectionmultiplexer" >Redis Service</param> PublicRedisservice (connectionmultiplexer connectionmultiplexer) {_connectionmultiplexer=Connectionmultiplexer; } /// <summary> ///get values based on key/// </summary> /// <param name= "key" >Key</param> /// <returns></returns> Public AsyncTask<webapiresponse> Get (stringkey) { Try { vardb =_connectionmultiplexer.getdatabase (); /*var set = await db. Stringsetasync ("Name", "Irving"); var get = await db. Stringgetasync ("name"); */ return NewWebapiresponse {IsError=false, MSG=string. Empty, Data=awaitdb. Stringgetasync (Key)}; } Catch(Exception ex) {logger. Error (ex,"redisservice Get Exception:"+Ex. Message); return NewWebapiresponse {IsError=false, MSG=string. Empty, Data=string. Empty}; } } }
Cacheoutput and Redis
The default Cacheoutput uses System.Runtime.Caching.MemoryCache to cache the data, can be customized to extend to Db,memcached,redis, and so on, only need to implement Iapioutputcache interface
Public Interfaceiapioutputcache{T Get<T> (stringKeywhereT:class; ObjectGet (stringkey); voidRemove (stringkey); voidRemovestartswith (stringkey); BOOLContains (stringkey); voidADD (stringKeyObjectO, DateTimeOffset expiration,stringDependsonkey =NULL);}
Implementation services/// <summary> ///Implementing Redis Services/// </summary> Public classRediscacheprovider:iapioutputcache {/// <summary> ///Redis Service/// </summary> Private ReadOnlyConnectionmultiplexer _connectionmultiplexer; /// <summary> ///constructor Function/// </summary> /// <param name= "Connectionmultiplexer" >Redis Service</param> PublicRediscacheprovider (connectionmultiplexer connectionmultiplexer) {_connectionmultiplexer=Connectionmultiplexer; } Public voidADD (stringKeyObjectO, DateTimeOffset expiration,stringDependsonkey =NULL) { Throw Newnotimplementedexception (); } Publicienumerable<string>AllKeys {Get{Throw Newnotimplementedexception ();} } Public BOOLContains (stringkey) { Throw Newnotimplementedexception (); } Public ObjectGet (stringkey) { vardb =_connectionmultiplexer.getdatabase (); returndb. Stringget (key); } PublicT get<t> (stringKeywhereT:class { Throw Newnotimplementedexception (); } Public voidRemove (stringkey) { Throw Newnotimplementedexception (); } Public voidRemovestartswith (stringkey) { Throw Newnotimplementedexception (); } }
Register Webapiconfig
Configuration. Cacheoutputconfiguration (). Registercacheoutputprovider (() = Rediscacheprovider);
Or use the AUTOFAC for Web API
var New Containerbuilder (); builder. RegisterInstance (newnew autofacwebapidependencyresolver (builder). Build ());
Refer:
How do I use the caching in ASP Web API?
Http://stackoverflow.com/questions/14811772/how-to-use-caching-in-asp-net-web-api
Output caching in ASP Web API
http://www.strathweb.com/2012/05/output-caching-in-asp-net-web-api/
NuGet package of the Week:ASP.NET Web API Caching with Cachecow and Cacheoutput
Http://www.hanselman.com/blog/NuGetPackageOfTheWeekASPNETWebAPICachingWithCacheCowAndCacheOutput.aspx
Caching resources using Cachecow and ETag
Http://www.cnblogs.com/fzrain/p/3618887.html
Using Cached "Redis" in ASP. NET WebAPI