MVC3的OutputCache RemoveOutputCacheItem的RenderAction [英] mvc3 OutputCache RemoveOutputCacheItem RenderAction

查看:230
本文介绍了MVC3的OutputCache RemoveOutputCacheItem的RenderAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做我的研究,但还没有找到任何答案。

I did my research but haven't found any answers.

我使用Html.RenderAction在母版(渲染特定于用户权限链接页头)。行动装饰着的OutputCache,返回部分控制,并如预期得到缓存。

I'm using Html.RenderAction in a masterpage ( to render page header with links specific to user permissions ). Action is decorated with OutputCache, returns partial control and gets cached as expected.

在该事件发生时(比如说权限更改)我想以编程方式缓存无效部分控制。

When the event happens ( let's say permissions are changed ) I want to programmatically invalidate cached partial control.

我试图使用RemoveOutputCacheItem方法。它作为一个参数的路径。我试图设置路径在Html.RenderAction使用的动作。这并不能否定的动作。

I'm trying to use RemoveOutputCacheItem method. It takes a path as a parameter. I'm trying to set the path to the action used in Html.RenderAction. That doesn't invalidate the action.

如何通过编程无效的动作?

How can I programmatically invalidate the action?

感谢

推荐答案

儿童行动缓存存储在<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.outputcacheattribute.childactioncache.aspx\">OutputCacheAttribute.ChildActionCache属性。问题是,该API生成儿童行动IDS并将其存储在这个对象是不公开的(为什么微软?)。所以,如果你在这个集合中的对象尽量循环,你会发现,它也将包含为您的孩子动作缓存值,但你不能,除非你逆向工程算法来识别它被用来生成密钥,看起来这样的事情(与反射所示):

The cache for child actions is stored in the OutputCacheAttribute.ChildActionCache property. The problem is that the API generating ids for child actions and storing them in this object is not public (WHY Microsoft??). So if you try to loop through the objects in this collection you will discover that it will also contain the cached value for your child action but you won't be able to identify it unless you reverse engineer the algorithm being used to generate keys which looks something like this (as seen with 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())));
    }
}

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

So you could perform the following madness:

public ActionResult Invalidate()
{
    OutputCacheAttribute.ChildActionCache = new MemoryCache("NewDefault");
    return View();
}

这显然会作废的所有缓存孩子这可能不是你在找什么行动,但恐怕是当然比其他的唯一途径反向工程密钥生成: - )

which obviously will invalidate all cached child actions which might not be what you are looking for but I am afraid is the only way other than of course reverse engineering the key generation :-).

@Microsoft,请,我求你了ASP.NET MVC 4.0:

@Microsoft, please, I am begging you for ASP.NET MVC 4.0:


  1. 引入的可能性做甜甜圈缓存除了甜甜圈洞缓存

  2. 引进的可能性,很容易过期缓存控制器动作的结果(更多的东西比MVCish Response.RemoveOutputCacheItem

  3. 引进的可能性,很容易过期缓存孩子行动的结果

  4. 如果你做一则显然介绍到期缓存环部的可能性。

这篇关于MVC3的OutputCache RemoveOutputCacheItem的RenderAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