HI WELCOME TO SIRIS
Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Donut Caching and Donut Hole Caching with Asp.Net MVC 4

Donut caching is the best way to cache an entire web page except for one or more parts of the web page. Before Donut caching, we have Output Caching which cache the entire web page.

When to use Donut caching...

Suppose, you have a web application in which some pages like HomePage,Tools etc. are same for all the users excepts the user's logged in details like username.
If you want to cache all these pages for all the users by using OutputCache with VaryByParam UserID, then the entire page would be cached every time for each user with a different user name (or whatever your dynamic part of the page is). This is not a good practice since there will be 1000 cached pages if there are 1000 logged in user at a time.
To resolve this issue, Donut Caching was introduced which cached only one copy of the entire page for all the user except for a small part which remain dynamic. This small part act like as a hole in the cached content and much like a donut.
Donut caching is very useful in the scenarios where most of the elements in your page are rarely changed except the few sections that dynamically change, or changed based on a request parameter.

The MvcDonutCaching NuGet Package

For implementing Donut caching you need to install MvcDonutCaching NuGet package within your Visual Studio 2012. You can install this through NuGet or directly type the command "install-package MvcDonutCaching" in the "Package Manager Console" as shown below:

Once the MvcDonutCaching NuGet package is installed, you can add the DonutOutputCache attribute to the action methods or to the controller. Most of the options of OutputCache attribute are also available with Donut Caching.

  1. [DonutOutputCache(Duration=60)]
  2. public ActionResult Index()
  3. [DonutOutputCache(CacheProfile="TwoMins")]
  4. public ActionResult Index()
  5. [DonutOutputCache(Duration=60, VaryByCustom="whatever")]
  6. public ActionResult Index()
  7. [DonutOutputCache(Duration=60, VaryByParam="something;that")]
  8. public ActionResult Index(string something)
  9. [DonutOutputCache(Duration=60, VaryByParam="none")]
  10. public ActionResult Index(int referrerId)

Donut Hole Caching

Donut Hole Caching is the inverse of Donut caching means while caching the entire page it cached only a small part of the page(the donut hole).

When to use Donut Hole caching...

Suppose, you have a web application in which ProductCategory is shown on each and every pages so it makes sense to render all of the categories just once and cache the resulting HTML by using Donut Hole Caching.
Donut Hole caching is very useful in the scenarios where most of the elements in your page are dynamic except the few sections that rarely change, or changed based on a request parameter. Asp.Net MVC has great support for Donut Hole caching through the use of Child Actions.
  1. [ChildActionOnly]
  2. [OutputCache(Duration=60)]
  3. public ActionResult CategoriesList()
  4. {
  5. // Get categories list from the database and
  6. // pass it to the child view
  7. ViewBag.Categories = Model.GetCategories();
  8. return View();
  9. }

View with Donut Hole Caching

Now call the above action method "CategoriesList" from the parent view as shown below:
  1. <h1>MVC Donut Hole Caching Demo</h1>
  2.  
  3. <div>@Html.Action("CategoriesList")</div>
  4.  
  5. <!-- Rest of the page with non-cacheable content goes here -->
What do you think?
I hope you will enjoy the tips while programming with MVC Razor. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Custom Razor View Engine for C# and VB

You should be happy to know, Asp.Net MVC is an open source and highly extensible framework. You can customize it according to your need. As you read my previous article Removing the Web Form View Engine for better performance of Razor View Engine from your Asp.Net MVC Razor application. In this article, you will learn how can you customize the Razor View engine for C# and VB language.

Removing All the View Engines & registering the Razor Engine

  1. protected void Application_Start()
  2. {
  3. //Remove All View Engine including Webform and Razor
  4. ViewEngines.Engines.Clear();
  5. //Register Razor View Engine
  6. ViewEngines.Engines.Add(new RazorViewEngine());
  7. //Other code is removed for clarity
  8. }
After removing the Web Form and other View engine as you noticed that Razor View engine looks up the C# and VB views as shown below.


