0

I'm dynamically generating a page by PHP. I am using the site's output string to generate a Etag and check that to send a 304 (not modified) when the site hasn't changed since the last request.

I am now trying to improve the caching of the pictures on my site as well. There are between 6 and 30 pictures (jpg, 70 - 200 KB) on each site. I want the user to reload the picture if its content has changed. I am thinking about doing this by adding a querystring to each picture's URL:

src="'.$files[$x].'?id='.md5_file($files[$x]).'"

Is this too complicated and generates too much workload on each request or is it worth it? As I said: I'm generating those md5-hashs for each image and then one md5-hash for the output string to use it as an Etag on each request.

These are the Response-Headers from my Images:

HTTP/1.1 304 Not Modified
Date: Mon, 10 Dec 2012 08:56:49 GMT
Server: Apache
Connection: keep-alive, Keep-Alive
Keep-Alive: timeout=1, max=99
ETag: "360f-4d02f5fcfc34f"
Expires: Mon, 07 Jan 2013 08:56:49 GMT
Cache-Control: max-age=2419200, must-revalidate

Thank you very much!

basbebe
  • 567
  • 1
  • 9
  • 25
  • Usually web server handles such staff, and correctly set headers 304 and 200. You don't really need to set any get variables to reload it correctly. – Константин Рекунов Dec 10 '12 at 08:53
  • Implement proper cache headers for each image as well. If they can change any time, do not send headers that prevent the browser from asking the server at all, but allow each of these images to be answered with a 304 status. Using your own strategy would mean that you have to make a checksum of every image every time - this prevents caching of your PHP page as well. – Sven Dec 10 '12 at 08:53

1 Answers1

1

Using time-based caching headers means the client will not ask the server at all whether a file was updated for a while.
Using Etag caching headers means the client will ask the server every time, but the data does not need to be transferred every time.

Both methods have their pros and cons. For assets which rarely ever change, you should use time-based cache headers, since this will take a lot of load from your server. To still have those update as soon as they are updated, adding a unique token to their URL is a good idea. For assets which often update, using Etags without time-based cache headers (or very short lived ones) is a good idea since it means they'll update as soon as they update while still taking load off the server. Typically you want to Etag your dynamic web pages and time-cache your image assets.

So, yes, adding an md5 hash to the URL of static time-cached assets is a good idea, if having the client download the updated version ASAP is a real concern. Make sure adding those hashes is not more overhead than you save though.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thanks for this answer! I decided to stick with creating Etags for my php-sites but give up on creating hash-queries for my images. They shall be cached by the browser for as long as determined in the Response-Header. Creating md5-sums for each and every image on each request seems to be overhead to me. – basbebe Dec 10 '12 at 13:20