0

I use image caching by reading a BLOB and then streaming using BufferedOutputStream. I set the following headers:

res.setHeader("Last-Modified", modDate);
res.setHeader("Expires", expTime.toString());
res.setHeader("Cache-Control", "public, max-age=31536000");

The caching works always except for the refresh button. If we load a page using menu click the images are loading from cache but if we click refresh button of browser it comes from server. Any idea?

coder247
  • 2,913
  • 19
  • 50
  • 70
  • 2
    the server should check the `If-Modified-Since` request header and send a 304 if appropriate; what server software are you using? – Christoph Nov 04 '10 at 13:08

2 Answers2

1

Different browsers deal with the refresh button differently.

You might want to check out this answer. Also, look at the headers to the request that is being sent to the server. See if the browser is sending the If-modified-since header.

Actually, you might check all of the answers to that question. For example, if you are using Firefox and initially did a Ctrl-(refresh) then Firefox won't cache it again until the browser is closed.

Community
  • 1
  • 1
NotMe
  • 87,343
  • 27
  • 171
  • 245
  • I tried with firebug. And saw that when I press press the menu button of the application there is no loading of images. And If I press refresh all the images are loading..I can see the If-modified-since in the request. – coder247 Nov 08 '10 at 16:26
0

Fixed the issue like this.

get the date from req.getHeader("If-Modified-Since"); And compare the if-modified-since date and the date from database. If they are same then I return a status of 304. Then it won't be modified.

String modifiedSince = req.getHeader("If-Modified-Since");
    if(formattedLastAccess.equalsIgnoreCase(modifiedSince)){
        res.setStatus(304);
    }else {  // stream file }
coder247
  • 2,913
  • 19
  • 50
  • 70