0

I implemented a system to edit images displayed on a website.

When an image is edited, I want to display its last version on all pages of the site (where it's called), at least for the user who edited the image.

Now I'm searching a better way to clear the cache for a specific image than adding the timestamp to the end :

<img src="myimage.jpg?01215654" alt="" />

Why ?

It supposes to always add the timestamp at the end of the image path, with a session system for example.

But when the session expires (for example, user leaves the site, and go back few hours after), the real path of the image is displayed :

<img src="myimage.jpg" alt="" />

And the navigator randomly displays the old image, or the last version... Because Apache cached :

  • myimage.jpg as the old image
  • myimage.jpg?01215654 as the new version because no one hit it before (was loaded as a new media)

Limitations

  • I can't change the path of an edited image, for example "myimage.jpg" to "myimage-v2.jpg", which could solve the problem...
  • Images are not served from a controller (not served from a PHP script), they are direclty loaded from their path on the server.
  • I don't want to use a session system that add timestamp or version string at the end of an image path with JS.

What I want

I want to say that the cache of "myimage.jpg" has expired, and so to display the last version, and then cache it normally, until the next edition. So I would like to load the image with "200 status", and not "304 modified" after an edit.

Note : when an image is edited, I reload it with JS (JQuery) in a hidden div, if you now a way to change cache header ?

So what I want is to clear the cache header of a specific image that is edited, taking into account the limitations of my project, so if possible by using cache directives without serving the image from a controller, is it possible ?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Sybio
  • 8,565
  • 3
  • 44
  • 53

1 Answers1

0

This whole question is very confused.

Apache cache? ...No, the cache is in your browser, not in Apache.

Sessions adding timestamps to image URLs? ...No, the querystring with the timestamp must be added to the HTML using server-side script (such as PHP, for example). It has nothing whatsoever to do with browser sessions. (And actually it's generally much better to use the file last-modified time rather than the current timestamp, BTW).

There are a number of ways to achieve the effect you want, each with its own benefits and drawbacks depending on your situation. You may want to read my answer here.

Community
  • 1
  • 1
Doin
  • 7,545
  • 4
  • 35
  • 37