Donut Caching
As I mentioned in the description, it improves performance of your web application. But as we know Output Cache also does the same thing. Both are the attributes used in the Controller-ActionResult methods. Now what they commonly do is they enable us to cache the content returned by a controller action.
Now what exactly does this mean!!, Lets take an example.
Suppose we have a basic Homepage to be displayed for all the users those who login only some part of the View will be dynamic for the different users, that can be rendered by creating Partial Views. But since the Homepage is common for all, we can use the attributes into the ActionResult method associated with the View.
- [OutputCache(Duration=60)]
- public ActionResult Index()
- {
- return View();
- }
Now if the OutputCache attribute is used for 1000 users logged in at a time 1000 cached Homepages will be framed. I guess that would cause problem and may block/slow down the browsing process. So to avoid this Donut Caching is introduced, that caches one copy of the common Homepage for all the users logged in at a time except a few dynamic parts of the page.
To implement Donut Caching in our web applications, we need to install it from the Godown of packages i.e. Package Manager Console by writing the following statement:
- PM> install-package MvcDonutCaching (hit enter and all the steps would be carried by the manager of the Godown).
- Installing 'MvcDonutCaching 1.2.3'.
- Successfully installed 'MvcDonutCaching 1.2.3'.
- Adding 'MvcDonutCaching 1.2.3' to Donut Caching.
- Successfully added 'MvcDonutCaching 1.2.3' to Donut Caching.
After this is successfully added, we can use the attribute in the controller.
- [DonutCaching(Duration=60)]
- public ActionResult ProductCategoryList()
- {
- ProductDb _db = new ProductDb();
- List<Product>products=db.product.ToList();
- return View(products);
- }
Now this will create one cache for the View page. We do not need to retrieve data from the database everytime we load the page within the Duration specified.
One thing to mention Donut Caching is the best way to cache an entire web page rather than caching one or more parts of the web page unless required.
A small thing but can be handy for beginners like me.
Thanks to Shailendra Chauhan.
Thank you.