Donut hole caching - exclude MiniProfiler.RenderIncludes

百般思念 提交于 2019-12-23 09:39:09

问题


I have an ASP.NET MVC action that is decorated with the OutputCache attribute, but the problem is that the MiniProfiler output is cached as well. I'd like to exclude the MiniProfiler output from the caching (donut hole), but I'm not sure how I can exclude a call like MiniProfiler.RenderIncludes().

Anyone who happen to know how I can do this?


回答1:


This is an important point if using MiniProfiler in production. As if the first visit to a page is by a user where MiniProfiler is enabled, all subsequent requests will include the MiniProfiler results in the DOM (as they are now cached). Not only will the results be incorrect (as they only consider first load), but all visitors will be able to see your MiniProfiler results.

Firstly, to enable donut hole caching, I'm making use of:

http://mvcdonutcaching.codeplex.com/

This allows you to add actions which will not be cached when using the OutputCache.

Given the above, you can remove @using StackExchange.Profiling; from your Layout page. You can then replace:

@MiniProfiler.RenderIncludes()

With:

@Html.Action("MiniProfiler", "DoNotCache", excludeFromParentCache: true)

I have created a DoNotCache controller, so all my non-cacheable elements are together, but this is not required and you can place this action in any controller.

 public ActionResult MiniProfiler()
 {
      return View();
 }

And then the view itself just looks like:

@using StackExchange.Profiling;
@{
    Layout = null;
}
@MiniProfiler.RenderIncludes()

This will ensure the MiniProfiler results are displayed when appropriate, and not cached in production even in places where you use the DonutOutputCache annotation.



来源:https://stackoverflow.com/questions/13893028/donut-hole-caching-exclude-miniprofiler-renderincludes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!