Caching is process of saving html and data in browser cache. Some data are difficult to obatin it assumes lot of resouceof the server. The best way to store it in cache. So we can retrive from cache instead doing round trip to the server.There are also some portion of the site does not change frequently so caching is a good idea to store this portion ofwebsite in cahce rather then loadind it from server.
ASP.NET also provide caching features.ASP.NET Caching API provides great features for caching.ASP.NET provide three types of caching.
1) Page Output Caching
2) Page Fragment Caching
3) Data Caching
1) Page Output Caching:
In ASP.NET you can do page output caching with output directive....
Here is the code for output caching
<%@ OutputCache Duration="120" VaryByParam="none"%>
This means that page will cache for 120 second and during that time page will not change with get or post parameters.After 120 second page cache will be flush and then page content can be change by get or post parameters.
2) Page Fragment Caching:
We all know that it is impratical to cache whole page of website. So it is better cache a portion of a website thenwhole page. ASP.NET caching API also provide that feature.
From Following code asp.net can cache categoryid parameter of get and post method.
<%@ OutputCache Duration="120" VaryByParam="CategoryID"%>
You can also cache whole control in asp.net which can be used as portion of parameter.
<%@ OutputCache Duration="120" VaryByParam="none" VaryByControl="ViewCategory" %>
This will cache whole user control called viewcategory.
3) Data Caching:
For that ASP.NET Caching API provides two classes in System.Web.Cache name space.The first class, Cache, is the classwill be using to add and remove items from the data cache. The second class, CacheDependency,is used when assigning a cache dependency to an item in the data cache.
To add an item to the cache you can simply do:
' In VB.NET
Cache("name") = value
// In C#
Cache["name"] = value;
to get value
value = Cache("name")
- or -
value = Cache.Get("name")