Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Donut hole caching - exclude MiniProfiler.RenderIncludes

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?

like image 214
larsw Avatar asked Dec 15 '12 14:12

larsw


1 Answers

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.

like image 132
dazbradbury Avatar answered Nov 01 '22 00:11

dazbradbury