【问题标题】:HttpResponse.RemoveOutputCacheItem is not workingHttpResponse.RemoveOutputCacheItem 不起作用
【发布时间】:2012-02-16 14:49:59
【问题描述】:

我有一个缓存的 ActionResult。

[OutputCache(Duration = 3600, VaryByParam = "product_Id")]
public ActionResult ProductPreview(Guid product_Id)
{
     // just for testing the cache
     System.Threading.Thread.Sleep(4000);
     return PartialView("ProductPreview", _repository.CreateProductModel(product_Id));
}

好的部分是缓存正在工作。第一次加载后,显示结果,没有任何 4 秒延迟。

但是,当对该产品进行了一些更改时,我需要清除缓存。

我尝试这样做清除缓存:

public ActionResult RemoveCache()
{
    var url = Url.Action("ProductPreview", "Common");
    // also tried with parameter
    // var url = Url.Action("ProductPreview", "Common", new { @product_Id = "productId" });
    HttpResponse.RemoveOutputCacheItem(url);

    return RedirectToAction("Index");
}

我还尝试使用 ajax 和整页刷新调用 RemoveCache 方法,但它们都不起作用。

我能做什么?问题出在哪里?

【问题讨论】:

    标签: asp.net-mvc model-view-controller caching


    【解决方案1】:

    RemoveOutputCacheItem 仅适用于路由参数,不适用于查询字符串。所以你可以修改你的路由定义:

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{product_Id}",
        new { controller = "Home", action = "Index" }
    );
    

    现在你可以使用 RemoveOutputCacheItem 方法了:

    public ActionResult RemoveCache(Guid product_Id)
    {
        var url = Url.Action("ProductPreview", "Common", new { product_Id = product_Id });
        // the url must look like this: /Common/ProductPreview/eeb2fe32-db58-4fc3-87c8-b47480fbe094
        // for the RemoveOutputCacheItem method to work
        HttpResponse.RemoveOutputCacheItem(url);
        return RedirectToAction("Index");
    }
    

    更新:

    这是我的测试用例:

    控制器:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        [OutputCache(Duration = 3600, VaryByParam = "product_id")]
        public ActionResult ProductPreview(Guid product_id)
        {
            var model = string.Format(
                "{0} - {1}", 
                product_id, 
                DateTime.Now.ToLongTimeString()
            )
            return PartialView("_Foo", model);
        }
    
        public ActionResult RemoveCache(Guid product_id)
        {
            var url = Url.Action(
                "ProductPreview", 
                "Home", 
                new { product_id = product_id }
            );
            HttpResponse.RemoveOutputCacheItem(url);
            return RedirectToAction("Index");
        }
    }
    

    查看(~/Views/Home/Index.cshtml):

    @{
        var productId = Guid.NewGuid();    
    }
    
    @Html.ActionLink("product 1", "ProductPreview", new { product_id = Guid.NewGuid() })
    <br/>
    @Html.ActionLink("product 2", "ProductPreview", new { product_id = productId })
    <br/>
    @Html.ActionLink("product 3", "ProductPreview", new { product_id = Guid.NewGuid() })
    <br />
    
    @Html.ActionLink(
        "clear cache for the second product", 
        "RemoveCache", 
        new { product_id = productId }
    )
    

    部分视图(~/Views/Home/_Foo.cshtml):

    @model string
    @Model
    

    在global.asax:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            "Default",
            "{controller}/{action}/{product_id}",
            new { controller = "Home", action = "Index", product_id = UrlParameter.Optional }
        );
    }
    

    更新 2:

    现在您已经显示了您的代码,您似乎正在使用Html.RenderAction 帮助程序,而ProductPreview 是一个子操作。子操作与普通视图不存储在同一个缓存中,HttpResponse.RemoveOutputCacheItem 帮助器根本不适用于缓存的子操作。如果您仔细查看我之前的示例,您会发现我使用标准链接来执行 ProductPreview 操作。

    目前您尝试实现的目标在 ASP.NET MVC 3 中是不可能实现的。如果您想使用甜甜圈输出缓存,我建议您使用 following article。希望此功能将添加到 ASP.NET MVC 4 中。

    【讨论】:

    • 我尝试在 Global.asax 文件中设置路由,例如“{controller}/{action}/{product_Id}”,但仍然无法正常工作。我也尝试了 VaryByCustom="product_Id" 并且也没有工作。与 ParialView 的事实有关吗?
    • @RaraituL,不,我认为这与它是局部视图这一事实无关。我已经更新了我的答案,以提供我的完整测试用例。
    • 我用相同的场景创建了一个测试项目,它不工作,我不明白为什么。我什至从 ActionResult 中删除了参数。是 web.config 设置吗?我需要将缓存存储在服务器上,还是更改一些默认设置?我会发布一些图片
    • @RaraituL,啊啊啊啊啊,既然你已经展示了你的代码,就很清楚为什么它不起作用了。您正在使用 Html.RenderAction 这意味着 ProductPreview 是一个子操作。 HttpResponse.RemoveOutputCacheItem 方法根本不适用于子操作。我会更新我的帖子。
    • 最后,我也一直在寻找互联网,发现“ChildActions”缓存问题。现在我将稍微玩一下 Donut Caching。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多