How ASP.NET Uses Caching


Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Day 14.  Using ASP.NET's Improved Caching Capabilities

ASP.NET comes with many caching mechanisms built-in. Caching is very important to Web applications because data transfer over the Internet can be painfully slow. By caching data, ASP.NET gives its applications a much-needed performance boost. ASP.NET also allows developers to modify cache settings as needed.

There are two caching locations for data in ASP.NET: client-side and server-side. Caching performed by a browser is client-side all data is stored on the user's computer and is initiated and managed by the browser. Server-side caching is managed by the server using the server's resources, and the browser has no control over it whatsoever.

Server-side caching is the focus of today's lesson. You'll see that this method allows much greater flexibility and benefits than client-side caching. Figure 14.2 illustrates the differences between the two methods.

Figure 14.2. The differences between server-side and client-side caching.


You'll also see that caching isn't limited to images or Web pages. It spans a wide range of data types. The next few sections take a look at a few of the caching mechanisms of ASP.NET.

Page Caching

All ASP.NET pages are compiled when they're requested. If you've ever developed applications in any compiled programming language, you'll know that compiling a program takes some time and computing power. If an ASP.NET Web page had to be recompiled each time it was requested, the performance of ASP.NET applications would suffer tremendously.

Luckily, once ASP.NET pages are compiled, they're cached on the server. It's these compiled pages that are served to clients. The user will experience some sluggishness in performance on the first request to the file due to compilation, but subsequent requests will be faster.

The Common Language Runtime monitors the source files for changes. If you modify the original .aspx file, the change will be detected, and ASP.NET will then render the existing cached page invalid and will compile and cache the new page.

Thus, ASP.NET provides improved performance by caching the compiled pages instead of recompiling them every time.

Configuration Caching

If you've been developing ASP.NET applications along with this book, you might have noticed another performance setback. The first time an application is started that is, the first time any page is requested from a virtual directory ASP.NET must load all configuration settings from web.config (see Day 18, "Configuring and Deploying ASP.NET Applications"). This can take some time, depending on the complexity of your application.

Take a look at the system web.config file (typically located at C:\WINNT\Microsoft.NET\Framework\version\CONFIG\machine.config). You'll see that it can get pretty complex. Typically, this file is more than 450 lines long! These configuration settings are cached the first time an application is started, allowing ASP.NET to apply configuration information to its files much faster than it could without caching.

Output and Data Caching

Page output caching is the process of caching any content generated by dynamic pages, such as calculated values or even controls such as DataGrids. Output caching doesn't happen automatically, but it's easy to use. You'll learn more in "How to Use Caching" later today.


Don't confuse caching with buffering. You use a buffer to store output temporarily until execution is finished, at which point the buffer is emptied. You use caching to store data for an extended amount of time so that it's available to you later. These two methods of data storage sometimes confuse beginners.

Data caching allows you to store any type of data in cache, from database records to user controls. It's similar to output caching, but you're not limited to content generated by a page. This method doesn't happen automatically, so what needs to be cached is up to you.


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne © 2008-2017.
    If you may any questions please contact us: