Back to the Konsensys

Back to Tips
Disabling Page Caching
by
George Van Treeck

Being able to completely turn off page caching is important for some web applications that use HTTP POST (particulary for database applications) or AJAX to replace contents of a web page. A frequent recommendation is is to set the following META tags:

<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Last-Modified" content="0" />
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate, max-age=0, post-check=0, pre-check=0"/>

But, I have found the above to be insufficient because some web servers (e.g., Tomcat) completely ignores those tags. I tried an experiment and found that the following worked with the Tomcat web server:

response.setDateHeader ("Expires", -1);
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache, " +
    "no-store,must-revalidate, max-age=0, " +
    "post-check=0, pre-check=0");

If the above does not work then you have a couple of other things to look at: 1) You might have to delve into the XML hell of Tomcat, Weblogic, etc. configuration files to adjust your Tomcat "valves" -- that's right valves (whatever they are)... 2) If you are using Apache or IIS as a proxy to redirect requests to your Java web server, then you might have to adjust the caching policy on those web servers as well.

On Apache you can edit the httpd.conf file to change the CacheEnable to CachceDisable. Or you can adjust some of the sizes to reduce caching. On CacheDisable/Enable you can specify a URL. A sample httpd.conf snippet is shown below:

Sample httpd.conf

#
# Sample Cache Configuration
#
LoadModule cache_module modules/mod_cache.so

<IfModule mod_cache.c>
  #LoadModule disk_cache_module modules/mod_disk_cache.so
  <IfModule mod_disk_cache.c>
    CacheRoot c:/cacheroot
    CacheSize 256
    CacheDisable disk /
    CacheDirLevels 5
    CacheDirLength 3
  </IfModule>

  LoadModule mem_cache_module modules/mod_mem_cache.so
  <IfModule mod_mem_cache.c>
    CacheDisable mem /
    MCacheSize 4096
    MCacheMaxObjectCount 100
    MCacheMinObjectSize 1
    MCacheMaxObjectSize 2048
  </IfModule>
</IfModule>

On IIS use the following Microsoft knowledge base article to Disable the IIS caching.


Copyright (C) 2010 George Van Treeck. All rights reserved.