加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – HttpResponse.RemoveOutputCacheItem不工作

发布时间:2020-12-15 19:45:30 所属栏目:asp.Net 来源:网络整理
导读:我有一个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",
我有一个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方法,而不是它们正在工作.

我能做什么?哪里有问题?

解决方法

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",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",new { product_id = productId })
<br/>
@Html.ActionLink("product 3",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

并在全球.asax:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default","{controller}/{action}/{product_id}",action = "Index",product_id = UrlParameter.Optional }
    );
}

更新2:

现在你已经显示了你的代码,好像你正在使用Html.RenderAction帮助器,而ProductPreview是一个子操作.子动作与普通视图不存储在相同的缓存中,HttpResponse.RemoveOutputCacheItem帮助器根本不起作用,缓存子操作.如果您仔细阅读我之前的示例,您将看到我使用标准链接进行ProductPreview操作.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读