Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top