Use Web Caching to Make Your Web Site Faster | 2 | WebReference

Use Web Caching to Make Your Web Site Faster | 2


Use Web Caching to Make Your Web Site Faster [con't]

Expires HTTP Header

The Expires HTTP header tells the caches how long the document should be considered fresh. When the Expires date passes, then the cache is considered stale and a fresh version is fetched from the server. There are several ways to set the Expires response. The most common ones are setting it to an absolute time, setting it based on the last time the client received the resource, and setting it based on based on the last time the resource was modified.

For static images, like the header and navigation buttons, you should set a long expiry time, as they don't change frequently. Your pages will load significantly faster. The value for Expires must be a valid HTTP date. If it is invalid, it is considered as a past date and the resource are fetched from the server every time.

Here is an example of the Expires response:

Expires: Sun, 10 May 2010 14:19:41 GMT

The only problem with Expires is that it involves the date, time, and two clocks. The time is in GMT, but the clocks on the web server and the client need to be in sync. Otherwise, the expected result may not be achieved.

Cache-Control HTTP Header

HTTP 1.1 introduced this new header response to overcome the shortcomings of Expires and give the developer more control over caching. Here is a list of the various values Cache-Control can take:

  • max-age=[seconds] – Like Expires, this specifies how long the resource is considered fresh.
  • s-max-age=[seconds] – While similar to max-age, this applies only to shared caches (like gateways and proxies).
  • no-cache – Use this when you want the resource to be fetched every time from the web server.
  • no-store – This specifies that the resource must not be saved under any circumstance.
  • must-revalidate – You can set caching rules on the web server that sometimes override the ones that you specify in the header. Using must-revalidate, you can tell the cache to strictly follow the rules you set.
  • proxy-revalidate – This is similar to must-revalidate, but only applies to proxy caches.
  • public – This makes authenticated responses cacheable. By default, HTTP authenticated resources are not cacheable on shared servers.

Here is an example of the Cache-Control response:

Cache-Control: max-age=4800, proxy-revalidate, public


With this brief overview of how caching behaves, you now can make it work for you. With the introduction of the Cache-Control header, you don't need to use the others as it covers everything you need to control the caches. Here are a few other tips to improve the speed of your site using caching:

  • Be consistent with URLs. – Don't serve the same content to different users on different pages. You won't be using the cached data in this case.
  • Set a high max-age for static resources such as images and downloadable files.
  • Minimize your use of SSL.
  • Don't change files unnecessarily.
  • Use a common set of images across the entire site.