clear cache of one image

2019-07-24 14:57发布

Consider the minimal example: Using php I have a form that you enter text and it produces an image of the text. When I then change the text and update, I don't see the new image because I assume, it is being cached. Is there some way to automatically remove this one image file from the cache when I update it?

标签: php caching
4条回答
Juvenile、少年°
2楼-- · 2019-07-24 15:22

Add a query string to the image perhaps containing the julian time + a random number so for example your image URL becomes: //.../myimage.jpg?112233445566-954967254

查看更多
Rolldiameter
3楼-- · 2019-07-24 15:24

You can re validate the cache header("Cache-Control: no-cache, must-revalidate");

查看更多
Luminary・发光体
4楼-- · 2019-07-24 15:30

This is frequently handled by adding a random string or timestamp to the query.

i.e. <img src="/images/image.jpg?timestamp=1357571065" />

查看更多
时光不老,我们不散
5楼-- · 2019-07-24 15:32

The typical solution is what ceejazoz gave in this answer: an additional timestamp added as a request parameter. That way the url is different each time, so no cache or proxy will deliver a cached version.

However although that works it is an ugly workaround.

The clean solution is to specify headers when delivering the image. Those headers take care that the image is not cached. That is what headers are there for: defining how resources are meant to be used. The drawback: the out-of-the-box configuration of todays http servers used to deliver static images does not offer to specify such headers. Because in 99,99% of all cases it makes no sense. So you will have to write an own mechanism. Not really difficult, but effort nonetheless. Using the above workaround certainly is easier and less hassle.

And to give a precise answer to your actual question: Cleaning 'the' cache from a single cached object usually is not possible. Though that actually depends on what cache you are talking about. If it is just the browsers cache whilst you are developing (testing), then just make a 'deep reload' (something like CTRL-SHIFT-R or CTRL-F5, depending on your browser). But this clears all cached objects of the current page. There is no easy way to clear a server side cache or even a proxy inbetween.

查看更多
登录 后发表回答