When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
Published: Wednesday, March 20, 2002

A Classic ASP Page Caching Object

By Joe Audette

A common scenario in ASP development is to dynamically create web page content by pulling data from a database. This capability is very important in many applications where the data changes frequently from minute to minute. However, in situations where the data changes less frequently, such as once a day or every few hours, it is inefficient to hit the database for every page request. Doing so reduces the maximum number of simultaneous page requests that the server can handle, or in other words, scalability.

- continued -

One solution is to upgrade to ASP.NET, which has some great caching features. (For more information about this topic, be sure to read the excellent article by Scott McFarland, Caching with ASP.NET.)

If you are looking at extending the scalability of an existing ASP application, in most cases upgrading to ASP.NET is not feasible because of the great deal of time you have already invested in developing the ASP Application. Porting it to ASP.NET would be prohibitively expensive.

Therefore, we'd ideally like to be able to have an easy to use caching solution for classic ASP that can be simply plugged into existing ASP pages to allow for page-level caching. In this article I will be introducing a VBScript page caching object that I have created and included here for you to use free of charge. The object is capable of caching to files or to application variables in memory on the Web server and can be added to any ASP page with just a few lines of code. I will explain how the object works and review the results of Web Stress testing. Using the free Microsoft Web Stress Tool, major improvements can be seen in scalability and performance with page caching enabled. (Step by step instructions for using this object can be found in the Readme.txt file included in the downloadable zip file that accompanies this article. I have also commented the code in objPageCache.asp thoroughly for those of you who want to fully understand the implementation.)

Getting Started
In order to start using my page caching object, you will need to ensure that your Web server has the following technologies installed:

  • Windows Script Host 5 or higher (download)
  • Version 4 of the Microsoft MSXML library (download)

Usage Overview
The class for the page cache object is contained in the objPageCache.asp file included in the download. To use the class you include it on your ASP page just before any content is written to the response. The page has its own script tags. To include the class within your script, you will need to wrap it in closing and opening script tag like this:

    <!-- #INCLUDE FILE="./Include/objPageCache.asp" -->

Of course the path to your include folder may be different. (If you are unfamiliar with include files be sure to read: The Low-Down on #include.)

If you have any logic in your ASP page that re-directs the user to another page, you should put the page caching object (i.e., the #include call shown above) somewhere on the ASP page beneath the redirect logic in order to maintain the re-direct functionality.

Immediately after adding the page caching object via the #include call, go ahead an instantiate the object using the following code:

Dim objCache
Set objCache = New CPageCache

By default the object will cache the page for 24 hours. You can override this behavior by specifying a cache interval unit and length:

'your other choices are CI_HOURS or CI_DAYS
objCache.CacheIntervalUnit = CI_MINUTES

'this will set the cache to expire every 10 minutes
objCache.CacheIntervalLength = 10

Next, call one of the AutoCache methods to cache the page. If you want to cache the results to Application variables in memory use the AutoCacheToMemory() method; if you want to cache the page to a text file, use AutoCacheToFile().

Cache to Application variables...

Cache to a file...

Finally destroy the object using Set objCache = Nothing. That is all the code that you need to add to your ASP page(s) to enable caching using the page caching object. Please see the readme file for step by step instructions on using this object. There are a few other small steps that must be taken on the server.

How it works
When the page is requested the objCache object is instantiated before any content is returned. When the object initializes, it determines the complete URL for the current request. In other words it knows what page it's on. Based on this information it determines what the cached file or application variable should be named. When the AutoCacheToMemory method is called, the object checks to see if the application variable exists, and if it exists, makes sure it has not expired. If the application variable exists and has not expired it writes the application variable to the response. Then it calls Response.End to prevent the dynamic content below it on the page from being processed thus sparing the server expensive hits to the database.

If the application variable is empty or expired the object uses the XMLHTTP control to request the HTML output from the dynamic ASP code on the page; note that the XMLHTTP control must pass in an extra query parameter, cachecommand=bypass, to tell the cache object on that instance of the page to do nothing and just let the dynamic content be returned. When the HTML response returns, the object assigns it to the application variable and writes a second application variable indicating what time the first variable was assigned. The object then writes the freshly created cached content from the application variable to the response and calls Response.End. (For more information on using XMLHTTP be sure to read this FAQ.)

If the ASP page has a form that posts back to itself, you should include a hidden field in the form:

<input type="hidden" name="IsPostback" value="true">

The object will look for this field in the request and if it is there it will do nothing thereby allowing the code on the page to handle the request.

AutoCacheToFile does exactly the same thing except that it uses the file system object to store and retrieve the cached content using text files instead of application variables. You configure the location of this folder using an application variable Application("CachedContentFolder") in your global.asa file. (For more information on the FileSystemObject object, be sure to review the FAQs in the FileSystemObject FAQ category!)

Keep in mind that whether you use AutoCacheToMemory or AutoCacheToFile, the object will make a separate cache for each combination of query string parameters that are passed with the page request. If there is a request for http://myserver/products.asp that request will create one cache item and if there is a request for http://myserver/products.asp?cat=tools&subcat=electric, that request will make an entirely different cache item. When users make subsequent requests for any page it will come from the respective cache item. Once the cached page is expired it will be recreated upon the next request.

In Part 2 we'll examine how to specify parameters to the page caching object via the querystring. We'll also look at the performance results of ASP pages not cached vs. those that have been cached using the caching object.

  • Read Part 2!

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article