Reputation: 2943
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?
Upvotes: 0
Views: 459
Reputation: 2943
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 }
Upvotes: 0
Reputation: 88044
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.
Upvotes: 1