ASP.NET MVC Extensible Donut Caching brings donut caching to ASP.NET MVC 3 and later. The code allows you to cache all of your page apart from one or more Html.Actions which can be executed every request. Perfect for user specific content.
The best way to add donut caching to your MVC project is to use the NuGet package. From within Visual Studio, select Tools | Library Package Manager and then choose either Package Manager Console or Manage NuGet Packages. Via the console, just type install-package MvcDonutCaching and hit return. From the GUI, just search for MvcDonutCaching and click the install button.
The package adds several overloads to the built-in Html.Action HTML helper. The extra parameter in each overload is named excludeFromParentCache. Set this to true for any action that should not be cached, or should have a different cache duration from the rest of the page.
@Html.Action("Login", "Account", true)
The package also include a DonutOutputCacheAttribute to be used in place of the built-in OutputCacheAttribute. This attribute is typically placed on every controller action that needs be be cached.
You can either specify a fixed duration:
[DonutOutputCache(Duration = "300")]
public ActionResult Index()
{
return View();
}
Or, use a cache profile:
[DonutOutputCache(CacheProfile = "FiveMins")]
public ActionResult Index()
{
return View();
}
If you are using cache profiles, be sure to configure the profiles in the web.config. Add the following within the system.web element:
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="FiveMins" duration="300" varyByParam="*" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>
You can also configure the output cache to use a custom provider:
<caching>
<outputCache defaultProvider="DistributedCacheProvider">
<providers>
<add name="DistributedCacheProvider" type="DevTrends.Example.DistributedCacheProvider" />
</providers>
</outputCache>
</caching>
Note, that a custom provider is not included with this project but you can write one fairly easily by subclassing System.Web.Caching.OutputCacheProvider. A number of implementations are also available on the web.
Sometimes you want to allow the output caching to be bypassed entirely. DonutCacheAttribute adds the ability to choose to do this dynamically, using a similar interface to the VaryByCustom functionality of the standard OutputCacheAttribute.
E.g., in Global.asax.cs,
public class Application : HttpApplication
{
protected void Application_Start()
{
this.SetSkipByCustomStringDelegate(SkipByCustomString); //extension method
}
private bool SkipByCustomString(HttpContextBase context, string custom)
{
if (custom == "SkipOnPreview")
{
if (context.Request.QueryString["cmspreview"] == "1")
return true;
}
return false;
}
}
then on your Action:
[DonutOutputCache(SkipByCustom = "SkipOnPreview")]
public ActionResult SomeAction(){ ... }
A comprehensive guide to MVC Extensible Donut Caching is now available on the DevTrends Blog.