Caching is generally used to catch frequently accessed data.when u used catching at that time u have to depend upon certain variables like parameter,time etc.. But one demerit of using catching is if ur using dynamic page at that time u will unable to get the updated value.
Types of catching:
1.Data caching (Programmatic)
2.Output caching
3.Fragment caching (Partial Page Caching)
Data Caching
The following code is an implementation of On-Demand caching. The method GetUserInfo checks to see if the data exists in the cache. If it is present, it is returned from the cache, else, the GetDS method fills the DataSet from the database and then populates the Cache.
public DataSet GetUserInfo()
{
string cacheKey = "UserInfo";
DataSet ds = Cache[cacheKey] as DataSet;
if (ds == null)
{
ds = GetDS();
Cache.Insert(cacheKey, ds, null, NoAbsoluteExpiration,
TimeSpan.FromHours(15),CacheItemPriority.High, null);
OR Cache[“ds”] = ds;
}
return ds;
}
Page output caching can be implemented in either of the following two ways:
At design time using the OutputCache directive
At runtime using the HttpPolicy class
The following is the complete syntax of page output caching directive
<%@ OutputCache Duration="no of seconds"
Location="Any | Client | Downstream | Server | None"
VaryByControl="control"
VaryByCustom="browser |customstring"
VaryByHeader="headers"
VaryByParam="parameter" %>
The following statement is used to implement output caching in an aspx page at design time. The directive is placed at the top of the .aspx page.
<%@OutputCache Duration="30" VaryByParam="none" %>
Page Fragment Caching
This allows specific portions of the page to be cached rather than caching the whole page. This is useful in situations where we can have a page that contains both static and dynamic content. The following code depicts how this can be accomplished.
<%@ OutputCache Duration="15"VaryByControl="EmpID;DeptID" VaryByParam="*"%>
This directive is placed at the top of any User Control (.axcx file).