我已使用 Microsoft RedisOutputCacheProvider 成功实现了 Azure Redis 缓存来自 NuGet,它对一般页面按预期工作。
[ChildActionOnly]
[ChildActionOutputCache(CacheProfile.StaticQueryStringComponent)]
public ActionResult Show(int id)
{
// some code
}
但是,我似乎无法让它适用于 child 操作。在使用 Redis 缓存之前,它使用默认的 OutputCacheProvider 工作。
有人有什么想法吗,或者这只是一个限制?
提前致谢
最佳答案
在 Global.asax.cs
中,设置与 Redis 通信的自定义子操作输出缓存:
protected void Application_Start()
{
// Register Custom Memory Cache for Child Action Method Caching
OutputCacheAttribute.ChildActionCache = new CustomMemoryCache("My Cache");
}
此缓存应从 MemoryCache
派生并实现以下成员:
/// <summary>
/// A Custom MemoryCache Class.
/// </summary>
public class CustomMemoryCache : MemoryCache
{
public CustomMemoryCache(string name)
: base(name)
{
}
public override bool Add(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
{
// Do your custom caching here, in my example I'll use standard Http Caching
HttpContext.Current.Cache.Add(key, value, null, absoluteExpiration.DateTime,
System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
return true;
}
public override object Get(string key, string regionName = null)
{
// Do your custom caching here, in my example I'll use standard Http Caching
return HttpContext.Current.Cache.Get(key);
}
}
有关 my blog post 的更多信息
关于Azure Redis 缓存和 MVC 子操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28749476/