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

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
Print this page.
Published: Friday, September 10, 1999

Caching a Web Page
By Julian Sitkewich

Note from the WebMaster (Scott Mitchell):
Hey-yo. I just wanted to share my opinions on this article. It is a neat idea, cachine a web page. At first, you might be inclined to think it would be quicker than using a standard HTML page, especially for an HTML page that is requested often. I don't know for sure, but I would assume the HTML page approach would be faster. To use the Application object, you need to be using an ASP page. This requires that the page be processed before the HTML output is sent to the user. While you may argue that this is quicker than having to read the HTML file off the disk each time it is requested, rest assured that IIS caches commonly requested pages, so there is not that disk access each time.

- continued -

However! That does not mean that this is not a great article. It discusses some important concepts: Global.asa, the Application object, and the FileSystemObject. Also, Julian makes an intersting comment near the end of this article, about how this concept can be expanded. This article is definately worth a read! OK, enough from me, read on! :)

Have you ever wanted to "cache" a page or file in server memory for quick performance? Well this quick tutorial can show you how using the Application Object. (Unfamiliar with the Application Object? Here is an article that might help! Caching Data, by Ian Stallings)

When your web application is started, the Application section of the Global.asa is executed. For example:

'   global.asa
Sub Application_OnStart
	Application("ConnectionString") = "MyOleDB;"
	' establish data connection strings
	' set session timeouts
     	' etc, etc.
End Sub

What we will do, is place some code inside the Application_OnStart sub to load a web page into the Application object. All items loaded into any Application Object are globally available to users and exist until the Application exits (server rebooted, etc.). The code essentially reads a web page using the FileSystemObject and loads it directly into the Application Object. This makes the page globally available when the Application is started.

Ok, let's look at this simple code:

Sub Application_OnStart
	'  Create Object
	'  Set application-scope object object 
	Set fso = CreateObject("Scripting.FileSystemObject")
	' Get the web page to be loaded
	' CHANGE this to the physical file location. 
	' If you don't want to keep it simple, you could do a UNC lookup	 
	set pagetext = fso.GetFile("E:\Inetpub\wwwroot\cached.html")	'Open a file for reading
	' Open the page
	set ts = pagetext.OpenAsTextStream(1,-2)	
	' Now, read each line of the page and add it to our sWebPage variable
	Do while not ts.AtEndOfStream
		 sWebPage = sWebPage & ts.readline & Chr(10)
                ' And finally we set variable to our web page variable
	Application("cached.html") = sWebPage
End Sub

Alright then, let's put it into action. Place the web page cached.html in your root directory, which just contains simple HTML. And then place the ASP page DisplayCached.asp in your root directory as well. This page just contains a one-liner Response.Write to display the contents of our Application Object. Now copy the above code and add it to your Global.asa file. You might want to retain the code that is already inside Application_OnStart, so be careful how you paste.

Restart your web application (restarting your computer is one way you can do this) and then load the page DisplayCached.asp. You should see the contents of cached.html. Pretty cool, huh!? Now that we've accomplished this, what other kinds of content could you set an Application object to? A commonly used recordset, perhaps? Or maybe some non-HTML thing. This could be done by setting Response.ContentType = "type/subtype" in DisplayCached.asp. Hmmm... What about modifying the contents? Keep in mind we are using server memory when storing an application-scope object.

Have fun with it, try something new, and learn... :)

Happy Programming!

Related Articles

  • Caching Data
  • Reading/Writing Text Files
  • Everything you wanted to know about Global.asa, but were afraid to ask!

    Attached Files:

  • Source code for DisplayCached.asp in text format
  • Source code for cached.html in text format.

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