Thursday, December 22, 2011

Caching in asp.net: Fragment Caching



Caching:


Caching is the process of storing frequently used data, usually data that is costly to generate, for reuse. Typically this data is stored in memory since retrieving data from memory is much more efficient than retrieving the data from other locations, such as a database.

Caching enables you to store the expensive data into Cache object and later retrieve it without doing expensive operations. A very common example where you want to use caching is datagrid paging. I am sure you all are familiar with datagrid paging which enables you to view the records in multiple pages. Each time you visit a different page all the records are fetched from the database. This becomes very expensive operation. Caching can save a lot of expensive operations since you can store all the records in the cache object and use the cache object as the data source. In this article we will see some important features that caching provides.

There are 3 Caching Type

a. Output Caching
b. Data Caching
c. Fragment Caching

Fragment Caching:
  • Fragment caching refers to the caching of individual user controls within a Web Form. 
  • Each user control can have independent cache durations and implementations of how the caching behavior is to be applied.
  • Fragment caching is useful when you need to cache only a subset of a page. 
  • Navigation bars, header, and footers are good candidates for fragment caching. 
Example
ASCX PAGE

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CahceUserControl.ascx.cs"
Inherits="CahceUseControl" %>

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

<p><asp:Label ID="lblTime" runat="server" EnableViewState="false"
ForeColor="GradientActiveCaption" /></p>

ASCX CODE BEHIND

protected void Page_Load(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString();
}
Now create a .aspx page.
ASPX PAGE

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CahcedControl.aspx.cs"
Inherits="CahcedControl" %>

<%@ Register Src="~/CahceUserControl.ascx" TagPrefix="uc1" TagName="UserControl" %>

<!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>
         Try to refresh this page, the Date time doesn't change. Keep refreshing and after
5 seconds you should see the change in time.
        <uc1:UserControl runat="server" ID="uc11" />
    </div>
    </form>
</body>
</html>
In the above code snippet, we have an asp:Label control in the user control and we are writing the current Date Time in the Page_Load method of the user control. That user control is being used into the CachedControl.aspx page. So normally every time the .aspx page refreshes, the DateTime should change but as we have kept the OutputCache directives onto the user control and specified duration as "5", the User control output data will be cached for 5 seconds. So till 5 seconds of the first request even if the page is refreshed the DateTime value doesn’t change on the page. After 5 seconds the user control is processed again on the server and fresh data is cached and subsequent request till next 5 seconds is served from the Cache again.


No comments:

Post a Comment