What are the different types of caching?

Caching is a technique to improves the access time when multiple users access a web site simultaneously, or a single user accesses a web site multiple times. It decreases server round trips for fetching data from database by persisting data in the memory.

ASP.NET supports three types of caching:

  1. Page Output Caching [Output caching]
  2. Page Fragment Caching [Output caching]
  3. Data Caching

Page Output Caching

Page Output Caching is used to fetch information or data at page level . For output caching, an OutputCache directive can be added at the top of the .aspx page , specifying the duration (in seconds) that the page should be cached.
< %@OutputCache Duration= "60" VaryByParam= "DepartmentId"% >
All the attributes that we specify in an OutputCache directive are used to populate an instance of the System.Web.HttpCachePolicy class. Moreover, It is good if the website is fairly static.

Page Fragment Caching

ASP.NET provides a mechanism for caching portions of pages, called page fragment caching . For example: user control. To cache a portion of a page, you must first encapsulate the portion of the page you want to cache into a user control . In the user control source file, add an OutputCache directive specifying the Duration and VaryByParam attributes. When that user control is loaded into a page at runtime , it is cached, and all subsequent pages that reference that same user control will retrieve it from the cache.

Data Caching

Data Caching is used to fetch the information of an application quickly based on the requirements. Cache object is just like application object which can be access anywhere in the application. The lifetime of the cache is equivalent to the lifetime of the application. Caching data can dramatically improve the performance of an application by reducing database contention and round-trips .