| 0 comments ]

Introduction:
This tutorial illustrates how to implement page caching, here I am creating a simple page that shows system current date & time in a Label control, here we have used Directive along with Directive to cache entire Page.
When we cache a Page we also have to declare it's Lifetime because if we don't declare it's lifetime than every time we request to the server & we will get the same old cached Page Hence it's mandatory to declare lifetime of a Cached Page so that after particular time Server refreshes that Page. Hence In OutputCache Directive we will use Duration attribute, when you again request the same page after that duration than a new cached page is send to you by the server.
Step 1:
Open a new Website name it Caching and rename the Default.aspx file to PageCache.aspx
Step 2:
Drag & Drop Label control on the Page with properties [ID= LblMsg ]

Step 3:
Write this Line of code under Directive in PageCache.aspx File.

I have already discuss the use of Duration attribute but here we have a new attribute VaryByParam by declaring this we can cache multiple version of a page based on parameters passed in it. The possible parameters we can pass are none, "*", any query string. For more details check with msdn.
Step 4:
and type this code in PageCache.aspx.cs File.protected void Page_Load(object sender, EventArgs e) { LblMsg.Text = "Current Time :
"; LblMsg.Text = DateTime.Now.ToString(); }
In this code we are saving current System time in Label [LblMsg] Control.
When you debug this page you will see that the page shows the same time for 30 sec whether how many time you have refresh the page as soon as the time elapsed you will get the current system time. This is how we use to cache a page you may be thinking that 30 sec is very short duration of time may be but for Web Servers where there are thousands of request are processed in a minute. Hence generally caching duration is decided by Website Traffic.

0 comments

Post a Comment