Azure Redis 缓存和 MVC 子操作

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

我已使用 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/

相关文章:

windows - 在 Windows 上启动 Redis 服务器。 window 自己关闭

asp.net-mvc - 处理 MVC 子操作中的错误

python - Microsoft azure devops python 管道失败,Bash 退出,代码为 '5'

Redis – 无法打开 .rdb 以保存 : Permission denied

Azure云服务磁盘空间不足

java - 如何配置 Spring session 以在 xml 中与 Redis 一起工作?

asp.net-mvc - 如何在 ASP.NET MVC 中将数据从子操作传递到布局

asp.net-mvc-3 - MVC3 为什么我会收到 "Child actions are not allowed to perform redirect actions"错误?

c# - 用于运行可执行文件并将输出文件从 Azure Batch 复制到 blob 的自定义 .NET 事件

ruby-on-rails - TinyTds 无法连接到 Debian 上的 azure