in reply to Slightly OT: Stop caching images

Not entirely sure I understand your requirements... but why do you need to disable caching at all? As long as each image is corresponding to a unique URL, things should work as intended. Let's assume you have three images. So, instead of having something like

----- ---------- --------- ----------- ----- | <-- | | left.jpg | | mid.jpg | | right.jpg | | --> | ----- ---------- --------- ----------- -----

and then changing the image content that's delivered as "left.jpg" etc. when the --> button is clicked, it's proably easier to just update/rotate the URLs. For example, if you initially have

----- ---------- --------- ----------- ----- | <-- | | img1.jpg | | img2.jpg | | img3.jpg | | --> | ----- ---------- --------- ----------- -----

then, after rotation, you'd output

----- ---------- --------- ----------- ----- | <-- | | img2.jpg | | img3.jpg | | img4.jpg | | --> | ----- ---------- --------- ----------- -----

i.e. "img2.jpg" remains the same image (so the browser can reload it from its cache). It'll just be displayed in a different place.

Even if your requirements are not quite as simple as that, it should still be possible to always make sure that, if the content of the image changes, it gets a new URL (so cache lookup will surely fail). At least, that approach should always work as a last resort.   But maybe I've completely misunderstood what you want to do... :)

Having said that, if you still want to go the other route trying to disable client side caching, you might want to look into using mod_headers, which lets you set/add specific custom headers for certain URLs, even if they're static, like images.

BTW, there's a neat caching test page (AJAX-based), to give you an idea of which caching features actually do work in current browsers... (things have gotten better, generally, but there are still a number of more sophisticated, unresolved issues)

Replies are listed 'Best First'.
Re^2: Slightly OT: Stop caching images
by astroboy (Chaplain) on Mar 27, 2007 at 11:47 UTC

    hmm ... I probably didn't explain myself very well. I am rotating an image (using Image::Magick's Rotate method) not changing the image order. So nothing about the web page has changed - but the image on the disk itself has rotated. I can't change the name of the image to prevent caching (other than to add redundant query info as above) as it's used throughout the system and by more than one module.

    mod_headers could be useful, though. Thanks

      Aside from cache-related pragma's and standard content-expiration, you may wish to check out the 'ETag' HTTP header, which seems to be favoured by Internet Explorer for this kind of thing.

      Using ETag, you uniquely idenify each 'version' of a resource, so if you want it to refresh, you send a different ID.

      I find it better than appending ?12309821 to a request because:
      • my logs arent filled with random numbers
      • 12309821 adds nothing to the document, semantically speaking.
      • the ETag header is generally left unmolested by internet proxies.

      -David

        I wasn't aware of the ETag (it sounds useful), but I'm not sure how to use it in practice.

        Image requests are handled by Apache not my code. Without using something like mod_perl, how would I intercept the request and set an appropriate ETag? Where would I get the version number from?