Experts of .net Development Company bring this article to help entire asp.net community understanding Donut Output Caching and its use in asp.net mvc programming. You can read further and explore what they want to explain.
Output cache in MVC is very similar to the output cache in Asp.Net webforms. In output caching, the controller actions output are stored in the cache. These cached results are returned on subsequent calls on controller actions instead of the executing the actions again. Given that a set of pages in a web application contains data from a service or some database, it will be quicker to return the data from the cached result instead of calling the services. Few people think that output caching does not offer any advantage for a web application and is not the perfect. However, output caching is suitable for many sites and can benefit these sites. Two main points for output cache are:
1) A high traffic site can have a great impact by cache durations of few seconds. That is, the output caching can be used by real time applications.
2) The output caching can significantly boost the performance of the application even if caching is done heavily in the application. It is comparatively slower to retrieve data from cache and then render the page with this data rather than rendering the entire the page with output cache.
There are three different types of output caching:
1) Full Page Cache: In this type of caching, the entire page of the application is cached without any substitutions. There is an output cache attribute to handle full page caching.
2) Partial page Cache (Donut Hole Caching): In this type of caching, instead of caching the complete page, only some parts of the page are cached. There is a build in attribute to handle partial page caching. This type of caching is available from Asp.Net MVC 3.
3. Partial page Caching (Donut Caching): Donut caching is a type of output caching where the entire page is cached leaving some parts. Donut caching was available in Asp.Net MVC 1 with the help of substitution control. However, it is not possible in Asp.Net MVC 3 and above versions.
Donut caching is an important requirement and its omission in MVC 3 is unfortunate.
Working of Donut Caching:
Output caching is performed in Asp.Net MVC with the help of an action filter attribute that is applied to the controller actions individually or to the entire controller. Donut caching is implemented with a simple concept. The output of the page is cached and that page is returned on subsequent calls. However, few extra steps are required in this case. Before caching the page, it is required to find the areas that should not be cached. These areas are referred as donut holes. These holes should be marked such that while retrieving the cached page, these areas can be replaced with the non-cached version.
Problem while Implementing Donut Caching
There is a difficulty with the way of working of output caching. The implementation of Donut hole caching is entirely different from the full page caching. The full page caching uses the OutputCacheModule for implementing caching while a separate path of code is used in partial page caching to save the output using the MemoryCache object.
As far as donut caching is concerned, it is very difficult to hook into the HttpModule. So the OutputCache attribute must be completely rewritten so that some of the inconsistencies are fixed in the implementation.
Main limitations of Output caching are as follows:
1) Not able to disable caching in Web.config file:
In case of full page caching, it is possible to disable caching by changing a value in web.config file. Partial page caching cannot be disabled with this value.
2) Cache Profiles not supported:
Cache profiles are not supported for child actions in Asp.Net MVC 3. However, these are supported for the controller actions.
3) In partial page caching, removing the output cache items is not very easy, especially for the child actions.
4) It is problematic to use a store for custom output cache.
MVCDonutCaching Nuget Package
MVC donut package can be installed using Nuget Package Manager. To install the same, open tools, then Library Package Manager, then go to Package Manager Console. Type the following command in the console:
"Install package MvcDonutCaching"
Once the package is installed, DonutOutputCache attribute can be added to the controller or to the action. Many types of parameters of OutputCache attribute can be seen in Donut cache as below:
Add the profile to the web.config file in case a cache profile is used.
Now run the application. At this stage, the Donut output cache will run in the similar way as the Output cache and full page will be cached. To start the donut caching, it is required to specify the donut hole. It can be implemented by adding various overloads to Action Html helper. Also, add a true parameter to specify that it should not be cached with the current page. Now run the project and see the execution of donut caching. The donut hole is executed on each request while caching the parent page. It is also possible to add DonutCacheAttribute or OutputCacheAttribute on child action. With this, it is possible to cache one part of the page for five minutes and rest for say an hour.
An item can be removed from the output cache by using various methods of the output cache manager class.
As we have seen, the Donut hole Caching is inverse of the Donut caching. Only some sections of the page are cached in this case, instead of caching the entire page and then leaving few sections aside.
The purpose of the experts behind sharing this post is to let you know about the use of Donut output caching in asp.net mvc. You can share your reviews regarding this post with .NET development company experts.