首页 > java > 有没有办法清除InputStream缓存

有没有办法清除InputStream缓存 (Is there a way to clear InputStream cache)

问题

我们使用InputStreams从外部URL读取图像,问题是图像在外部服务器中不断变化,但保持相同的URL。有时即使在外部服务器中对图像进行版本控制之后,更改也不会出现在我们这边。

调试这个简单的代码行时:

InputStream inputStream = new URL(srcURL).openStream();

我发现输入流仍然返回旧版本的媒体。即使通过浏览器访问相同的srcURL也会提供新版本的图像。

我以为是因为我们之前没有关闭inputStream。但是现在,即使纠正了这个/重新启动应用程序,我们仍然会得到旧版本。

inputStream是否使用某种memorycache?你能告诉我这里我们做错了什么吗?

先感谢您。

解决方法

如果在服务器端设置了适当的HTTP头(即,等等)LastModified,我相信您使用的代码将正常工作。ETagCache-Control

无论如何,你使用的代码:

InputStream inputStream = new URL(srcURL).openStream();

...是简写:

URLConnection connection = new URL(srcURL).openConnection();
InputStream inputStream = connection.getInputStream();

调用之前URLConnection,您可以使用实例来控制缓存,使用它的setUseCache(boolean)方法:getInputStream()

URLConnection connection = new URL(srcURL).openConnection();
connection.setUseCache(false);
InputStream inputStream = connection.getInputStream();

对于HTTP / HTTPS,这应该等同Cache-Control: no-cache于在请求中设置标头值,从而迫使行为良好的服务器向您发送最新版本的资源。

您也可以使用该URConnection.setRequestProperty(key, value)方法执行此操作,这是设置HTTP标头的更常用方法。您当然也可以在这里设置其他HTTP标头,例如If-None-MatchIf-Modified-Since。同样,必须调用之前设置所有标头getInputStream()

URLConnection connection = new URL(srcURL).openConnection();
connection.setRequestProperty("Cache-Control", "no-cache"); // Set general HTTP header
InputStream inputStream = connection.getInputStream();

问题

We're using InputStreams to read images from an external URL, the problem is that images are constantly changing in the external server, but keep the same URL. Sometimes even after versionning the image in the external server, the changes do not appear on our side.

When debugging this simple code line:

InputStream inputStream = new URL(srcURL).openStream();

I found that the input stream still returns the old version of the media. even if accessing the same srcURL via browser gives the new version of the image.

I thought it is because we weren't closing the inputStream before. But now, even after correcting this/restarting the app, we still get the old version.

Is the inputStream using some sort of memorycache? Can you tell me what we're doing wrong here?

Thank you in advance.

解决方法

I would believe that the code you use, would work correctly, if the appropriate HTTP headers are set on the server side (ie. LastModified, ETag, Cache-Control etc.).

Anyway, the code you use:

InputStream inputStream = new URL(srcURL).openStream();

...is shorthand for:

URLConnection connection = new URL(srcURL).openConnection();
InputStream inputStream = connection.getInputStream();

You can use the URLConnection instance to control caching, using it's setUseCache(boolean) method, before invoking getInputStream():

URLConnection connection = new URL(srcURL).openConnection();
connection.setUseCache(false);
InputStream inputStream = connection.getInputStream();

For HTTP/HTTPS this should be equivalent to setting a Cache-Control: no-cache header value in the request, forcing a well-behaved server to send you the most recent version of the resource.

You can also do this using the URConnection.setRequestProperty(key, value) method, which is a more general way of setting HTTP headers. You can of course set other HTTP headers here too, like If-None-Match or If-Modified-Since. Again, all headers have to be set before invoking getInputStream().

URLConnection connection = new URL(srcURL).openConnection();
connection.setRequestProperty("Cache-Control", "no-cache"); // Set general HTTP header
InputStream inputStream = connection.getInputStream();
相似信息