Boost Performance With Caching
Maximize your Web applications' scalability by using ASP.NET page- and data-caching techniques.
by Leonard Lobel
November 2003 Issue
Technology Toolbox: VB.NET, ASP.NET
Scalability is essential to effective Web applications. A scalable application does its work with less CPU processing, fewer trips to the database, reduced RAM requirements, and minimum resources. You usually achieve maximum scalability by taking an aggregate of measures, including caching. In simple terms, caching is a technique that reuses previously performed processing in subsequent requests for the same information without doing the work all over again. Caching data in Web apps avoids unnecessary processing, which in turn improves scalability.
Classic ASP developers must create their own solutions, or resort to expensive add-on solutions, to gain scalability and performance with caching. ASP.NET comes to the rescue with easily accessible caching functionality that's built into the .NET Framework. I'll explain this functionality and the techniques you can use to take advantage of it in your applications.
ASP.NET lets you cache frequently accessed pages. When a client makes a request of your application, ASP.NET generates the response on the fly and delivers it as an HTML page. The page content typically is based on parameters the client passes and/or data the app retrieves from a database or other data source on the server side. In many cases, the resulting page doesn't change from moment to moment, and it provides the same information for identical requests—for example, product catalog pages based on databases updated once daily. Even frequently changing data, such as stock quotes, might not be updated more often than every few minutes. Processing every client request for pages you know don't vary with great frequency is wasteful. Such pages are perfect candidates for caching (see Figure 1).
ASP.NET caches pages on a per-page basis, which allows you to specify individual pages to cache in different ways or not at all. A powerful extension of this feature is partial caching, which lets you cache portions of a page—specifically, user controls. A page can include several user controls whose HTML is cached, while the rest of the page's content is regenerated on each request.
You can enable caching for a page either declaratively or programmatically. You express declarative caching by including a directive at the top of the ASPX file. You specify programmatic caching in the page's code-behind module by manipulating the HttpCachePolicy object. Both methods work the same way for basic caching, and it doesn't matter which you choose. However, you can't express some less-common caching requirements declaratively, in which case you must use the HttpCachePolicy object.
ASP.NET looks for directives at the top of each ASPX file before processing the page. You declare a specific ASPX page for caching by including the OutputCache directive at the top (typically, immediately below the Page directive):
<%@ OutputCache
Duration="#ofseconds"
Location="Any | Client | Downstream
| Server | None"
VaryByCustom="browser |
customstring"
VaryByHeader="headers"
VaryByParam="parametername" %>
The key attribute is Duration, which specifies how long (in seconds) to cache the page. ASP.NET places the page into the cache the first time the page is generated. It serves subsequent requests for the same page directly from the cache without reprocessing the page. The page is discarded from the cache when the amount of time you specify in Duration expires. The next request generates the page anew and places it into the cache, starting the procedure all over again.
Control Where a Page is Cached
You can cache the generated page in one of several places (see Figure 2). The Location attribute specifies where the cached page resides. Although caching achieves the main goal of avoiding repetitive processing on the server, the cache's location still impacts performance in terms of network I/O that must occur to get the cached page to the client.
Back to top
|