Using Fragment Caching

only for RuBoard

So far, you have seen how to enable caching for entire pages. You might be thinking, "This is all well and good, but I make money off advertising. I can't have my ad banners cached because they are rotating banners and need to change on every request." Perhaps your site serves some dynamic content that must be refreshed on every request ”for instance an up-to-the-minute news flash.

You are in luck because, in this section, we will be discussing fragment caching, the technique of caching the output of portions of pages, rather than complete documents. It might sound complicated, but it is very easy.

You can take advantage of fragment caching by including one or more cache-enabled user controls within your web form. For example, you might have a standard Web Form with three user controls included. You can set the first user control cache for 1 minute, the second for 15 minutes, and the third for 30 minutes. If you don't have caching enabled anywhere else on the page (other user controls or the web form) then all content will be dynamically created on each request except for the three user controls whose cached version will be used.

Enabling caching within a user control is done the same way as in a Web Form, so I will not explain the semantics of enabling caching in this section. As previously mentioned, there are some attributes of the @OutputCache directive that are not supported by user controls. On the flip side, there is a new attribute added to the directive specifically for user controls. Table 16.1 lists supported and non-supported attributes for the @OutputCache directive when it is used in a user control.

Table 16.1. Supported and Non-Supported Attributes of the @OutputCache Directive When Used in a User Control
Attribute Supported Description
Duration Yes ”Required Length in seconds that the item should remain in the cache.
Location No N/A
VaryByCustom Yes Varies the document cache by a custom string. Default value "Browser".
VaryByHeader No N/A
VaryByParam Yes ”Required Varies the document cache by parameters. * Varies for all parameters; none of them disables the VaryByParam attribute.
VaryByControl Yes ”Not supported by Web Forms Varies the user control cache by property names . An example follows .

Listings 16.11, 16.12 and 16.13 contain sample code for two user controls and a Web Form to demonstrate fragment caching.

Listing 16.11 User Control That Displays a DataGrid and Cache Time ”Duration is set to 60
 [VisualBasic.NET] 01: <%@ OutputCache Duration="60" VaryByParam="none" %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.Data.SqlClient" %> 04: 05: <script language="vb" runat="server"> 06:  public sub Page_Load(sender as Object, e as EventArgs) 07:   dim sCon as new SqlConnection("server=localhost;" & _ 08:    "uid=sa;pwd=;database=northwind") 09:   dim SqlCmd as String = "SELECT TOP 10 * FROM Products" 10:   dim sda as new SqlDataAdapter(SqlCmd,sCon) 11:   dim ds as new DataSet() 12:   sda.Fill(ds,"Products") 13: 14:   dgUC1.DataSource = ds.Tables(0).DefaultView 15:   dgUC1.DataBind() 16: 17:  end sub 18: </script> 19: <b>This user control was cached at:</b><%=DateTime.Now.ToString("G")%> 20: <p> 21: <asp:DataGrid runat="server" id="dgUC1" font-size="10" /> [C#.NET] 01: <%@ OutputCache Duration="60" VaryByParam="none"  %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.Data.SqlClient" %> 04: 05: <script language="c#" runat="server"> 06:  void Page_Load(Object sender, EventArgs e) { 07:   SqlConnection sCon = new SqlConnection("server=localhost;" + 08:    "uid=sa;pwd=;database=northwind"); 09:   string SqlCmd = "SELECT TOP 10 * FROM Products"; 10:   SqlDataAdapter sda = new SqlDataAdapter(SqlCmd,sCon); 11:   DataSet ds = new DataSet(); 12:   sda.Fill(ds,"Products"); 13: 14:   dgUC1.DataSource = ds.Tables[0].DefaultView; 15:   dgUC1.DataBind() ; 16: 17:  } 18: </script> 19: <b>This user control was cached at:</b><%=DateTime.Now.ToString("G")%> 20: <p> 21: <asp:DataGrid runat="server" id="dgUC1" font-size="10" /> 

Listing 16.11 renders a DataGrid control with the top 10 rows from the Products table and the time that the user control is cached ”in this example, 60 seconds. In Listing 16.12, you add a new attribute, VaryByControl . Its value is one of this user control's public properties.

