Donut hole caching - exclude MiniProfiler.RenderIncludes
Asked Answered
M

1

6

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?

Mei answered 15/12, 2012 at 14:23 Comment(2)
Does it matter? If page comes from output cache then the action and controller not called so profiler would not show anything anyway, no?Necrophilism
That's a really good point for the server-side, but MiniProfiler also outputs information about the load-time in the DOM etc that would be interesting.Mei
S
6

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.

Samellasameness answered 28/5, 2013 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.