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

时间:2022-11-19 12:11:11

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. If it is improper, please help me to re-tag it.

我想也许JavaScript可以做到这一点,所以我将这篇文章标记为javascript。如果不合适,请帮我重新标记。

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. If a thumbnail is already cached, use the cached version; otherwise, show a generic image icon.

编辑:在这里,我提供了有关我将要实施的内容的更多详细信息。我正在开发类似于基于Web的文件浏览器,只有当用户单击每个图像旁边的“视图缩略图”按钮时才会加载缩略图。如果已经缓存了缩略图,请使用缓存版本;否则,显示通用图像图标。

5 个解决方案

#1


0  

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. Check the W3 caching rules regarding these headers.

浏览器可以发送“If-Modified-Since”和“ETag”标头,以向服务器指示它确实具有资源版本。检查有关这些标头的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”响应。

#2


1  

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
  2. 使用“show thumbnail”选项显示文件
  3. 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)
  4. 当选择“显示缩略图”选项时,从服务器加载缩略图(请注意,脚本通常会提供已调整大小的图像,该图像也可能存储在服务器上,以避免必须再次处理调整大小的一定数量的时间)
  5. The browser will automatically cache the image and prevent further server requests for a time
  6. 浏览器将自动缓存映像并防止进一步的服务器请求

#3


1  

My RSS reader, for feeds that have a lot of posts, loads a full page of posts. 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.

我的RSS阅读器,对于包含大量帖子的Feed,会加载整页帖子。条目数取决于实际窗口的大小。当我滚动到列表的底部时,如果有更多帖子,它将加载另一页。

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. I've never used a file explorer that required me to click to view a thumbnail.

这适用于您的文件浏览器吗?这意味着用户总是会加载20个缩略图,但除非必要,否则永远不会加载。我从未使用过要求我点击查看缩略图的文件浏览器。

#4


0  

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请求。

#5


0  

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. The rest of the people will not se anything

我认为这将为那些在他们的broswer chache中拥有它的人显示缓存版本。其他人不会做任何事情

#1


0  

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. Check the W3 caching rules regarding these headers.

浏览器可以发送“If-Modified-Since”和“ETag”标头,以向服务器指示它确实具有资源版本。检查有关这些标头的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”响应。

#2


1  

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
  2. 使用“show thumbnail”选项显示文件
  3. 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)
  4. 当选择“显示缩略图”选项时,从服务器加载缩略图(请注意,脚本通常会提供已调整大小的图像,该图像也可能存储在服务器上,以避免必须再次处理调整大小的一定数量的时间)
  5. The browser will automatically cache the image and prevent further server requests for a time
  6. 浏览器将自动缓存映像并防止进一步的服务器请求

#3


1  

My RSS reader, for feeds that have a lot of posts, loads a full page of posts. 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.

我的RSS阅读器,对于包含大量帖子的Feed,会加载整页帖子。条目数取决于实际窗口的大小。当我滚动到列表的底部时,如果有更多帖子,它将加载另一页。

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. I've never used a file explorer that required me to click to view a thumbnail.

这适用于您的文件浏览器吗?这意味着用户总是会加载20个缩略图,但除非必要,否则永远不会加载。我从未使用过要求我点击查看缩略图的文件浏览器。

#4


0  

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请求。

#5


0  

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. The rest of the people will not se anything

我认为这将为那些在他们的broswer chache中拥有它的人显示缓存版本。其他人不会做任何事情