May 03, 2003

Caching Dynamic Content

A common use case in building a data-capture application is to select something from a decent sized list.

You don't want to have to send the whole list down to the client, but do want to update it when the data actually does change.

One answer is to split the data out into a seperate file, and then write it in using DHTML and JScript

Here is what I came up with (tested under IE only :-))...

I started with looking around for information on Caching. I found this Tutorial, and a bunch of stuff on preventing caching (JGuru, and JDC, and more), suprisingly I ended up at Microsoft which had some of the most practical Performance Tips for the web that I could find.

I ended up putting the following snippet in the jsp that contains the data:


response.setDateHeader("Last-Modified", lastRefDataUpdateTime);
//check the data every 15 minutes after loading, force
//a reload if a day has passed between loads
//(session timeout makes this a bit redundant).
response.setHeader("Cache-Control", "post-check=900,pre-check=86400");

Another cool feature mentioned in the MSDN article is the use of the DEFER attribute on Script tags. A bit of a google found this.

So now on the login page I pull down the data asynchronously when the user first logs in using:


<script src="datafile.js.jsp" DEFER></script>

Ending up with a quick little widget.

Posted by rob@rojotek at May 3, 2003 10:12 AM
Comments