Optimize Output Caching for Dynamic Web Pages (IIS 7)
Applies To: Windows 7, Windows Server 2008, Windows Server 2008 R2, Windows Vista
Internet Information Services (IIS) 7.0 has an output cache feature that caches dynamic content in memory (for example, output from your Microsoft® ASP.NET, classic Active Server Pages (ASP), PHP, or other dynamic pages). This helps to improve performance because the script used to generate dynamic output does not need to run for each request. The cache is able to vary the output that is cached, based on query string values and HTTP headers that are sent from the client to the server. The cache is also integrated with the HTTP.sys kernel mode driver to help improve performance speed.
IIS automatically caches static content (HTML pages, images, and style sheets) since these types of content do not change from request to request. IIS also detects changes in updated files and flushes the cache as needed.
The output from dynamic pages can now be cached in memory as well. However, not every dynamic page can use the output cache effectively. Pages that can be personalized, such as shopping cart or e-commerce transactions, cannot use the output cache because the dynamic output will probably not be requested repeatedly. Content output that results from a POST-type request to an HTML form also cannot be cached.
The output cache works well for pages that are semi-dynamic in nature, for example, when data is generated dynamically but is not likely to change from request to request based on the URL or the header information. Photo gallery applications, for instance, dynamically resize images for display on Web pages and can use the output cache to prevent the server from having to reprocess image resizing for each request.
Another example of an application that can use the output cache feature is a stock ticker application. For example, you have an application that allows a user to input a stock symbol, and the user submits the following symbol to the application: http://server/showStockPrice.asp?symbol=MSFT
In this case, the URL is always http://server/showStockPrice.asp, but the query string value (symbol=MSFT) tells the showStockPrice.asp page which data to include in the response, thereby varying the output of the page according to the query string value. This type of cache policy is called varyByQuerystring. IIS also supports another type of cache policy called varybyHeaders, which can vary the cache based on the HTTP headers sent from the client to the server. Caching the output of this data is useful because you can prevent the server from having to look up the current stock price for a particular symbol after each request.
Because you are caching dynamic data, keep in mind that the data will change. You will need to flush the cache, allowing new data to be retrieved and re-cached. In this example, the stock price is likely to change frequently. Even if the stock price changes every second, it may be useful to cache the data if the site receives hundreds of requests per second. Caching the data requires just one trip to the database to retrieve the stock price, instead of a hundred requests every second, saving the Web server and the database server from extra work.
IIS supports two types of invalidation schemes for dynamic content. The first is a simple time-out period, using the configuration property CacheForTimePeriod. The other way to invalidate the cache is for IIS to detect a change to the underlying resource. The configuration property for this is CacheUntilChange. Use this type of invalidation scheme only when you want to flush the cache after the underlying resource (in this case, showStockPrice.asp) changes.
You can configure the cache by using IIS Manager.
To configure output caching
Open IIS Manager, and then move to the server level.
Double-click Output Caching.
In the Actions pane, click Add.
In the Add Cache Rule dialog box, in the File name extension text box, type .php.
Select the User-mode caching checkbox.
In the Advanced Output Cache Rule Settings dialog box, select the Query string variable(s) checkbox, and then type your variables in the text box.
Click OK to close the Advanced Output Cache Rule Settings and Add Cache Rule dialog boxes.
You can also configure the caching feature in the local Web.config file, which is in the content directory. The following is a sample of the configuration needed for a showStockPrice.asp page with a varyByQueryString parameter of * and a time-out of 1 second (putting an asterisk as the value for this parameter caches all unique variations of query string parameters):
<add varybyquerystring="*"location="Any" duration="00:00:01" policy="CacheForTimePeriod" extension=".asp">
To cache this data in the kernel for even faster performance, change the policy attribute to kernelCachePolicy.
For more information about output caching, see IIS 7.0 Output Caching. This article provides troubleshooting tips, an end-to-end example of the output cache, and additional information about how to use this new feature in IIS 7.
Note: ASP.NET also has an output cache feature. The IIS output cache feature works in parallel with the ASP.NET cache.