Browse DevX
Sign up for e-mail newsletters from DevX


Learn Better Ways to Manage Your Cache in ASP.NET

Learn how to use caching effectively today and tomorrow with some new (and some not-quite-as-new) cache management techniques for ASP.NET 1.x and 2.0.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

aching is one of the features in ASP.NET 1.x that most developers are familiar with. And it has helped to fuel a yearning for cache dependency in SQL Server. The ability to refresh the cache if a row in a table has been modified is very useful to a lot of developers. And hence in ASP.NET 2.0, Microsoft built SQL Server cache dependency right into the product.

In this article, I'll look at the various ways you can cache your Web pages in ASP.NET 2.0; some of these techniques are even applicable in ASP.NET 1.x.

Technique #1: Page Output Caching
(ASP.NET 1.x and ASP.NET 2.0)
By caching the output of a page, the page need not be re-generated on every request; this is the basic logic behind page output caching. Suppose I have a Web form with a GridView control bound to a SqlDataSource control (see Figure 1).

Author's Note: If paging is enabled on the GridView control, do not enable page output caching; it can cause page-loading problems.

When the page loads, I change the SelectCommand of the SqlDataSource control so that the GridView control displays rows based on the product name. The product name is retrieved from the query string of the URL. For example:


Here is the Page_Load event:

Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles Me.Load Dim productName As String = _ Request.QueryString("productName") SqlDataSource1.SelectCommand = _ "SELECT * FROM Products WHERE ProductName LIKE '%" & _ productName & "%'" Response.Write("Page created on :" & Now) End Sub

To cache the output, add an "OutputCache" directive to this page:

<%@ OutputCache Duration="15" VaryByParam="none" %>

Figure 1. Basic Cache: The following application, a Web form with a GridView and SqlDataSource controls, will make a good example for basic caching.
The Duration attribute specifies that the page will be cached for 15 seconds. That is, once a page is loaded it will be cached for the next 15 seconds—any request for the page during this period will be served by the cache. The VaryByParam attribute specifies the condition for invalidating the cache. In this case, if it is set to "none," the validity of the cache is not dependent on the query string. That is, no matter what you enter in the query string the cache will be valid. For example, you should see the same page if you enter the following URLs in succession (within the 15 seconds time frame):

http://localhost:59140/Cache/Default.aspx?productName=pears http://localhost:59140/Cache/Default.aspx?productName=seed http://localhost:59140/Cache/Default.aspx?productName=chef

To set a condition to invalidate the cache, specify the parameter name in the VaryByParam attribute. For example:

<%@ OutputCache Duration="15" VaryByParam="productName" %>

The above statement invalidates the cache if the productName parameter is changed. So the following URLs will cause the page to regenerate on every request:

http://localhost:59140/Cache/Default.aspx?productName=pears http://localhost:59140/Cache/Default.aspx?productName=seed http://localhost:59140/Cache/Default.aspx?productName=chef

If you have more than one parameter in the query string, separate the parameter name by a semi-colon, as in:

<%@ OutputCache Duration="15" VaryByParam="productName;price" %>

To invalidate the cache based on all parameters in the query string, use "*":

<%@ OutputCache Duration="15" VaryByParam="*" %>

What You Need
  • Microsoft Visual Studio .NET 2003
  • Microsoft Visual Studio 2005 (Whidbey)
  • Microsoft SQL Server 2000

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date