Sunday, August 14, 2005

DynamicPageCache filter using EHCache

There are times when you require a dynamically generated JSP page to be cached on the client side for a certain amount of time. For example if you are producing an XML feed or a page that displays information that need only be recent rather than real time. With an appropriate caching mechanism this can significantly reduce the amount of processing that the server has to do, meaning that the same application instance can serve a much larger audience.

Sometimes it is not possible to know when the Last-modified date for a dynamically generated page should be, for example I wrote one application that converted an IMAP folder into an RSS feed and couldn’t find a satisfactory simple way of obtaining a last modified date from the IMAP server. Sometimes an arbitrary caching period is the best choice (e.g. every ten minutes).

I previously extolled the virtues of the HTTP conditional get mechanism and how projects like ROME FeedFetcher were reducing the load on content providers and at the same time improving local performance. Web browsers too have integral support for the Conditional Get.

So the question is how to provide this Conditional Get information for your content consuming client? Well, if you’ve read my recent entries you won’t be surprised to hear me suggest a servlet filter is the answer.

Feed aggregator application example

In the first version of my feed aggregator application I originally had a page that would make a request to a number of RSS feeds on each request. This was obviously very slow and not scalable, so my solution was to introduce a feed cache. Using ROME, ROME FeedFetcher, EHCache and Quartz scheduler I constructed a background process that would periodically poll each feed for new entries and cache the results, this was far more efficient. However, I still had a page that was dynamically querying my feed cache each time it was called, even though the page would only change once every 10 minutes. What I needed was a mechanism to instruct the web browser to cache this page for a certain duration, so that the page behaved as static content that was updated every 10 minutes. Once again I turned to EHCache as my chosen solution. EHCache is a highly performant cache solution, it is the default cache for Hibernate. A subproject of EHCache is something called EHCache constructs, this includes a SimplePageCachingFilter servlet filter. Unfortunately I couldn’t get the SimplePageCachingFilter filter to work properly in Internet Explorer, so I made a couple of extensions and I now have working Dynamic Generated Page Cache (which incidentally also GZips the page for browsers supporting that kind of thing).