众所周知,OutPutCache不适用于Web API,因此我尝试寻找另一种解决方案。一个是:ASP.NET Web API CacheOutput,但我不知道为什么这个方法对我不起作用。(每次我得到Cache-Control:no-cache。从js脚本调用下的方法)
[CacheOutput(ClientTimeSpan = 60, ServerTimeSpan = 60)]
public virtual IEnumerable <UserDaysSummaryModel> Get(string startDate, string endDate)
{
...
}另一种解决方案:使用Redis Cache Service on Azure,但我没有找到任何使用Web的示例。
那么,什么以及如何为Web API使用缓存,更新缓存最简单的方法是什么?
发布于 2016-08-20 23:50:43
您可以使用以下代码在Web API中进行缓存
public class WebAPICachingHelper<T>
{
public static ObjectCache cache = MemoryCache.Default;
public static T GetCacheValue(string key)
{
if(cache[key] != null)
{
return (T)cache[key];
}
return default(T);
}
public static void SetCacheValue<T>(string key, T t)
{
cache[key] = t;
}
}https://stackoverflow.com/questions/25289087
复制相似问题