【问题标题】:Azure Redis Cache and MVC Child ActionsAzure Redis 缓存和 MVC 子操作
【发布时间】:2015-04-29 05:52:15
【问题描述】:

我已经使用来自 NuGet 的 Microsoft RedisOutputCacheProvider 成功实现了 Azure Redis 缓存,它对一般页面的预期效果。

[ChildActionOnly]
[ChildActionOutputCache(CacheProfile.StaticQueryStringComponent)]
public ActionResult Show(int id)
{
    // some code
}

但是,我似乎无法让它对子操作起作用。在使用 Redis Cache 之前,它使用的是默认的 OutputCacheProvider。

有没有人有任何想法,或者这只是一个限制?

提前致谢

【问题讨论】:

    标签: azure redis child-actions azure-redis-cache redis-cache


    【解决方案1】:

    在您的 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

    【讨论】:

    • 这对我来说是一个很好的开始,但是 CustomMemoryCache 的 Add 方法中名为 key 的字符串与 CustomOutputCacheProvider 的 Add 方法中生成的字符串不同。例如,在 CustomMemoryCache 中,当调用 Add 方法时,密钥是随机生成的,在我的情况下,密钥是 DyTdvXwzRuwozPQ4TW4atFVQjGIIO1s850zOBRPKf8s=" 其中与在 CustomOutputProvider 的 Add 方法中一样,字符串密钥是使用 "a2/test/authenticatedonly"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多