简体   繁体   English

缓存的图片问题

[英]Cached image problem

Right, my problem is that I need to replace an image that appears on every page of a site, which is fine, the problem is I want every user to see the new image and not a cached version of the old one. 是的,我的问题是我需要替换出现在站点每一页上的图像,这很好,问题是我希望每个用户都可以看到新图像,而不是旧图像的缓存版本。 This is made especially difficult because I can't make any code changes (far too many places to do it for it to be viable) so I need to replace the image on the server with a new one of the same name. 这变得特别困难,因为我无法进行任何代码更改(要想使它可行,要在太多地方进行更改),因此我需要用相同名称的新服务器替换服务器上的映像。 Not only that but the pages are quite heavy so I don't want to force the entire page to reload completely every time the page is requested. 不仅如此,页面也很沉重,所以我不想每次请求页面时都强制整个页面完全重新加载。

Im thinking the best way would be to do something in IIS that will force the new image onto anybody who hasn't got it already. 我认为最好的方法是在IIS中执行某些操作,以将新映像强加给尚未拥有该映像的任何人。 The Last-Modified or Expires HTTP headers where my first stop but I can find a way to make them work against a single item rather than the entire page. Last-Modified或Expires HTTP标头是我的第一站,但我可以找到一种方法使它们适用于单个项目而不是整个页面。

Any help/ideas would be fantastic! 任何帮助/想法都太棒了!

Thanks, Tom. 谢谢,汤姆。

I had the same problem and found this solution: 我遇到了同样的问题,并找到了以下解决方案:

$randy = md5( uniqid (rand(), 1) );

then make your image like: 然后使您的图像像:

$profile_img = "<img src='$imageLocation2?$randy' border='0'/>";

echo $profile_img;

This puts a random string on the end of the jpg file like ?3984289040823 so the source will look something like this: 这会在jpg文件的末尾放置一个随机字符串,例如?3984289040823,因此源代码将如下所示:

<img src="example.jpg?3984289040823" />

Now each time you load page browser thinks it's a new image and loads it. 现在,每次加载页面时,浏览器都会认为这是一个新图像并进行加载。

I believe the only way to do it is to play with HTTP headers. 我相信唯一的方法就是使用HTTP标头。

I see two options: 我看到两个选择:

  1. Add headers to the file. 将标头添加到文件。 I don't know IIS well, but I think they have such options; 我对IIS不太了解,但是我认为他们有这样的选择。
  2. Replace your image with the script (php or C#), add headers manually in the code and send the image 用脚本(php或C#)替换图像,在代码中手动添加标题并发送图像

IIS ought to be setting the last modified header and/or the etag based on the file itself, so if it's still caching it's possible that the browser is misbehaving, in which case you would be looking to set the No-Cache pragma. IIS 应该根据文件本身来设置最后修改的标头和/或etag,因此,如果仍在缓存,则浏览器的行为可能不正确,在这种情况下,您将希望设置No-Cache编译指示。 References: Microsoft KB reference , Section 14.9 of the HTTP1.1 spec 参考: Microsoft KB参考HTTP1.1规范的14.9节

Elalfer's solution 2 would probably achieve a No-Cache pragma as a by-product. Elalfer的解决方案2可能会实现副产品No-Cache实用性。

The HTTP headers always apply to only one item. HTTP标头始终仅适用于一项。 In the case of those set on the page itself they apply only to the page and not to the linked resources. 对于在页面本身上设置的内容,它们仅适用于页面,而不适用于链接的资源。

Browsers and HTTP caches in the wild will have cached copies of this image already. 野外的浏览器和HTTP缓存将已经缓存了该映像的副本。 There is no way to force them to reload the image without changing the URL of the image (which you've stated you cannot do). 没有办法强迫他们在不更改图像URL的情况下重新加载图像(您已声明无法这样做)。

If this is the only time you are changing this image, or you expect such changes to be infrequent, then just change the image and eventually the web will cache the new image. 如果这是您唯一一次更改此图像,或者您希望这种更改很少发生,则只需更改该图像,最终网络将缓存新图像。 Browsers that check for updated versions will be notified by the web server that the timestamp has changed. Web服务器将通知检查更新版本的浏览器时间戳已更改。

For making a more permanent change, you need to investigate how IIS is setting headers for your static files. 为了进行更永久的更改,您需要调查IIS如何为静态文件设置标头。 I suspect that it sets all the headers uniformly for all files, but there may be a way to override the headers for a specific file. 我怀疑它为所有文件统一设置了所有标头,但是可能有一种方法可以覆盖特定文件的标头。 If not, you should be able to wrap the request for that url in a script which sets the headers before delivering the content. 如果没有,您应该能够在传递内容之前将对该URL的请求包装在设置标题的脚本中。 What you want to do, in this case, is set the Expires or max-age header so that browsers know how long they should keep this item in the cache. 在这种情况下,您想要做的就是设置Expires或max-age标头,以便浏览器知道将此项保留在缓存中的时间。 Note that these headers will only apply to future requests for this file. 请注意,这些标头仅适用于以后对此文件的请求。

Putting these headers in the pages themselves will not affect requests for the file. 将这些标题放在页面本身中不会影响对该文件的请求。 Be careful when adding cache-control headers as your site may not function properly if the wrong things are being cached too aggressively; 添加缓存控制标头时要小心,因为如果错误地过分缓存了错误的内容,则您的站点可能无法正常运行。 alternatively your bandwidth may spike as things are not being cached which should be. 否则,您的带宽可能会突然增加,因为应该缓存的内容没有被缓存。

For future consideration you should make it possible to change the paths of static resources when the content changes. 为了将来考虑,您应该可以在内容更改时更改静态资源的路径。 This makes it clear to a browser that the file is new because there will be nothing with that url in the path. 这使浏览器清楚地知道该文件是新文件,因为路径中的URL不会包含任何内容。 For example: 例如:

www.yoursite.com/images/v1/logo.png

becomes

www.yoursite.com/images/v2/logo.png

once logo.png changes to v2. 一旦logo.png更改为v2。 Then, in the pages where logo.png is referenced, you have code to automatically write the correct url in the page. 然后,在引用了logo.png的页面中,您有代码可以自动在页面中写入正确的url。 This way each revision of the file has a unique name and thus never conflicts with any previously cached versions. 这样,文件的每个修订版都具有唯一的名称,因此不会与任何先前缓存的版本冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM