ASP.Net MVC Framework Donut & Donut Hole Caching

1. Donut Caching

Donut caching is a server-side caching technique in which the entire page is cached, except for small portions that remain dynamic.

OutputCache with VaryByParam serves most of the purpose for the web applications, but there are cases when it is necessary to cache an entire page while leaving dynamic sections not cached, like logged-in user name and local time display. In the ASP.NET MVC Framework the Razor View engine was the most successful framework, however even this does not have full support for this requirement. ASP.NET Web Forms offers the Substitution control to shape out the "holes" or dynamic sections out of the entire page.

<header>

   <h1>Donut Caching Example 1</h1>

     <div class="UserName">

       <asp:Substitution runat="server" MethodName="GetUserName" />

     </div>

</header>

The Substitution control registers a callback event within the ASP.NET output cache and invokes a static method on the page when the cached page is requested.

partial class DonutCachingPage : System.Web.UI.MasterPage

{

  public static string GetUserName(HttpContext Context)

  {

    return "Hello " + Context.User.Identity.Name;

  }

}

When the request comes for DonutCachingPage, the entire cached page is returned except for the username section, that continues to get generated for each request.

We can also develop the same logic with custom code that actually does what the Substitution control does. The HttpResponse class has the WriteSubstitution() method; it is what the Substitution control uses behind the scenes. Develop the following code with an extension method.
 

public delegate string CacheCallback(HttpContextBase context);
public static object Substitution(this HtmlHelper myHtml, CacheCallback cacheCallback)
{
   myH
tml.ViewContext.HttpContext.Response.WriteSubstitution(
     
c => HttpUtility.HtmlEncode(
          
cacheCallback (new HttpContextWrapper(c
))

      ));

   return null;

}
 

Use the following code that will produce the same output.

<header>

  <h1>MVC Donut Caching  example 2</h1>

    <div class="UserName">

       Hello @Html.Substitution(context => context.User.Identity.Name)

    </div>

</header>


2. Donut Hole Caching

Donut hole caching is the inverse of donut caching. While the donut caching technique caches the entire page, by leaving out only a few small sections, donut hole caching caches only one or a few portions of the page. Unlike donut caching, ASP.NET MVC has great support for donut hole caching through the use of the ChildActionOnlyAttribute attribute.
 

[ChildActionOnly]

[OutputCache(Duration=3600)]

public ActionResult ProductsChildAction()

{

    // Fetch Categories from the database and

    // pass it to the child view via its ViewBag

    ViewBag.Categories = Model.GetCategories();

    return View();

}

The OutputCacheAttribute attribute caches the Products for an hour, whereas the rest will be dynamic when you call the ProductsChildAction method in the HTML page.
 

<header>

  <h1>MVC Donut Hole Caching Example 1</h1>

</header>

<aside>

  <section id="products">

    @Html.Action("ProductsChildAction ")

  </section>

</aside>

The results of this call are cached via the OutputCacheAttribute attribute. When the page is rendered the next time, the Products list is rendered from the cache while the rest of the page is generated from scratch.

3. Distributed Caching


In modern web deployment, web applications are deployed to multiple web servers with a load balancer. With the Server-side Caching enabled, if one request is served by one server then data is cached on that server, now with load balance configuration it is not guaranteed that the same request goes to the cached server. If that request goes to another server then again the same page will be cached and that is redundant.
What if we could generate the Cache and share it with another server or even with the other farms? If we can do that then we can improve the performance of the web application.
The technique of caching data on one application instance and sharing it with other instances is known as distributed caching, and it is the most detailed of all caching techniques.

Distributed caching is an extension of caching techniques by which data from a database or session file system or another application is stored in a central location that all instances of an application have access to.

There are many advantages with the distributed caching, including
the following:

  1. Performance

    Large data can stored in the servers in-memory; that improves the read performance and the page load will faster.

  2. Scalability

    Allows the application to scale to higher demands and load easily.

  3. Redundancy

    Redundancy ensures that if one server fails then the entire application does not suffer. Instead, another failover server can pick up the request and serve it without any manual intervention. Failover and redundancy are essential features of many distributed caching solutions.

4. Distributed caching solutions

There are a number of distributed caching products available today, and although each of these products offer very different APIs and ways of working with the data they manage, the basic concepts of distributed caching remain the same. Microsoft provides a solution for distributed caching, the Velocity or Windows AppFabric.
  

Up Next
    Ebook Download
    View all
    Learn
    View all