1. Computing

Program an ASP.NET File Based Output Cache

A "Pro Level" technique to improve the performance of your ASP.NET website!

From , former About.com Guide

Updated July 18, 2010

Caching is the technique of storing a copy of some information that takes processor cycles and time to create. There are several different types of caching used by advanced web technologies like ASP.NET. For example, you could cache the results of a complex database query. Then later requests don't need to access the database at all. Instead, they can grab the data directly from the cache.

Note: This article is adapted from the book Pro ASP.NET 4.0 in VB 2010, scheduled to be published in October 2010. Matthew MacDonald and Dan Mabbutt are the authors and this except is available by permission of the publisher, Apress. While most articles at About Visual Basic are intended for people who are still learning, this one is for people who already know most of what they need. Matthew is the guiding guru in writing the book. Dan only did the work to write the ASP.NET 4.0 VB.NET version. But any errors here are exclusively the fault of Dan!

The real beauty of caching is that unlike many other performance-enhancing techniques, caching bolsters both performance and scalability. Performance is better because the time taken to retrieve the information is cut down dramatically. Scalability is improved because you work around bottlenecks such as database connections. As a result, more simultaneous page requests with fewer database operations are served.

With ASP.NET, you get first-rate caching for free, and you have a variety of options. You can cache the completely rendered HTML for a page, a portion of that HTML, or arbitrary objects. You can also customize expiration policies and set up dependencies so that items are automatically removed when other resources-such as files or database tables-are modified.

ASP.NET has two types of caching:

  • Output caching: This is the simplest type of caching. It stores a copy of the final rendered HTML page sent to the client. The next client that submits a request for the page doesn't actually run the page. Instead, the final HTML output is sent automatically from the cache. There is virtually zero time required from the server to satisfy the request.
  • Data caching: This type of caching requires code in your application. Data caching works by storing information that takes a lot of server time to reconstruct (such as a DataSet retrieved from a database) in the cache. Other pages can check for the existence of this information and use it. The steps usually needed to rebuild it aren't needed.

Output Caching

With output caching, the final rendered HTML of the page is cached. When the same page is requested again, the control objects are not created, the page life cycle doesn't start, and none of your code executes. Instead, the cached HTML is served. Clearly, output caching gets the theoretical maximum performance increase, because all the overhead of your code is sidestepped.

ASP.NET 4 added new support for creating custom output cache providers. (A software "provider" is a bridge between your program and some service, in this case, caching. It lets Microsoft give you a standard, less complex interface because the provider takes care of the interface with the service.) You'll see this feature used in products from third-party developers to create components that work with all kinds of storage for the cache.

The usual ASP.NET caching model works very well for most web applications. It's simple to use and blisteringly fast, because the cache service runs in the ASP.NET process and it stores the information cached in memory. But it doesn't work as well if you want to cache huge amounts of data for long amounts of time. For example, consider the sprawling product catalog of a giant e-commerce company. Assuming the product catalog changes infrequently, you may want to cache thousands of product pages to avoid the expense of creating them. With this much data, using web server memory is risky. Instead, you might prefer to rely on another type of storage that's slower than memory but still faster than recreating the page and less likely to cause resource bottlenecks. Some options include disk-based storage, database-based storage, or a distributed storage system like Windows Server AppFabric.

In the past, systems that could do this have not been part of ASP.NET. As a result, every third-party caching solution has its own programming API. ASP.NET 4.0 has finally added a "provider model" to its caching architecture. Now, you can plug in cache providers that use different data stores. But keep in mind that ASP.NET doesn't include any pre-built caching providers yet. Members of the ASP.NET team have demonstrated prototypes that use file-based caching and Windows Server AppFabric. The plan seems to be to turn these into separate components that you can download for free from CodePlex, the Microsoft supported open source site. And look for examples from Microsoft that will show how to integrate caching systems like "MemCached" (a popular open-source distributed caching system) with ASP.NET output caching.

In the rest of this article, you'll consider a basic example of a file-based caching solution.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Using VB.NET
  5. ASP.NET Output File Based Cache

©2013 About.com. All rights reserved.