Tôi đã thực hiện một số kiểm tra về bộ nhớ đệm. Đây là những gì tôi tìm thấy:
Bạn phải xóa bộ nhớ cache cho mọi tuyến đường dẫn đến hành động của bạn. Nếu bạn có 3 tuyến dẫn đến hành động tương tự chính xác trong bộ điều khiển của mình, bạn sẽ có một bộ nhớ cache cho mỗi tuyến đường.
Hãy nói rằng, tôi có cấu hình tuyến đường này:
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 }
);
Sau đó, những 3 tuyến đường dẫn đến myAction
trong myController
với param myParam
:
- http://example.com/c/myParam
- http://example.com/myController/myAction/myParam
- http://example.com/u/0/myController/myAction/myParam
Nếu hành động của tôi là như sau
public class SiteController : ControllerCommon
{
[OutputCache(Duration = 86400, VaryByParam = "id")]
public ActionResult Cabinet(string id)
{
return View();
}
}
tôi sẽ có một bộ nhớ cache cho mỗi tuyến đường (trong trường hợp này 3). Do đó, tôi sẽ phải làm mất hiệu lực mọi tuyến đường.
Như thế này
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));
}
bạn đã tìm cách khắc phục? – Stefanvds
Bản sao của: http://stackoverflow.com/questions/1200616/abort-outputcache-duration-programatically-in-asp-net-mvc và http://stackoverflow.com/questions/1167890/how-to-programmatically- clear-outputcache-for-controller-action-method –