How to enable Output Caching in IIS

Hello folks,
   In this short article we will talk about the Output Caching feature available with IIS servers. We will see what are the main aspects behind this technology and how to configure it to aid to our web applications functionality.
   Before going straight to the configuration part we have to talk about the concepts of caching, what caching actually means in IIS and when it’s recommended to use this feature. Output caching is a method of improving the web server’s performance by storing dynamic content into memory. Caching ca be enabled for classic ASP and ASP.NET, PHP and other dynamic content.
   By default, IIS will cache static content such as images or HTML files but, for dynamic content this feature has to be configured and customized manually. I’m saying that the caching feature can be customized because it’s not recommended with some dynamic objects and can even cause problems to your web application. Make sure that your web application requires output caching because it may cause instability to your system. This feature should be configured on dynamic content that is not changed with every request based on the header or URL. In IIS output caching is configured based on two variables: URL (varyByQuerystring) and header information (varybyHeaders).
   Because dynamic content changes it’s information frequently, it is necessary that resources are deleted before receiving updated information. This is why the cache memory must be flushed or invalidated. IIS presents two methods of invalidating information:
– a timeout period (CacheForTimePeriod)
– a change detection mechanism (CacheUntilChange)
   For a resource to became cached by the IIS server, it must be requested a number of times in a predefined period of time. IIS offers two parameters to configure the timing and number of requests: frequentHitTimePeriod and frequentHitThreshold
If a number of requests (frequentHitThreshold) are made for the same item in the configured period of time (frequentHitTimePeriod), the resource is cached to allow the IIS server to respond faster for future requests. When a resource has met these two conditions we say that it has become “worthy”.
There are two methods available when configuring Output Caching on your IIS server:
configure Output Caching using the IIS management console 
You can enable output caching for the whole IIS server or from each website individually. Open the IIS Manager console and navigate to your web application section and click on Output Caching:

IIS management console

Now click on the Add button from the right section to configure a new caching rule:

Output Caching
Windows Server supports two caching methods:
  • User-mode caching – uses a local cache stored in the IIS worker process
  • Kernel-mode caching – uses a cache stored in the Http.sys driver.
Note that even though the Kernel-mode caching is much faster than user-mode caching it does not support features that must run in user mode (authentication and authorization). Which caching method you use depends a lot on the application’s purpose and requirements.
For this example I’ve created a cache rule for .php files to use change notifications:
IIS cache rule
Note that you can press the Advanced button and enable the cache different version of file based on: query string variable and/or headers feature:
Cache rule
There are some options available in both User-mode and Kernel-mode caching:
  • Using file change notifications: an item will be removed from the cache once a newer version of the file is added in the web application.
  • At time intervals (hh:mm:ss): items will be removed from the cache once the period of time has elapsed.
  • Prevent all caching: this option prevents caching for the specified type of files
Once you’ve configured all these parameters, the application will be configured for caching.
configure Output Caching by modifying the config file of your web applications
Navigate to your web application physical location, open the web.config file and enter the following lines:

     <location path="mywebsite.php">     
             <add varybyquerystring="*"location="Any"
               duration="00:00:01" policy="CacheForTimePeriod"            
 (Source Microsoft's website)

The policy=”CacheForTimePeriod” parameter can be changed to kernelCachePolicy to enable Kernel-mode caching.

That’s about is for this article folks, hope you’ll find it interesting. Don’t forget to enjoy your day and stay tuned for the following articles from IT training day.

One thought on “How to enable Output Caching in IIS

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s