As you know MVC is highly extensible hence you can completely replace the Razor view engine with a new custom razor engine. by doing so, this will slightly improve your application performance. If your MVC application is using only C# language, there is no need to looks up the .vbhtml views and same for VB language.

Custom C# Razor View Engine

  1. public class CSharpRazorViewEngine : RazorViewEngine
  2. {
  3. public CSharpRazorViewEngine()
  4. {
  5. AreaViewLocationFormats = new[]
  6. {
  7. "~/Areas/{2}/Views/{1}/{0}.cshtml",
  8. "~/Areas/{2}/Views/Shared/{0}.cshtml"
  9. };
  10. AreaMasterLocationFormats = new[]
  11. {
  12. "~/Areas/{2}/Views/{1}/{0}.cshtml",
  13. "~/Areas/{2}/Views/Shared/{0}.cshtml"
  14. };
  15. AreaPartialViewLocationFormats = new[]
  16. {
  17. "~/Areas/{2}/Views/{1}/{0}.cshtml",
  18. "~/Areas/{2}/Views/Shared/{0}.cshtml"
  19. };
  20. ViewLocationFormats = new[]
  21. {
  22. "~/Views/{1}/{0}.cshtml",
  23. "~/Views/Shared/{0}.cshtml"
  24. };
  25. MasterLocationFormats = new[]
  26. {
  27. "~/Views/{1}/{0}.cshtml",
  28. "~/Views/Shared/{0}.cshtml"
  29. };
  30. PartialViewLocationFormats = new[]
  31. {
  32. "~/Views/{1}/{0}.cshtml",
  33. "~/Views/Shared/{0}.cshtml"
  34. };
  35. }
  36. }

Regisering the C# Razor View Engine

  1. protected void Application_Start()
  2. {
  3. //Remove All View Engine including Webform and Razor
  4. ViewEngines.Engines.Clear();
  5. //Register C# Razor View Engine
  6. ViewEngines.Engines.Add(new CSharpRazorViewEngine());
  7. //Other code is removed for clarity
  8. }

Custom VB Razor View Engine

  1. Imports System.Web.Mvc
  2. Public Class VBRazorViewEngine Inherits RazorViewEngine
  3. Public Sub New()
  4. AreaViewLocationFormats = {
  5. "~/Areas/{2}/Views/{1}/{0}.vbhtml",
  6. "~/Areas/{2}/Views/Shared/{0}.vbhtml"
  7. }
  8. AreaMasterLocationFormats = {
  9. "~/Areas/{2}/Views/{1}/{0}.vbhtml",
  10. "~/Areas/{2}/Views/Shared/{0}.vbhtml"
  11. }
  12. AreaPartialViewLocationFormats = {
  13. "~/Areas/{2}/Views/{1}/{0}.vbhtml",
  14. "~/Areas/{2}/Views/Shared/{0}.vbhtml"
  15. }
  16. ViewLocationFormats = {
  17. "~/Views/{1}/{0}.vbhtml",
  18. "~/Views/Shared/{0}.vbhtml"
  19. }
  20. MasterLocationFormats = {
  21. "~/Views/{1}/{0}.vbhtml",
  22. "~/Views/Shared/{0}.vbhtml"
  23. }
  24. PartialViewLocationFormats = {
  25. "~/Views/{1}/{0}.vbhtml",
  26. "~/Views/Shared/{0}.vbhtml"
  27. }
  28. End Sub
  29. End Class

Regisering the VB Razor View Engine

  1. Sub Application_Start()
  2.  
  3. ViewEngines.Engines.Clear();
  4. ViewEngines.Engines.Add(new VBRazorViewEngine());
  5.  
  6. End Sub

Note

  1. Use one of the approach when you are sure that you will use only either C# language or VB Language. It will be helpful to you.
  2. If you are using both type of views (cshtml and vbhtml), don't implement this.
What do you think?
I hope you will enjoy the tricks while programming with MVC Razor. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.