众所周知,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 API 配合使用的示例。
那么什么以及如何使用 Web API 的缓存以及什么方法最容易更新缓存?
最佳答案
您可以使用以下代码在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;
}
}
关于.net - Web API 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25289087/