Click here to Skip to main content
15,881,248 members
Articles / Web Development / ASP.NET
Article

ASP.NET Caching

Rate me:
Please Sign up or sign in to vote.
3.50/5 (10 votes)
11 Dec 2007CPOL6 min read 66.3K   30   5
In this article, I would like to share my experience and knowledge about ASP.NET Caching and how it can improve your website performance.

Introduction

In this article, I would like to share my experience and knowledge about ASP.NET Caching and how it can improve your website performance. As you all might know, performance is the key requirement for any application or piece of code that you develop. For mission critical websites, Caching is the way to go to significantly enhance your web response times. Caching is the process of storing frequently used data on the server to fulfill subsequent requests. You will discover that grabbing data or objects from memory is much faster than recreating the web pages or items contained in them from scratch.

When and Why use Caching

A proper use and fine tuning of the caching approach will result in better performance and scalability of your site. However, improper use of caching will actually slow down and consume lots of your server time and memory usage.

A good candidate to use caching is if you have infrequent changes of data or static content in your web pages.

For example, a stock market website displaying 20 minutes delayed data can actually use data caching to store the data, so that it can reduce database calls every time the user refreshes the page.

Another good example would be using partial page caching to store some of the infrequently changed or static content. If you have websites that show lots of information or features, you can actually group the static or infrequently changed content into user controls and then set the caching for those user controls. You can set the amount of time when the caching expires so that it will load new data from the database.

Types of Caching

  1. Output Caching (Page Caching): The most easiest and simplest caching that you can implement. Good for caching static pages that are accessed frequently. You can copy and paste the code below on your ASP.NET page:
  2. ASP.NET
    <%@ OutputCache Duration="60" VaryByParam="None" %>

    The Duration attribute specifies how long, in seconds, the page will be held in the memory. When the cache expires, the ASP.NET engine will automatically reload and refresh the page again. If the page never changes, you can actually set the duration to a very long period.

    Valid parameters for Output Caching:

    1. VaryByParam - The VaryByParam attribute is for caching different types of pages based on the HTTP Post or HTTP Get protocols. E.g.: you can cache dynamic content based on different query strings provided.
    2. ASP.NET
      <%@ OutputCache Duration="60" VaryByParam="CategoryID" %>

      In this case, the ASP.NET engine will cache the dynamic page based on different query strings provided. If your page generates different content based on the query string, then you need to put that in the output cache directive or else all your users will see the same content.

      If you want to cache a new version of the page based on any differences in the query string parameters, use VaryByParam = "*", as in the following code:

      ASP.NET
      <%@ OutputCache Duration="60" VaryByParam="*" %>
    3. VaryByControl - VaryByControl can be used to cache the user controls inside your page. For example, you can cache a user control that contains a ComboBox that renders all the country names in the world. And perhaps, the country data can be retrieved from a database; this will make significant performance improvement in the page loading time.
    4. VaryByCustom - To make the Cache even more flexible, Microsoft built in the ability to cache a page based on a string, which is then controlled from the actual code of the application.
    5. It does have one "out of the box" property, Browser. When VaryByCustom is set to Browser, the page is cached every time a different browser agent and major version number requests the page.

      ASP.NET
      <%@ OutputCache Duration="60" VaryByCustom="browser" %>

      If you like to have your own set of caching rules, then you might need to tweak some of the code in the global.aspx. E.g., you might need to differentiate caching content for different sets of users based on a cookie called Language. Then, you need to copy and paste the following code in your global.asax file:

      Code in VB.NET

      Overrides Function GetVaryByCustomString(ByVal context as HttpContext,_
          ByVal arg as String) As String
          If arg.ToLower() = "cookies" Then
              Dim cookie as HttpCookie = context.Request.Cookies("Language")
              If cookie isNot nothing Then
              Return cookie.Value
              End if
              End If
              Return MyBase.GetVaryByCustomString(context,arg)
      End Function

      Code in C#

      public override string GetVaryByCustomString(HttpContext context, string arg)
      {
          if (arg.ToLower() == "cookies")
          {
              HttpCookie cookie = context.Request.Cookies["Language"];
              if (cookie != null)
              {
                  return cookie.Value;
              }
          }
          return base.GetVaryByCustomString(context, arg);
      }

      After that, set the VaryByCustom attribute to "cookies" in your OutputCache directives. By doing that, you will generate a different set of caching based on the client languages cookies. This is just one of the examples of doing VaryByCustom caching; you can actually create your own set of caching rules by differentiating caching based on user logged on, and etc.

    6. VaryByHeader: Varies cache entries based on variations in a specified header.
  3. Partial Page (UserControl) Caching - Similar to output caching, partial page caching allows you to cache certain blocks of your website. You can, for example, only cache the center of the page. Partial page caching is achieved with the caching of user controls. You can build your ASP.NET pages consisting of numerous user controls and then apply output caching on the user controls you select. This will cache only parts of the page that you want, leaving other parts of the page outside the reach of caching. This is a very nice feature, and if done correctly, it can lead to pages that perform better.
  4. Note: Typically, UserControls are placed on multiple pages to maximize reuse. However, when these UserControls (ASCX files) are cached with the @OutputCache directive, they are cached on a per page basis. That means even if a User Control outputs identical HTML when placed on pageA.aspx as it does when placed on pageB.aspx, its output is cached twice. You can prevent this happening by adding Shared = true in the output cache directive.

    ASP.NET
    <%@ OutputCache Duration="300" VaryByParam="*" Shared="true" %>

    By putting the Shared attributed, the memory savings can be surprisingly large. If you have an ASCX User control using the OutputCache directive, remember that the User Control exists only for the first request.

  5. Data Caching - Output caching and partial page caching is useful if you want to cache the output of a page. However, if you like to cache a DataSet object or any collection object, you can use data caching to implement that.
  6. ASP.NET has a class called Cache to cache specific data items for later use on a particular page or group of pages. The Cache enables you to store everything from simple name/value pairs to more complex objects like datasets and entire aspx pages.

    VB.NET

    VB
    Cache("MyDataSet") = myDataSet;

    C#

    C#
    Cache("MyDataSet") = myDataSet;

    To retrieve data from the cache, you can use the code below:

    VB.NET

    VB
    Dim ds as New DataSet
    ds = CType(Cache("MyDataSet"),DataSet)

    C#

    C#
    DataSet ds = new DataSet();
    ds = (DataSet) Cache["MyDataSet"];

