简体   繁体   English

是否可以知道文件是否在用户的浏览器缓存中?

[英]Is it possible to know if a file is inside the user's browser cache?

I am doing something like this: if an image is cached on user's computer and its timestamp is the same as the one on the server, then display the cached version; 我正在做这样的事情:如果图像缓存在用户的计算机上,并且其时间戳与服务器上的时间戳相同,则显示缓存版本; otherwise, do NOT load the image from the server. 否则,请勿从服务器加载图像。

I guess maybe JavaScript can do this, so I tagged this post as javascript. 我想也许JavaScript可以做到这一点,所以我将这篇文章标记为javascript。 If it is improper, please help me to re-tag it. 如果不合适,请帮我重新标记。

Edit: Here I give more details about what I am going to implement. 编辑:在这里,我提供了有关我将要实施的内容的更多详细信息。 I am working on something like a web-based file explorer, where thumbnails are loaded only if the user click on a "view thumbnail" button beside each image. 我正在开发类似于基于Web的文件浏览器,只有当用户单击每个图像旁边的“视图缩略图”按钮时才会加载缩略图。 If a thumbnail is already cached, use the cached version; 如果已经缓存了缩略图,请使用缓存版本; otherwise, show a generic image icon. 否则,显示通用图像图标。

This seems a bit odd to me. 这对我来说有点奇怪。

You describe your functionality as showing a list of files and giving the user the option to click "view thumbnail". 您将功能描述为显示文件列表,并为用户提供单击“查看缩略图”的选项。 This means it will only attempt to load the image from the server if the user specifically requests the image - so you have already stopped the potential for a mass-load of a large number of images. 这意味着如果用户专门请求图像,它将仅尝试从服务器加载图像 - 因此您已经停止了大量图像的大量加载的可能性。

So if the image isn't in my browser cache, which on my first visit none will be, then I will get the generic image icon. 因此,如果图像不在我的浏览器缓存中,在我第一次访问时将没有,那么我将获得通用图像图标。 On my next visit, the image still won't be in my cache so I will get the generic image icon again. 在我下次访问时,图像仍然不在我的缓存中,因此我将再次获得通用图像图标。 So how will I ever see anything except the generic image icon? 那么除了通用图片图标外,我怎么会看到任何东西?

Now onto a solution, which I admit doesn't meet your requirements, but this is a typical implementation for this type of software. 现在进入一个我承认不符合您要求的解决方案,但这是此类软件的典型实现。

  1. Show the files with a "show thumbnail" option 使用“show thumbnail”选项显示文件
  2. When the "show thumbnail" option is selected, load the thumbnail from the server (note that quite often a script will serve a resized image, which may also be stored on the server to avoid having to process the resize again for a certain amount of time) 当选择“显示缩略图”选项时,从服务器加载缩略图(请注意,脚本通常会提供已调整大小的图像,该图像也可能存储在服务器上,以避免必须再次处理调整大小的一定数量的时间)
  3. The browser will automatically cache the image and prevent further server requests for a time 浏览器将自动缓存图像并暂时阻止进一步的服务器请求

My RSS reader, for feeds that have a lot of posts, loads a full page of posts. 对于包含大量帖子的Feed,我的RSS阅读器会加载整页帖子。 The number of entries is dependent on the size of the actual window. 条目数取决于实际窗口的大小。 When I scroll to the bottom of the list, if there are more posts it will load another page. 当我滚动到列表的底部时,如果有更多帖子,它将加载另一页。

Would that be applicable to your file explorer? 这适用于您的文件浏览器吗? It would mean that a user would always load twenty thumbnails but never any more unless it's necessary. 这意味着用户总是会加载20个缩略图,但除非有必要,否则永远不会加载。 I've never used a file explorer that required me to click to view a thumbnail. 我从来没有使用过要求我点击查看缩略图的文件浏览器。

This isn't something you have to worry about. 这不是你必须担心的事情。 The browser does this automatically for you. 浏览器会自动为您执行此操作。 You do have to make sure that the server is sending the right headers. 您必须确保服务器正在发送正确的标头。 It does help to have proper cache settings on the files to avoid the additional HEAD request. 它确实有助于对文件进行适当的缓存设置,以避免额外的HEAD请求。

Couldn't you just move/delete/rename it on the server? 你不能在服务器上移动/删除/重命名吗? And then handle the broken image with a javascript Image.onError to hide the broken image tag for the people that don't have the cached image. 然后使用javascript Image.onError处理损坏的图像,以隐藏没有缓存图像的人的损坏图像标记。

I think that would show cached version for those whohave it in their broswer chache. 我认为这将为那些在他们的broswer chache中拥有它的人显示缓存版本。 The rest of the people will not se anything 其他人不会做任何事情

A browser can send the "If-Modified-Since" and "ETag" headers to indicate to the server that it does have a version of a resource. 浏览器可以发送“If-Modified-Since”和“ETag”标头,以向服务器指示它确实具有资源的版本。 Check the W3 caching rules regarding these headers. 检查有关这些标头的W3缓存规则

You could query for those headers on the server, sending back the generic icon if not found, and a "304 Not Modified" response if found. 您可以在服务器上查询这些标头,如果找不到则发回通用图标,如果找到则返回“304 Not Modified”响应。

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

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