Caching

caching is used to inprove the performance of our application.
normally caching is enabled on the pages which are requested a lot and for which some cached responce can be given. you can control how long the cached responce should be valid and after the time out the cache page should be removed.

there are three types of caching
page caching(also called output caching)
fragment cache
data cache

page caching

to enable page caching add the following tag in the html view of the page
<%@ OutputCache Duration="50" VaryByParam="None" %>

sometimes the same page will be asked with varying parameters say list of employees belonging to a particular department

for this use the vary by param attribute of the output cache directive

try giving deptno in varybyparam attribute and then when checking the page in browser attach ?deptno = 1 to the url and check
later change it to deptno = 2
you will get various versions of the same page depending on the deptno, each version valid for 50 seconds from the time of its creation.

Fragment caching

create a user control and enable output caching for that control. when this control is placed on a page contents of this control will be comming from cache where as the remaining page will be refreshed so here as only some part of the page is comming from cache it is called fragment cache.
for crating user control, in the solution explorer right click and add new item select web user control and click add.
drag a lable contorl on it and in the source view i.e. html view type the output cache directive

<%@ OutputCache Duration="50" VaryByParam="None" %>

in the load event of the control type the code
label1.Text =DateTime.Now.TimeOfDay.ToString();

now on any page this control is used this will be served from the cache for 50 secs.

Data Caching

Cache["Key"] = value;

cache["name"] = "Hello";
string st = (string)Cache["name"];

cache["age"] = 10;
int age = (int)Cache["age"];

you can attach the time of expiration and dependencies to data caching