SIGN UP MEMBER LOGIN:    
ARTICLE

Caching in ASP.NET 2.0

Posted by Raj Kumar Articles | Web Services in C# January 08, 2008
Caching is a technique of storing a copy of data in memory. You could cache a page or the results of a query. The advantage of caching is to build better performance into your application.
Reader Level:
Download Files:
 

Introduction

Caching is a technique of storing a copy of data in memory. For example, you may cache the a page or the results of a query. The advantage of caching is to build better performance into your application. Accessing cached data in memory is much faster than re-building a page or re-querying the database.

There are two types of caching in asp.net 2.0:

  1. Output Caching
  2. Data Caching

1. Output Caching

Output caching stores an HTML page in a cache that will not refresh until your specified duration time passes.
 
<%@ OutputCache Duration="30" VaryByParam="None" %>.
 
Duration -> Allows you to specify time in seconds.

VaryByParam -> Output based on query string.

<%@ OutputCache Duration="30" VaryByParam="UserID" %>.

Result



The date/time will refresh automatically after 30 seconds if you put the following code on a page load event.

protected void Page_Load(object sender, EventArgs e)
{

     LabelDate.Font.Bold = true;

     LabelDate.Font.Italic = true;

     LabelDate.Text = "The DateTime is now:<br/>";

     LabelDate.Text += DateTime.Now.ToString();
}

Put this line on top of your .aspx page and set your duration time as desired.

<%
@ OutputCache Duration="30" VaryByParam="None" %>.

 
2. Data Caching

Data caching is the most important type of caching. The basic principle of data caching is that you can add items in your collection object called Cache.

Example

protected void Page_Load(object sender, EventArgs e)

{

    LabelData.Font.Bold = true;

    LabelData.Font.Italic = true;

    if (Cache["RajCacheItems"] == null)

    {

         LabelData.Text += "Raj creating items.....<br />";

         DateTime dateTime = DateTime.Now;

         LabelData.Text += "Storing items in cache ";

         LabelData.Text += "for 50 seconds.<br />";

         Cache.Insert("RajCacheItems", dateTime, null, DateTime.Now.AddSeconds(50), TimeSpan.Zero);

    }

    else

    {

         LabelData.Text += "Accessing RajCacheItems.....<br />";

         DateTime dateTime = (DateTime)Cache["RajCacheItems"];

         LabelData.Text += "RajCacheItems is '" + dateTime.ToString();

    }

}

Result


 
Data Source Control Caching

This is the caching that is built into the data source controls like SqlDataSource, ObjectDataSource and XmlDataSource.
 
There are four properties of caching in SqlDataSource:

  1. Enable Caching: You need to set the enable caching property to true; the default value is false.
  2. Cache Expiration Policy: Absolute and sliding.
  3. Cache Duration: Duration time in seconds.
  4. CacheKeyDependency: Makes a cached item dependent on another item in the data cache or on a table in your database.

Example

<
asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"

SelectCommand="SELECT [Id], [FirstName], [LastName], [Address], [City], [State], [Country] FROM [Records] WHERE ([City] = @City)"

EnableCaching="True" CacheExpirationPolicy="Sliding" CacheDuration="20">
<SelectParameters>

<asp:ControlParameter ControlID="DropDownList1" Name="City" PropertyName="SelectedValue" Type="String" />

</SelectParameters>

</asp:SqlDataSource>

Result 



For another example I am attaching an application for you to use in your database. I hope you guys will like this article; if you did, then drop me a line in the c-sharpcorner comment section.

Login to add your contents and source code to this article
share this article :
post comment
 

You have done a great job.

Posted by Vivek Singh Feb 26, 2008

Thanks, i'll do that next time.

Posted by Raj Kumar Jan 14, 2008

hey i like your articles. i read your all article. i am looking some xml and xaml articles from you. good work Keep It UP!!!

Posted by sneha pathak Jan 14, 2008
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor