Preliminary looked under CacheCow and OutputCache , still feel CacheOutput more in line with their requirements, but also very simple to use
PM>Install-Package Strathweb.CacheOutput.WebApi2
Basics
CacheOutput properties
[Route("get")] [CacheOutput(ClientTimeSpan = 60, ServerTimeSpan = 60)] public IEnumerable<string> Get() { return new string[] { "Tom", "Irving" }; }
A parameter for the key
[Route("get")] [CacheOutput(ServerTimeSpan = 50, ExcludeQueryStringFromCacheKey = true)] public string Get(int id) { return DateTime.Now.ToString(); }
Etag head
Use Redis
The client uses StackExchange.Redis ,
Install-Package StackExchange.Redis.StrongName
Autofac registered in Redis connection
var Builder = new new ContainerBuilder (); // register achieve api container builder.RegisterApiControllers (Assembly.GetExecutingAssembly ()); // register achieve mvc container // builder.RegisterControllers (Assembly.GetExecutingAssembly ()); // in Autofac Redis connection registered and set as the Singleton builder.Register (R & lt => { return ConnectionMultiplexer.Connect (DBSetting.Redis); }). AsSelf () the SingleInstance ();. var Container = builder.Build (); GlobalConfiguration. Configuration.DependencyResolver = new new AutofacWebApiDependencyResolver(container);
By constructing the injection can be
/// <summary> ///Redis服务 /// </summary> public class RedisService : IRedisService { private static readonly Logger logger = LogManager.GetCurrentClassLogger(); /// <summary> ///Redis服务 /// </summary> private readonly ConnectionMultiplexer _connectionMultiplexer; /// <summary> /// 构造函数 /// </summary> /// <param name="connectionMultiplexer">Redis服务</param> public RedisService(ConnectionMultiplexer connectionMultiplexer) { _connectionMultiplexer = connectionMultiplexer; } /// <summary> /// 根据KEY获得值 /// </summary> /// <param name="key">key</param> /// <returns></returns> public async Task<WebAPIResponse> Get(string key) { try { var db = _connectionMultiplexer.GetDatabase(); /* var set = await db.StringSetAsync("name", "irving"); var get = await db.StringGetAsync("name"); */ return new WebAPIResponse { IsError = false, Msg = string.Empty, Data = await db.StringGetAsync(key) }; } catch (Exception ex) { logger.Error(ex, "RedisService Get Exception : " + ex.Message); return new WebAPIResponse { IsError = false, Msg = string.Empty, Data = string.Empty }; } } }
CacheOutput与Redis
Using default CacheOutput System.Runtime.Caching.MemoryCache to cache data may be custom extensions to the DB, Memcached, Redis like; only needs to implement the interface IApiOutputCache
public interface IApiOutputCache { T Get<T>(string key) where T : class; object Get(string key); void Remove(string key); void RemoveStartsWith(string key); bool Contains(string key); void Add(string key, object o, DateTimeOffset expiration, string dependsOnKey = null); }
Implement the service
/// <Summary> /// achieve Redis service /// </ Summary> public class RedisCacheProvider: IApiOutputCache { /// <Summary> /// Redis service /// </ Summary> Private Readonly ConnectionMultiplexer _connectionMultiplexer; // / <Summary> /// constructor /// </ Summary> /// <param name = "connectionMultiplexer"> the Redis service </ param> public RedisCacheProvider(ConnectionMultiplexer connectionMultiplexer) { _connectionMultiplexer = connectionMultiplexer; } public void Add(string key, object o, DateTimeOffset expiration, string dependsOnKey = null) { throw new NotImplementedException(); } public IEnumerable<string> AllKeys { get { throw new NotImplementedException(); } } public bool Contains(string key) { throw new NotImplementedException(); } public object Get(string key) { var db = _connectionMultiplexer.GetDatabase(); return db.StringGet(key); } public T Get<T>(string key) where T : class { throw new NotImplementedException(); } public void Remove(string key) { throw new NotImplementedException(); } public void RemoveStartsWith(string key) { throw new NotImplementedException(); } }
Registration WebAPIConfig
configuration.CacheOutputConfiguration().RegisterCacheOutputProvider(() => RedisCacheProvider);
Or use Autofac for Web API
var builder = new ContainerBuilder(); builder.RegisterInstance(new RedisCacheProvider()); config.DependencyResolver = new AutofacWebApiDependencyResolver(builder.Build());
REFER:
Lap around Azure Redis Cache
http://azure.microsoft.com/blog/2014/06/04/lap-around-azure-redis-cache-preview/
Caching data in Azure Redis Cache
https://msdn.microsoft.com/en-us/library/azure/dn690521.aspx
ASP.NET Output Cache Provider for Azure Redis Cache
https://msdn.microsoft.com/en-us/library/azure/dn798898.aspx
How to use caching in ASP.NET Web API?
http://stackoverflow.com/questions/14811772/how-to-use-caching-in-asp-net-web-api
Output caching in ASP.NET 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 use CacheCow and ETag cache resources http://www.cnblogs.com/fzrain/p/3618887.html ASP.NET WebApi - Redis AS CacheManager the Use HTTP: / /www.codeproject.com/Tips/825904/ASP-NET-WebApi-Use-Redis-as-CacheManager RedisReact https://github.com/ServiceStackApps/RedisReact .Net framework cache management CacheManager HTTP: //www.cnblogs. com / JustRun1983 / p / CacheManager.html
---------------------
Author: Irving
Source: CNBLOGS
Original: https: //www.cnblogs.com/Irving/p/4618556.html
Disclaimer: This article author original article, reproduced, please attach Bowen link!