Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Donut Caching and Donut Hole Caching to cache pages in ASP.NET MVC applications

Shulou Source: shulou.com Published: 2022-06-02 00:19:35 11月15日 Update

This article introduces how to use Donut Caching and Donut Hole Caching to cache pages in ASP.NET MVC applications, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Donut Caching is the best way to cache the entire page except part of the content, and before it appeared, we used the "output cache" to cache the entire page.

When to use Donut Caching

Suppose you have an application that has a page like "home page", which always presents the same or little change to the user except the user name to which the user is logged in. At this point you may need to cache most of the content. Add that if you use the "output cache" and apply a VaryByParam UserID for caching, the entire page will generate a cache for each visiting user, but this is not a good way to cache when we have a large number of logged-in users.

To solve this problem, Donut Caching proposes to cache only one copy for all users and contains a small portion of dynamic content, which is like a hole in a doughnut.

Donut Caching's Nuget package

Before using Donut Caching, you need to use the Nuget installation package in Visual studio. Generally, type the command directly to install it:

Install-package MvcDonutCaching

Once installed, you can control the cache by adding DonutOutputCache tags to action or controller, and most OutputCache tags can be used in DonutOutputCache.

Public class HomeController: Controller

{

[DonutOutputCache (Duration = 60 VaryByParam = "username")]

Public ActionResult Index (string username)

{

Return View ()

}

[DonutOutputCache (Duration=60)]

Public ActionResult About ()

{

ViewBag.Message = "Your application description page."

Return View ()

}

Public ActionResult Contact ()

{

ViewBag.Message = "Your contact page."

Return View ()

}

}

Donut Hole Caching

Donut Hole Caching, in contrast to Donut Caching, is used to cache a small portion of a page.

When to use Donut Hole Caching

Suppose you have an application that needs to display a product list on each page, so it is necessary to cache a product list in the form of HTML. Donut Hole Caching is designed to handle this situation. ASP.NET MVC can support sub-requests by setting the ChildActionOnly tag.

[ChildActionOnly]

[DonutOutputCache (Duration=60)]

Public ActionResult CategoryList ()

{

Return View (categoryService.GetCategories)

}

So much for sharing about how to use Donut Caching and Donut Hole Caching to cache pages in ASP.NET MVC applications. I hope the above content can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Tags: Cache pages content users applications parts tags products product lists dynamic can be passed that is applications methods more programs processing help output good Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Linux Shulou Technology macOS MySQL