Asp.net mvc 如何";“作废”;ASP.NET MVC输出缓存的部分?

Asp.net mvc 如何";“作废”;ASP.NET MVC输出缓存的部分?,asp.net-mvc,caching,Asp.net Mvc,Caching,有没有办法以编程方式使ASP.NET MVC输出缓存的某些部分无效?我希望能够做到的是,如果用户发布的数据更改了缓存操作将返回的内容,则能够使缓存数据无效 这可能吗?一种方法是使用以下方法: HttpResponse.RemoveOutputCacheItem("/Home/About"); 这里描述了另一种方法: 我认为您可以通过为所需的每个操作使用一个方法级属性来实现第二个方法,只需向其中添加表示键的字符串。如果我理解你的问题 编辑:是的,asp.net mvc OutputCache只是

有没有办法以编程方式使ASP.NET MVC输出缓存的某些部分无效?我希望能够做到的是,如果用户发布的数据更改了缓存操作将返回的内容,则能够使缓存数据无效


这可能吗?

一种方法是使用以下方法:

HttpResponse.RemoveOutputCacheItem("/Home/About");
这里描述了另一种方法:

我认为您可以通过为所需的每个操作使用一个方法级属性来实现第二个方法,只需向其中添加表示键的字符串。如果我理解你的问题

编辑:是的,asp.net mvc OutputCache只是一个包装器

如果您使用的是
varyByParam=“none”
,那么您只需使
“/Statistics”
无效,也就是说
/
是查询字符串值。这将使页面的所有版本无效


我做了一个快速测试,如果您添加
varyByParam=“id1”
,然后创建页面的多个版本-如果您说invalidate invalidate
“/Statistics/id1”
,它只会使该版本无效。但是您应该做进一步的测试。

我做了一些缓存测试。这就是我发现的:

您必须清除导致您的操作的每个路由的缓存。 如果在控制器中有3条路由导致完全相同的操作,则每条路由将有一个缓存

比如说,我有一个路由配置:

routes.MapRoute(
                name: "config1",
                url: "c/{id}",
                defaults: new { controller = "myController", action = "myAction", id = UrlParameter.Optional }
                );

            routes.MapRoute(
                name: "Defaultuser",
                url: "u/{user}/{controller}/{action}/{id}",
                defaults: new { controller = "Accueil", action = "Index", user = 0, id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Accueil", action = "Index", id = UrlParameter.Optional }
            );
然后,这3条路径通过param
myParam
myController
中引导到
myAction

  • 如果我的行动如下

    public class SiteController : ControllerCommon
        {
    
            [OutputCache(Duration = 86400, VaryByParam = "id")]
            public ActionResult Cabinet(string id)
            {
                 return View();
    }
    }
    
    每个路由都有一个缓存(在本例中为3)。因此,我将不得不宣布每条路线无效

    像这样

    private void InvalidateCache(string id)
            {
                var urlToRemove = Url.Action("myAction", "myController", new { id});
                //this will always clear the cache as the route config will create the path
                Response.RemoveOutputCacheItem(urlToRemove);
                Response.RemoveOutputCacheItem(string.Format("/myController/myAction/{0}", id));
                Response.RemoveOutputCacheItem(string.Format("/u/0/myController/myAction/{0}", id));
            }
    

    MVC OutputCache属性只是普通ASP.NET输出缓存的包装器吗?那么,假设我想使名为“/Statistics/”的操作的结果无效,我只需调用HttpResponse.RemoveOutputCacheItem(“/Statistics/”)?FWIW,属性的“VaryByParams”属性是“None”。我是否正确地使用了该属性?@Matthew Belk:你最终使用了这种技术吗?参数对缓存项的无效化是否按预期工作?谢谢。我建议您使用MVCDonutCache,此处提供的更多信息ASP联盟的链接已断开。重复:和