【问题标题】:How to remove output cache for child action (MVC3)如何删除子操作的输出缓存(MVC3)
【发布时间】:2012-09-27 00:53:03
【问题描述】:

我试试这个代码

public ActionResult RemoveCache(Guid product_Id)  
{  
    var url = Url.Action("ProductPreview", "Common", new { product_Id = product_Id });  
    HttpResponse.RemoveOutputCacheItem(url);  
    return RedirectToAction("Index");  
}  

删除子操作的输出缓存,但它不起作用。请帮助我,谢谢!

【问题讨论】:

  • 看看这个答案stackoverflow.com/a/1169671/1342180 - 尤其是关于设置[OutputCache(Location = OutputCacheLocation.Server, ...)[ 的部分,当然是在缓存的操作方法上。
  • @AlexSchimp - 适用于甜甜圈孔缓存?
  • @AdamTuliper - 我可能误解了这个问题。我认为 OP 最初希望缓存该操作,并询问如何稍后清除它。
  • @Alex Schimp - 你没有误解这个问题。我的问题是这个。但它不适用于 Html.RenderAction 调用的子动作。我找到了stackoverflow.com/questions/8968508/…

标签: asp.net-mvc-3


【解决方案1】:

使用 MVCDonutCaching nuget 包:http://mvcdonutcaching.codeplex.com/

请参阅标题为“使用”的部分: http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3

用于从缓存中删除项目。 Html.Action 重载带有您添加的参数以强制甜甜圈洞(因此排除缓存)

@Html.Action("Login", "Account", true)

上面的 true 表示“不要缓存这个子动作 - 创建一个甜甜圈洞”

【讨论】:

  • 你能给我一个通过甜甜圈缓存来存储和删除缓存子动作的样本吗?非常感谢!
  • 我尝试将甜甜圈缓存用于子操作。 var cacheManager = new OutputCacheManager(); cacheManager.RemoveItem("Home", "List", new { page = 1 });删除缓存,但它没有被删除!请帮帮我!
  • 效果很好!非常感谢!但它不适用于一个区域。例如:cacheManager.RemoveItem("Home", "List", new { area = "Forum", page = 1 });不行!你能帮助我吗?非常感谢!
【解决方案2】:

如果接受清除所有 chid 操作的输出缓存,这是一种方法:

OutputCacheAttribute.ChildActionCache = new MemoryCache("NewRandomStringNameToClearTheCache"); 

NewRandomStringNameToClearTheCache 应该是一个随机字符串。

来源参考: http://dotnet.dzone.com/articles/programmatically-clearing-0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    • 2020-09-02
    • 2015-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多