Using Caching to Improve Performance

From Hostek.com Wiki
Jump to: navigation, search

This article will show a few ways that you can use caching to improve page load times for your website.

Using Cache-Control Headers to Improve Page Load Performance

The Cache-Control header of an HTTP response tells the client web browser when and how long it should cache responses. This can be used for resources that do not often change to prevent the browser from repeatedly requesting the same resources on every page.

The cache-control header that caches a response for 60 seconds appears as follows in the headers of a request:

Cache-Control: max-age=60

You can also indicate that the page should only be cached by the end-user and not by any proxies (I.E. It contains content only intended for a single user) by adding the "private" directive:

Cache-Control: private, max-age=60

Alternatively, if you want to ensure a page is cached at all proxies in addition to the end user, you can add the "public" directive:

Cache-Control: public, max-age=60

Static Content

Static content is the easiest to apply cache-control to because by its very nature, it does not change frequently. Static content includes javascript, css, images, and plain html pages. In-general, These items can be safely cached by the client for the duration of their visit. I would recommend setting the cache duration to be about the length that the average visitor stays on the site.

The control panel allows you to specify the static cache duration on a per-folder basis by selecting a domain and going to 'IIS Settings' -> 'Caching'.

Dynamic Content

Dynamic content is often more difficult to cache due to its changing nature. However, there are still often pages that are dynamically generated but do not change very often. And these can be cached for a reasonable duration.

Packaged Applications

For pre-written applications, altering the cache-control settings for dynamic pages may or may not be available through the application's configuration. Additionally, some applications, such as WordPress and Joomla, require plugins to add caching functionality.


Custom Applications

For applications that you have written yourself, you can specify the cache-control setting for a particular page by setting the cache-control header directly from the code. Below are some examples of how to do this in several different programming languages:

ColdFusion

Cache for 1 hour:

<cfheader
    name="cache-control"
    value="max-age=3600"
    />
PHP

Cache for 5 minutes:

header('cache-control: max-age=300');
C#.Net

Cache for 1 day:

Response.AddHeader("cache-control", "max-age=86400");
VB.Net

Cache for 30 minutes:

Response.AddHeader("cache-control", "max-age=1800");
Classic ASP

Cache for 1 hour:

Response.AddHeader "cache-control","max-age=3600"

Using Response Caching to Improve Page Load Performance

Response caching allows the server to remember the response that was given for a particular page and use that response to reply to future requests for that page. When using the response cache, the initial request for the page will take longer since the application must execute the code to generate the page. However, subsequent requests will be almost instantly served from the response cache. This can be helpful for sites that are dynamically built by a CMS-engine, but the content of the site does not change very often.

The response caching must be configured within the folders "web.config" IIS configuration file. If you have an existing configuration file, only the "caching" section below should be added. If you do not currently have a web.config file in the folder, the entire example should be used:

<configuration>
  <system.webServer>
    <caching enabled="true" enableKernelCache="true">
      <profiles>
        <add extension=".asp" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
      </profiles>
    </caching>
  </system.webServer>
</configuration>

The important part above is the add element inside of the profiles element. You must add one more add elements to define what should be cached and for how long. Below are the available properties for the add element:

  • Extension(required): The file extension for which you would like requests to be cached.
  • Duration: How long to cache files when the CacheForTimePeriod option is used with the Policy or kernelCachePolicy parameters. The default is 30 seconds. The format is hh:mm:ss.
  • Policy: How long to cache files. Possible options are:
    • DontCache: No caching is enabled. This is the default.
    • CacheUntilChange: Cache until the source file is modified. This will only work when the URL maps to an actual file.
    • CacheForTimePeriod: Cache for the duration specified in the Duration property.
  • kernelCachePolicy: Same options as Policy property.
  • varyByHeaders: A semi-colon delimited list of headers that should cause separate content to be cached for each combination.
  • varyByQueryString: A semi-colon delimited list of querystring parameters that should cause separate content to be cached for each combination. You can also specify * if you want any changes in the querystring to cause separate content to be cached.


[[Category:Windows]