As some of you perhaps have noticed earlier, I’m using gzip compression through Apache’s mod_deflate on my mobile web application. Lately I’ve been working on a new service that load it’s content from an external XML request, and obviously this made me look for caching possibilities, either for the LWP-request behind the scenes or some sort of front end, varnish-style caching.
Since my application was developed in Perl and LWP-Simple, the caching options was not to many. Using Varnish caches was little to extravagant for this solution, so my eyes focused on Apaches own mod_cache module.
Mod_cache can handle both memory- and disk caching.
With simplicity in mind, I started to implement the memory solution, mod_mem_cache. The amount of different pages needed simultaneous in the cache was not bigger than it would be achieved by allocating roughly 10 MB for this purpose.
The setup was quite easy (read about it here), but it has one serious flaw – it does not support caching of compressed files. I’m not sure why, I think it has something to do with the order Apache handles these modules.
Since I don’t consider disabling compression an option (see why), the next module to try out was the mod_disk_cache. This module caches all text files in a specified directory, valid for the time set in the file’s header expire time. Here is the setup I ended up using:
mod_disk_cache config explained
CacheRootneeds to be writeable for your Apache user.
- In order to ignore all requests from browsers to not load cached files, I set
CacheIgnoreHeaderslists all header entities that you want to remove from the cached file to be able to serve the same cached version to many user agents (browser types). The cached file’s header information contain these parameters if you don’t list them here, and one difference in those headers results in a new cached version of the same file.
CacheDisablespecifies directories not intended for caching. All flat files and images are practically served the same way from where they are, this cache function are mainly for dynamically generated content. Like my Perl XML-parsing pages
For further information, please read Apache’s highly informative documentation on mod_caching.
I am now happily serving rather sluggish Perl-parsing-scripts blazingly fast from this simple caching mechanism!
Fishing at Lake Høvringen, Rondane, Norway
I run a Norwegian quiz site for mobile phones called MobilQuiz. This service is a simple Trivial Persuit-like Quiz where you are presented a question on your mobile screen with a simple link to the answer. Really simple and fool prof.
I proxy my Rails-application through Apache2 and that’s where this gzip-compression-trick called mod_deflate applies. You have to compile your Apache2-installation with
--enable-deflate. When that is done, you could just insert this snippet inside your virtual domain space in
<Location /> SetOutputFilter DEFLATE SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ \ no-gzip dont-vary SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ \ no-gzip dont-vary </Location>
Result in bandwidth usage
After applying mod_deflate the front page only weights 17 KB
That’s a 48,5% reduction. Not bad, huh?
Webdirect.no er et av de mer udefinerte nettstedene der ute som ikke passer inn i noen sjanger. Innholdet varierer fra reisebrev i Sør-Amerika til hvordan du setter opp og bruker en SVN-repository. Ikke noe å si på mangfoldet, med andre ord.
Den mest populære artikkelen på dette nettstedet er "Lær deg HTML på 21 minutter" som jeg skrev i 1998 og som tydeligvis fortsatt tåler tidens tann.