Home > Articles > Web Services

  • Print
  • + Share This

When Caching Is Bad

The HTTP caching optimizations described in this article might not be relevant to your application. eCommerce applications, shopping carts, forums (bulletin boards), etc. are notable applications that always have to present up-to-date information to their clients. But even in these environments, there’s always room for improvement; for example, product descriptions accessed from a shopping cart application usually don’t change on a daily basis.

The mechanisms that disable browser caching are very well known because a large number of developers have already stumbled across this problem:

  • To support HTTP/1.0 clients, set the Expires header to a date in the past (for example, Expires: Thu, 01 Dec 1994 16:00:00 GMT) and a use a Pragma: no-cache header.
  • Add a Cache-control: no-cache header for the benefit of HTTP/1.1-compliant proxies and clients.

To control the browser caching behavior, your application should set the HTTP headers in the previous list on every response. Each programming environment uses a different approach to set these headers. The code in Listing 1 shows how to disable browser and proxy caching in ASP; Listing 2 shows the same for ASP.NET, and the code for PHP appears in Listing 3. The same methods will be used in the next sections of this article to set other caching-related headers.

Listing 1 Disable browser caching in ASP.

Response.CacheControl = "no-cache"
Response.ExpiresAbsolute = DateSerial(1994,12,1)
Response.AddHeader "Pragma","no-cache"

Listing 2 Disable browser caching in ASP.NET.

<%@ OutputCache Location="None" VaryByParam="None" %>


Response.ExpiresAbsolute = DateTime.Now.AddDays(-1)

Listing 3 Disable browser caching in PHP.

header(’Expires: Thu, 01 Dec 1994 16:00:00 GMT’);
header(’Cache-Control: no-cache’);
header(’Cache-Control: post-check=0, pre-check=0’, FALSE);
header(’Pragma: no-cache’);

If you work in a programming environment where you can’t control the HTTP headers, you might be able to achieve the same results by using the META headers in the HTML output (see Listing 4). Some web servers parse the HTML output generated by their scripting engines and transfer the values from the META headers into the HTTP headers; in such cases, this approach makes sense. Otherwise, the META headers are largely ignored by proxies and browsers.

Listing 4 HTML META headers.

<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="Thu, 01 Dec 1994 16:00:00 GMT">
<meta http-equiv="Pragma" content="no-cache">
  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.