Friday, March 30, 2007

ASP.NET Caching

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

'IN VB.NET
value = Cache("name")

- or -

//IN C#
value = Cache.Get("name")


That's all..
Happy Programming....
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

About Me
My Photo

Full Stack Developer,Blogger,Microsoft Mvp, Mentor, Life long learner, 10+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC,Web Forms,C#,AngularJs,Golang
Mvp profile
Subscribe to my blog

Enter your email address:

Follow us on facebook
Blog Archive
Total Pageviews