Caching is the way of improving performance of web Application.
Note : Please replace ! with >
and * with >
before running the below code
In .net we can cache whole page (i.e. Output caching), part of page (i.e. fragment caching) and data cache.
1. whole page (i.e. Output caching):
!%@ OutputCache Duration="20" VaryByParam="none" %*
Description: Single copy of Page will be cached for 20 seconds.
Note: doesn’t support for the dynamic pages that vary their output based on the query string.
Since page will be cached for the upcoming 20 seconds,and output page will remain same for 20 seconds regardless the varying url.
e.g
www.dummyUrl.com?Studentid=1
if we apply caching on the above page for 20 seconds than
on changing ?Studentid=2 will return previous page only
www.dummyUrl.com?Studentid=2 goes to www.dummyUrl.com?Studentid=1
Caching with Specific Query String Parameters:
!%@ OutputCache Duration="20" VaryByParam="*" %*
It will cache all the query string based pages
Note: in this case unwanted query string based pages will be cached
!%@OutputCacheDuration="20 VaryByParam="Projectid;ProjectName” %*
It will cache only those pages which are having Projectid and ProjectName in Query string.
2. part of page (i.e. fragment caching):
In this case, you identify just the content you want to cache, wrap that in a dedicated User Control and cache just the output from that control.
Step 1. Create a User Control say “FragmentCtrl1.ascx”.
Source view:
!%@ Control Language="C#" AutoEventWireup="true" CodeFile="FragmentCtrl1.ascx.cs" Inherits="FragmentCtrl1" %*
!%@ OutputCache Duration="20" VaryByParam="none" VaryByControl="MyRadioButtonList"%*
!asp:Label ID="CacheEntryTime" runat="server" Font-Bold="True"*!/asp:Label*
!asp:RadioButtonList ID="MyRadioButtonList" runat="server"*
!asp:ListItem Selected="True"*Yes!/asp:ListItem*
!asp:ListItem*No!/asp:ListItem*
!asp:ListItem*Maybe!/asp:ListItem*
!/asp:RadioButtonList*
!asp:Button ID="Button1" runat="server" Text="Submit" /*
FragmentCtrl1.ascx.cs File:
protected void Page_Load(object sender, EventArgs e)
{
CacheEntryTime.Text = "FragmentCtrl1: " + DateTime.Now.TimeOfDay.ToString();
}
Step 2. Create a User Control say “FragmentCaching.aspx”.
Source view:
!%@ Page Language="C#" AutoEventWireup="true" CodeFile="FragmentCaching.aspx.cs" Inherits="FragmentCaching" %*
!%@ Register TagPrefix="uc1" TagName="FragmentCtrl1" Src="FragmentCtrl1.ascx" %*
!!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"*
!html xmlns="http://www.w3.org/1999/xhtml"*
!head runat="server"*
!title*!/title*
!/head*
!body*
!form id="form1" runat="server"*
!div*
!p*
!asp:Label ID="Time" runat="server" Font-Bold="True"*!/asp:Label*
!/p*
!p*
!uc1:FragmentCtrl1 id="FragmentCtrl1" runat="server"*
!/uc1:FragmentCtrl1*
!/p*
!/div*
!/form*
!/body*
!/html*
FragmentCaching.asppx.cs File:
protected void Page_Load(object sender, EventArgs e)
{
Time.Text = "WebFormTime: " + DateTime.Now.TimeOfDay.ToString();
}
No comments:
Post a Comment