共用方式為


Taking Advantage of HTTP Cache-Control in Microsoft SharePoint Server 2003

Microsoft Office SharePoint Server 2007 (MOSS 2007) has some great output cache settings, but what about SharePoint 2003? Internet Explorer does conditional caching by default, but wouldn't it be nice to eliminate the majority of HTTP round trips (HTTP 304 - Not Modified) requests?

In the Internet Information Services (IIS) administration console, navigate to the web site properties of our SharePoint 2003 enabled web site and add the HTTP header "Cache-Control" as a custom HTTP header with a value of "max-age=86400" which effectively sets an expiration date of 24 hours. This can also be accomplished by enabling the content expiration feature in the same form. This will now add the Cache-control HTTP header to all outbound content on the web site. This has the effect of making the web site load *very* quickly due to far less HTTP round trips.

Important facts to know about this setting:

1) While this setting applies to all of the outbound SharePoint content - not all of the content is cacheable by the browser. I'll describe some of my observations later in this posting.

2) Normally, this setting only applies to content in the IIS file system, but since we set it at a web site level, it is applied to all of the outbound traffic.

Since this brings great performance, then why is this not on by default? Well, there are drawbacks to this configuration. The biggest drawback is that *nearly all* of the content will be cached. This means that images loaded on the web site as content will show the cached data until the expiration date you set has been reached or if the user explicitly does refresh in the browser and other SharePoint content such as Microsoft Office 2007 documents will also show the *cached* version of the document versus any updated versions. To some people this is a very important issue, but again it is worked around by refreshing the browser.

Here is the cacheable items that I experienced:

Non-Cached items: ASPX pages and Microsoft Office 2003 documents do not seem to be effected by the cache-control setting. Effectively, everything that is *not* opened directly in the browser with the exception of ASPX pages is not cached.

Cached items: Effectively everything that is opened by the browser is cached. This includes images (*.jpg, *.gif, *.tif, etc) and Microsoft Office 2007 documents (*.pptx, *.docx, *.xlsx). This means once it is downloaded once, the browser will just get it from local cache and won't ask for it unless the content has expired, or an explicit Refresh is done in the browser (Internet Explorer).

So how would you get the best of both world - meaning you want to explicitly not cache certain content items under certain paths? We considered using HTTP handlers in IIS6 to do this, but HTTP handlers only work with .NET content - not static items such as Office 2007 documents and images. Therefore, the only viable solution is to upgrade to MOSS 2007 (assuming it supports this kind of caching) or implement an IIS ISAPI filter.

IIS ISAPI filters are DLL's that have exclusive access to inbound and outbound HTTP traffic before and after IIS is finished processing the request. In this case, you could write an outbound ISAPI filter that looks for a specific path and a specific file extension, then have it modify the cache-control header on the way out. While this is a great benefit, developing a custom ISAPI filter is no easy task.

In conclusion, it is certainly possible to greatly increase the perceived performance of SharePoint 2003 web sites by adding the cache-control (expiration date) on your content, but be aware of the disadvantages of this implementation. Furthermore, to do cache control correctly, you would need a custom ISAPI filter and that can be a difficult task.