Conclusion

As we've just seen, caching Web pages with ASP.NET is amazingly easy, accomplished with a simple line. With this glimpse of ASP.NET's caching abilities, you can improve your Web application's performance. But remember, caching in ASP.NET is a trade off between CPU and memory. Something like, how hard is it to make this page versus whether you can afford to hold 200 versions of it. If it's only 5KB of HTML, a potential megabyte of memory could pay off handsomely versus thousands and thousands of database accesses - every page of request served from the cache saves you a trip to the database.

Reference

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I work in a Software House Company in Malaysia (Kuala Lumpur) and I am MCP Certified in C# and Web Application course.

I originally started my programming in Java but later on changed to Microsoft platform because of the simplicity and ease of use.

I love .NET programming and am doing it almost every day now.
I normally post lots of my articles here
ASP.NET Tutorial

I also host my website at ASP.NET Web Hosting

Comments and Discussions

 
GeneralMy vote of 3 Pin
jigarpandya201217-Sep-12 19:35
jigarpandya201217-Sep-12 19:35 
Questionasp.net cache Pin
wesnur11-Jul-11 0:26
wesnur11-Jul-11 0:26 
Generalnice Pin
mlpo89769-Nov-10 22:10
mlpo89769-Nov-10 22:10 
nice article!
GeneralMy vote of 3 Pin
mlpo89769-Nov-10 22:10
mlpo89769-Nov-10 22:10 
QuestionCache issue -Help Pin
mpchand16-Dec-08 8:01
mpchand16-Dec-08 8:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.