Control Your Cache, Speed Up Your Site

Nancy Cluts
Microsoft Corporation

March 18, 1999

Contents

What's Wrong With the Current Expiration Schemes?
How Internet Explorer 5 Uses Cached Data
Lazy Update
More Information

Most Web pages combine content that is static with content that is dynamic. The dynamic content can change every 30 seconds or every week—it depends on the Web site. An example of dynamic content is stock ticker data. The static content will change so infrequently that you know it will always be on the page. An example of static content is a Web site's logo.

As a result, it would certainly make sense that you could speed up your Web site's responsiveness by downloading only the content that changed (i.e.: the dynamic content). The content that changes infrequently would be stored in the client's memory cache. That way, the static content would not have to be downloaded every time the page was displayed; instead, it would be retrieved swiftly from the cache. The larger number of static items you have on your Web site, the greater the performance increase you can get by caching that content. In fact, many sites on www.microsoft.com (such as the Windows NT Server site) have taken the time to cache their static items and have reaped the benefit of a noticeable performance increase.

Internet Explorer 4.0 offered a way to cache content by setting an expiration for the content. The expiration directs the server to download content immediately or to wait until the content has expired. Internet Explorer 5 offers an even greater enhancement of caching that improves the responsiveness for Web sites via an improved use of the EXPIRES directive and a method known as lazy update.

Bear in mind that the features discussed in this article are supported in Internet Explorer 5, and were not available in previous versions (including the developer preview) of Internet Explorer.

What's Wrong With the Current Expiration Schemes?

In Internet Explorer 4.0, the Web browser uses Hyper Text Transfer Protocol (HTTP) to transfer data. Within this protocol, header information describes the type of content that is being transferred, the date and time that the content was sent, and so forth. You can use the Win32 function HttpQueryInfo and specify HTTP_QUERY_EXPIRES in the call to the function to determine when the content attached to the HTTP header should be considered out of date. Using this attribute, the Web browser on the client could determine whether the data should be refreshed from the server. The problem is that this header information was not used everywhere on the Internet. As a result, the browser had to use another mechanism as well to check for data freshness (i.e.,: the browser would conditionally call GET to determine whether the version of the object in the cache was the same as the version on the server). Unfortunately, this conditional GET took enough time—and most objects are small enough—that using it didn't provide much of a benefit over just refreshing the data regardless of the version.

How Internet Explorer 5 Uses Cached Data

Internet Explorer 5 uses a new mechanism to determine whether to reload data. When it sees a cached object that includes an EXPIRES header, Internet Explorer 5 will use this information to determine whether to contact the server. If the expiration date is in the future, the object will be retrieved from cache and the server will not be contacted at all. As a result, any sites that use the EXPIRES header will afford their clients a noticeable performance gain.

You can set EXPIRES either on the server or by using a <META> tag. If you set it on the server, you can specify different expiration values for different objects on the Web page. For example, using ASP, you can set Response.Expires to a negative number to turn off buffering or set it to some other value to specify buffering. This means that you could set a particular object on the site (such as the link of the week) to expire within a week, while the stock ticker object could expire immediately (for immediate refresh).

If you want to set EXPIRES using the <META> tag, use the following syntax:

<META HTTP-EQUIV="expires"
  VALUE="Tue, 23 Jun 1998 01:46:05 GMT">

Lazy Update

Internet Explorer 5 has a new feature called lazy update. Lazy update allows a Web site to cache an object as well as being able to count the number of hit counts for the object. This is useful for advertising images, start pages, or search pages. On these pages, you want the user to get an immediate view of the page, but you still may want to update the content at some point and to keep track of how many eyeballs have seen your page. For a lazy update page, the object is downloaded and cached the first time the page is visited. Subsequently, the user will see the cached copy. If the server has a new copy, the page will be downloaded into the cache in the background. The next time the user visits the page, the updated content will be displayed.

To provide this behavior, Internet Explorer 5 uses two new time interval directives to HTTP headers: pre-check and post-check. The pre-check extension defines a time interval (expressed in seconds) after which an object should be checked for freshness prior to being shown to the user. This check can occur after the object has already been displayed to the user, but it ensures that the next time the user needs to see that object, the cached copy will be up-to-date. The post-check extension defines a time interval (in seconds) after which the object must be checked for freshness but before the object is displayed to the user. Because these are extensions to HTTP header information, they can be used only on pages retrieved via the HTTP protocol. In addition, the server must generate HTTP 1.1 responses. If you are using Internet Information Server (IIS) 4.0 or later, this is supported.

By altering the way it handles the EXPIRES property and by using these cache-control extensions to make fewer roundtrips to the server and render content more quickly, Internet Explorer 5 can improve your Web site's performance on the user's machine. In addition, content can be updated in the background and displayed on subsequent visits.

More Information

If this has whetted your appetite for more information about cache control and Internet Explorer, take a look at the following links. More detailed information about cache control, persistence, and HTTP can be found on the pages listed below.

Be real nice to Nancy Cluts, developer-technology writer extraordinaire, and maybe she'll take you for a ride in that gorgeous red convertible.