URLConnection.setUseCaches() 在实践中是如何工作的? [英] How does URLConnection.setUseCaches() work in practice?

查看:51
本文介绍了URLConnection.setUseCaches() 在实践中是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Applet,它使用 URLConnection 通过 http 连接加载图像.我正在为所有连接设置 setUseCaches(true),但仍然没有看到任何缓存行为.我的图像的 HTTP 标头具有合理的缓存设置.如果您查看 bug 4528599 有一个相当神秘的声明:

I have an Applet which is loading images over a http connection using URLConnection. I am setting setUseCaches(true) for all connections, but still not seeing any caching behavior. My image's HTTP headers have reasonable cache settings. If you look at bug 4528599 there is this rather mysterious statement:

Java Plug-In 的当前版本 (1.3.1) 只检查浏览器缓存名称以 .jar 或 .class 结尾的文件.有人告诉我,对于 Java Plug-In1.4 浏览器缓存将检查以下文件类型:.class、.jar、.zip、.jpg、.gif、.wav、.au.

The current version (1.3.1) of Java Plug-In only checks the browser cache for files whose names end in .jar or .class. I have been told that for Java Plug-In 1.4 the browser cache will be checked for the following file types: .class, .jar, .zip, .jpg, .gif, .wav, .au.

当然,这在 1.6 中被标记为 FIXED,但即使在 1.6 以下我也看不到任何缓存.我的图像是 PNG 文件,在某些情况下,实际上并没有以 .png 扩展名结尾.我没有看到任何缓存.

Of course, this was marked as FIXED for 1.6, but even under 1.6 I don't see any caching. My images are PNG files, and in some cases don't actually end in with a .png extension. I don't see any caching.

错误修复报告谈到了 1.6 统一下载引擎,但 google 似乎不太了解它.

The bug fix report talks about the 1.6 Unified Download Engine, but google doesn't seem to know much about it.

这应该有效还是只是另一个损坏的 Sun功能".有什么方法或解决方法可以让我的小程序从浏览器缓存中加载 PNG 图像?我不想实现我自己的......

Is this supposed to work or it just another broken Sun "feature". Is there any way or workaround where I can get my applet to load PNG images from the browser cache? I would prefer not to implement my own....

更新:缓存似乎与 ResponseCache 实现.有关更多信息,请参阅此技术说明这是如何工作的.最后一行说:

UPDATE: The caching seems to be tied to the ResponseCache implementation. See this technote for more info on how that works. The last line says:

URLConnection 缓存中没有默认实现Java 2 标准版.但是,Java Plugin 和 Java WebStart 可以提供一个开箱即用的.

There is no default implementation of URLConnection caching in the Java 2 Standard Edition. However, Java Plugin and Java WebStart do provide one out of the box.

所以在我看来问题真的变成了:Java Plugin ResponseCache 实现是如何真正工作的?v1.4/v1.5/v.16有什么区别

So it seems to me that the the question really becomes: How does the Java Plugin ResponseCache implementation really work? What are the differences between v1.4/v1.5/v.16

有人有什么想法吗?

推荐答案

此方法很可能仅将传出请求中的 HTTP Cache-Control 标头指令设置为允许缓存的值.如果你调用了 setUseCaches(false),就会有一个

This method very likely only sets the HTTP Cache-Control header directives in the outgoing request to values that permit caching. Had you called setUseCaches(false), there'd be a

Cache-Control: no-cache

指令为例.要检查这一点,您可以在小程序和服务器之间放置一个 HTTP 调试代理服务器,并查看标头.

directive for example. To check this, you could put an HTTP debugging proxy server between your applet and the server and take a look at the headers.

现在仅仅因为请求说它愿意使用缓存,您的服务器可能没有设置为启用它们.也许它没有在响应中设置一个适当长的 Expires 标头,或者它可能在响应中设置了一个禁止缓存的 Cache-Control 标头.

Now just because the request says it's willing to use caches, your server might not be set up to enable them. Perhaps it's not setting an Expires header with an appropriately long time in the response, or perhaps it's setting a Cache-Control header in the response that prohibits caching.

要检查的其他一些事项:

Some other things to check:

  • https 响应永远不会被缓存;
  • 您可能没有客户端和服务器之间的 HTTP 缓存;
  • 唯一的 HTTP 缓存是浏览器的缓存,但它可能被禁用或设置为使用很少的磁盘.

如果这只是用于 Web 应用程序测试的普通浏览器,您还需要确保没有点击刷新按钮,因为这相当于设置无缓存.

If this were just a vanilla browser to web application test, you'd also want to make sure you weren't hitting the refresh button, since that's equivalent to setting no-cache.

这篇关于URLConnection.setUseCaches() 在实践中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