clear cache of one image

心已入冬 提交于 2020-01-04 14:09:02

问题


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?


回答1:


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

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




回答2:


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.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/14197493/clear-cache-of-one-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!