I’ve a jsp page which loads many images. I’d like to cache the images for faster loading.
I’ll explain my idea, please correct it if it’s wrong. I’m calling the picture loading servlet for each image and return as a BLOB. My idea is to add a modified date with the image and the other values like Last-Modified, expires, Cache-control and max age. And thereby make the browser understand if the image changes.
But how can i append a modified date to a BLOB? Or is there some better ideas to make them cachable?
Thanks…
This is a Good ThingTM.
For that you actually need the
ETag,Last-Modifiedand optionally alsoExpiresheader. With theETagheader both the server and client can identify the unique file. You can if necessary use under each the database key for this. With theLast-Modifiedheader header both the server and client knows if they both have the same version of the file. With theExpiresheader you can instruct the client when to re-request the file the firstnext time (thus, when the date as specified inExpireshas been expired).The
Cache-Controlheader is not so relevant here as you just want to allow caching and the average client already does that by default.For more information and a servlet example, you may find this article useful and maybe also this article for the case you’d be interested in tuning performance of a JSP/Servlet webapplication.
Just add one more column to the database table in question which represents the insertion date. In most DB’s you can just use
now()function for this or even create it as an auto-trigger so that it get set automatically on every insert/update.