Listing 16.12 User Control That Displays a DataGrid and Cache Time ”Duration is set to 30
 [VisualBasic.NET] 01: <%@ OutputCache  Duration="30" VaryByParam="none" VaryByControl="PageSize" %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.Data.SqlClient" %> 04: 05: <script language="vb" runat="server"> 06: 07:  public PageSize as integer 08: 09:  public sub Page_Load(sender as Object, e as EventArgs) 10:   dim sCon as new SqlConnection("server=localhost;" & _ 11:    "uid=sa;pwd=;database=northwind") 12:   dim SqlCmd as String = "SELECT TOP " & PageSize & " * FROM Suppliers" 13:   dim sda as new SqlDataAdapter(SqlCmd,sCon) 14:   dim ds as new DataSet() 15:   sda.Fill(ds,"Products") 16: 17:   dgUC1.DataSource = ds.Tables(0).DefaultView 18:   dgUC1.DataBind() 19: 20:  end sub 21: </script> 22: <b>This user control was cached at:</b><%=DateTime.Now.ToString("G")%> 23: <p> 24: <asp:DataGrid runat="server" id="dgUC1" font-size="10" /> [C#.NET] 01: <%@ OutputCache  Duration="30" VaryByParam="none" VaryByControl="PageSize" %> 02: <%@ Import Namespace="System.Data" %> 03: <%@ Import Namespace="System.Data.SqlClient" %> 04: 05: <script language="c#" runat="server"> 06: 07:  public int PageSize; 08: 09:  void Page_Load(Object sender, EventArgs e) { 10:   SqlConnection sCon = new SqlConnection("server=localhost;" + 11:    "uid=sa;pwd=;database=northwind"); 12:   string SqlCmd = "SELECT TOP " + PageSize + " * FROM Suppliers"; 13:   SqlDataAdapter sda = new SqlDataAdapter(SqlCmd,sCon); 14:   DataSet ds = new DataSet(); 15:   sda.Fill(ds,"Products"); 16: 17:   dgUC1.DataSource = ds.Tables[0].DefaultView; 18:   dgUC1.DataBind() ; 19: 20:  } 21: </script> 22: <b>This user control was cached at:</b><%=DateTime.Now.ToString("G")%> 23: <p> 24: <asp:DataGrid runat="server" id="dgUC1" font-size="10" /> 

Listing 16.12 renders a DataGrid control with the top ( n ) rows from the Products table and the time the user control is cached. This example takes advantage of the VaryByControl attri bute to vary the cache of the control based on the PageSize variable, which sets the number of rows returned from the SQL query. This user control is set to cache for 30 seconds. The code in Listing 16.13 will work for both the C#.NET and VisualBasic.NET files.

Listing 16.13 Web Form with Two User Controls Included, Each with Different Cache Durations
 [VisualBasic.NET & C#.NET] 01: <%@ Register TagPrefix="DotNetJunkies" TagName="UserControl1" Src="Listing16.11.ascx" graphics/ccc.gif %> 02: <%@ Register TagPrefix="DotNetJunkies" TagName="UserControl2" Src="Listing16.12.ascx" graphics/ccc.gif %> 04:  <body> 05:   <asp:Table> 06:    <asp:TableRow> 07:     <asp:TableCell> 08:  <p> 09:  <DotNetJunkies:UserControl1 runat="server"/> 10:     </asp:TableCell> 11:    </asp:TableRow> 12:    <asp:TableRow> 13:     <asp:TableCell> 14:  <p> 15:  <DotNetJunkies:UserControl2 PageSize="2" runat="server"/> 16:  <br> 17: <DotNetJunkies:UserControl2 PageSize="4" runat="server"/> 18:    </asp:TableCell> 19:    </asp:TableRow> 20:   </asp:Table> 21:  </body> 22:  </html> 

The Web Form in Listing 16.13 demonstrates fragment caching. You will notice that I have registered the two different user controls from Listing 16.11 and Listing 16.12. Within the code, I put each control in its own row in a Table control. Located on lines 15 and 17 is the user control that has the VaryByControl attribute set to vary the cache based on the PageSize variable. The first has a PageSize value of 2, and the second, a PageSize value of 4. When this page is executed, you will receive a page similar to Figure 16.6. There will be three DataGrid controls and three cache times rendered. After 30 seconds has elapsed, try refreshing the page. Only the bottom two DataGrids displaying the Suppliers table will refresh.

Figure 16.6. Web Form with two user controls included. If you refresh the page after 30 seconds, only the Suppliers user control will refresh.
graphics/16fig06.gif
only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net