Saturday, July 17, 2010

ASP .net PAGE CACHING and Substitution Control

Here we can have some basics about asp.net page caching. Think about a situations in which we are paging lakhs of data  in this case response each times goes to server and if the users are large in number that site goes to stuck definitely. It is worth to note that every post backed pages are in server memory and which are eating  server memory.  To over come this we use cache in asp.net pages.

Cache can be on same server or on another server. Cache have a duration and server will handle the post back only after the cache is expired. Till the page is in cache post back are not handled by the server.



Let us see the situation with and with out cache..
suppose that we  have a button and a label in an aspx page.

on each button click we will get the Time from server..  This means server is handling the post-back event or request.

protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = System.DateTime.Now.ToString();
}


Now suppose i am going to put that page in a cache..

for that in aspx source i add the following

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

So that that page will be in cache for 1 sec since Duration is 1000ms.(1000ms=1 sec)
so now you press the button frequently you can see that the time in the label is not changing for 1 sec since server is idle in this situation.

so the entire page is cached for 1 sec.







Now suppose we want some dynamic data to be displayed in a cached page. for that we use Substitution control

Drag that control and place it in the form.


public static string GetCurrentDateTime(HttpContext context)
    {
        return DateTime.Now.ToString();
    }

then for substitution control set the method name as the function name have HTTPCOntext  response.


            MethodName="GetCurrentDateTime" />



Now on button press u can see that Substitution control time chnages inside it while no change in label.

Thus we can display data in a chached page id data need to be dynamic..

No comments:

Post a Comment