/ Java

Grails, Tomcat6, Apache2 Caching

I've been struggling a while with getting caching to work. I'm using Tomcat6 and Apache2 and running an application built with Grails.

Apache2 Modules

I am using 2 modules. I initially tried mem_cache but is now using disk_cache because it is said to be of higher quality.

sudo a2enmod expires
sudo a2enmod disk_cache

Configuring mods

disk_cache config (disk_cache.conf).

<IfModule mod_disk_cache.c>
CacheRoot /var/cache/apache2/mod_disk_cache
CacheEnable disk /
CacheDirLevels 5
CacheDirLength 3
</IfModule>

The virtual host looks like this.

<VirtualHost *:80>
ServerName htmlunitgenerator.bjurr.se
ServerAlias www.htmlunitgenerator.bjurr.se
ProxyPass / http://htmlunitgenerator.bjurr.se:8080/
ProxyPassReverse / http://htmlunitgenerator.bjurr.se:8080/
CustomLog /var/log/apache2/htmlunitgenerator.bjurr.se.access.log combined

#Cache config

CacheStorePrivate On #Cache even if browser indicates it is a private site
CacheIgnoreCacheControl On
ExpiresActive On
ExpiresByType text/html "access plus 6 hours"
ExpiresByType text/txt "access plus 6 hours"
</VirtualHost>

What to cache

Only GET-requests are cached! So in Grails you need to do method="GET" like this:

<g:remoteLink action="show" id="1" method="get">Test 1</g:remoteLink>

If you want to cache other content types, here are some you may want to use:

ExpiresByType text/css "access plus 6 hours"
ExpiresByType application/x-javascript "access plus 6 hours"
ExpiresByType application/javascript "access plus 6 hours"
ExpiresByType text/javascript "access plus 6 hours"
ExpiresByType text/html "access plus 6 hours"
ExpiresByType text/plain "access plus 6 hours"
ExpiresByType text/xsd "access plus 6 hours"
ExpiresByType text/xsl "access plus 6 hours"
ExpiresByType text/xml "access plus 6 hours"
ExpiresByType application/java "access plus 6 hours"
ExpiresByType image/gif "access plus 6 hours"
ExpiresByType application/x-gzip "access plus 6 hours"
ExpiresByType image/x-icon "access plus 6 hours"
ExpiresByType image/jpeg "access plus 6 hours"
ExpiresByType application/pdf "access plus 6 hours"
ExpiresByType image/png "access plus 6 hours"