mvc3 OutputCache RemoveOutputCacheItem RenderAction

mb6*_*666 7 outputcache asp.net-mvc-3

我做了我的研究,但没有找到任何答案.

我在主页中使用Html.RenderAction(用于呈现具有特定于用户权限的链接的页眉).Action使用OutputCache修饰,返回部分控件并按预期缓存.

当事件发生时(假设权限已更改)我想以编程方式使缓存的部分控件无效.

我正在尝试使用RemoveOutputCacheItem方法.它将路径作为参数.我正在尝试设置Html.RenderAction中使用的操作的路径.这不会使行动失效.

如何以编程方式使操作无效?

谢谢

Dar*_*rov 9

子操作的缓存存储在OutputCacheAttribute.ChildActionCache属性中.问题是,为子操作生成ID并将其存储在此对象中的API不是公共的(为什么是Microsoft?).因此,如果您尝试遍历此集合中的对象,您将发现它还将包含子操作的缓存值,但除非您反向设计用于生成看起来的键的算法,否则您将无法识别它这样的事情(如Reflector所示):

internal string GetChildActionUniqueId(ActionExecutingContext filterContext)
{
    StringBuilder builder = new StringBuilder();
    builder.Append("_MvcChildActionCache_");
    builder.Append(filterContext.ActionDescriptor.UniqueId);
    builder.Append(DescriptorUtil.CreateUniqueId(new object[] { this.VaryByCustom }));
    if (!string.IsNullOrEmpty(this.VaryByCustom))
    {
        string varyByCustomString = filterContext.HttpContext.ApplicationInstance.GetVaryByCustomString(HttpContext.Current, this.VaryByCustom);
        builder.Append(varyByCustomString);
    }
    builder.Append(GetUniqueIdFromActionParameters(filterContext, SplitVaryByParam(this.VaryByParam)));
    using (SHA256 sha = SHA256.Create())
    {
        return Convert.ToBase64String(sha.ComputeHash(Encoding.UTF8.GetBytes(builder.ToString())));
    }
}
Run Code Online (Sandbox Code Playgroud)

所以你可以执行以下疯狂:

public ActionResult Invalidate()
{
    OutputCacheAttribute.ChildActionCache = new MemoryCache("NewDefault");
    return View();
}
Run Code Online (Sandbox Code Playgroud)

这显然会使所有缓存的子操作无效,这些操作可能不是您正在寻找的但我担心除了当然是对密钥生成进行逆向工程之外的唯一方法:-).

@Microsoft,拜托,我求你了解ASP.NET MVC 4.0:

  1. 除了甜甜圈洞缓存之外,还介绍了进行甜甜圈缓存的可能性
  2. 介绍了缓存控制器动作结果的可能性(更多MVCish比Response.RemoveOutputCacheItem)
  3. 介绍了轻松过期缓存子操作结果的可能性
  4. 如果你这样做1.那么显然有可能使缓存的甜甜圈部分到期.

  • @Darin - 你的祷告得到了回答:-) 1-4点都是开源的MvcDonutCaching NuGet包.http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3 (5认同)