刷新图像或清除缓存
有没有什么方法(服务器或客户端)强制浏览器从服务器提取文件(图像)的新版本。否则,相关图像会被缓存很长时间。我知道我可以附加一个随机数,例如,附加到图像的 URL,但在这种情况下这是不可接受的。我需要从完全相同 URL 刷新图像。
我正在做的事情:类似 YouTube 的门户网站,用户可以在其中上传视频。每个视频都有一个缩略图,显示在门户的各个页面上。用户可以随时更改缩略图(他可以从三个生成的缩略图中进行选择)。因此,当发生这种情况时(新图像覆盖“原始”图像),我不想刷新视频的缩略图,以便所有者(我不在乎其他用户是否看到旧缩略图)无论如何都会看到新缩略图显示缩略图的位置。
恐怕这是不可能的,但我在这里问只是为了确定一下。
更新:我在服务器端使用 nginx 和 PHP
Is there any way (server or client side) to force the browser to pull a new version of a file (image) from the server. The image in question is otherwise cached for a long time. I know I can append a random number, for instance, to the URL of the image but this is not acceptable in this situation. I need for the image to be refresh from the exact same URL.
What I'm doing: a YouTube like portal where users upload videos. Each video has a thumbnail which is shown on various pages on the portal. User can, at any time, change the thumbnail (he can select from three generated thumbnails). So when this happens (a new image overwrites the 'original' image), I wan't to refresh the video's thumbnail so that the owner (I don't care if other users see the old thumbnail) will see the new thumbnail no matter where the thumbnail is shown.
I'm afraid this can't be done but I'm asking here just to be sure.
update: I'm using nginx and PHP on the server side
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在缩略图上使用 ETAG。如果实际缩略图数据没有改变(即仍然具有相同的散列),这将阻止实际缩略图数据的传输。但是,您仍然会面对客户端 HTTP 请求来检查 ETAG 是否已更改(通常由 HTTP 304 应答)。
但结合相当短的新鲜度阈值(例如几分钟),您可以在缓存和新鲜度之间实现权衡同时仍然节省资源,如果您需要绝对的新鲜度,那么您可能必须坚持使用 ETAG。如果您创建一个聪明的哈希函数,您可以在前端负载均衡器(或至少接近它)上处理 ETAG 请求,这可能会相当好。 另
编辑:从我的其他评论中添加替代方案。
一种替代方案是,当资源按照另一个答案中的建议发生变化时,使用添加的请求参数强制重新获取(该架构的一个变体)。许多 Rails 应用程序都使用)是将最后一次更改的时间戳(或某种哈希值)作为参数附加到文件中,该参数仅在文件实际发生更改时才更改,或者上述方法之一,实际上,这是真正确保没有不必要的缓存验证请求,同时始终拥有最新资源的唯一方法。
You could use ETAGs on your thumbnails. This would prevent the transmission of the actual thumbnail data if it hasn't changed (i.e. still has the same hash). However, you would still face the clients HTTP requests to check if the ETAG has changed (normly to be answered by HTTP 304.
But combined with a rather short freshness threshold (say a couple of minutes), you could achieve tradeoff between caching and freshness while still conserving resources. If you need absolute freshness, you might have to stick to ETAGs though. If you create a clever hash function, you could handle the ETAG requests on your frontend loadbalancer (or at least near it) which could thus be rather cheap.
Edit: Add alternative from my other comment.
An alternative could be to use added request parameters to force a re-fetch when the resource changed as suggested in another answer. A variation of that schema (which is used by many Rails applications) is to append the timestamp of the last change (or some kind of hash) as a parameter to the file which only changes when the file actually does change. Something like this, or one of the above methods, is actually the only way to be really sure to not have unnecessary cache validation requests while at the same time having always the freshest resource.
在文件名末尾添加一个 get 参数,例如:
您还可以使用 rand() 参数在每次访问时刷新该图像。
在 PHP 中:
Add at the end of the filename a get parameter, such as:
You could also refresh that image each visit by using a rand() param.
In php: