通过java启用浏览器缓存
美好的一天,
我正在使用 CacheFilter 过滤到我的服务器的某个路径(它将图像流输出到响应流)。 我在 web.xml 中对其进行了如下配置:
<filter>
<filter-name>imagesCache</filter-name>
<filter-class>com.samaxes.cachefilter.presentation.CacheFilter</filter-class>
<init-param>
<param-name>privacy</param-name>
<param-value>public</param-value>
</init-param>
<init-param>
<param-name>expirationTime</param-name>
<param-value>2592000</param-value>
</init-param>
</filter>
...
<filter-mapping>
<filter-name>imagesCache</filter-name>
<url-pattern>/my/path/*</url-pattern>
</filter-mapping>
使用我的 Firefox,如果我通过地址栏访问我的 url,它会第一次访问服务器,但在后续调用期间使用缓存。 但是,如果网址位于我的页面内(即 ),它似乎总是会访问服务器。
[编辑] 经过几次测试后,通过地址栏访问我的图像并不总是有效。 但缓存似乎确实比 . 至于是否真的,我不确定。
附加信息: 我的路径类似于 /my/path?then=some&query=strings。 请注意,它没有扩展名(即 gif、png、jpeg ),但它的 mimetype 设置正确( image/gif、image/png、image/jpeg )。 我不确定缺少扩展名或查询字符串的存在是否有任何影响。 (另外,还有一点。虽然我的 url 有查询字符串,但我在测试中一遍又一遍地使用相同的 uri + 查询字符串)。
有什么想法吗?
谢谢
Good day,
I am using CacheFilter to filter a certain path to my server (which outputs an image stream to the response stream). And I've configured it in my web.xml as follows:
<filter>
<filter-name>imagesCache</filter-name>
<filter-class>com.samaxes.cachefilter.presentation.CacheFilter</filter-class>
<init-param>
<param-name>privacy</param-name>
<param-value>public</param-value>
</init-param>
<init-param>
<param-name>expirationTime</param-name>
<param-value>2592000</param-value>
</init-param>
</filter>
...
<filter-mapping>
<filter-name>imagesCache</filter-name>
<url-pattern>/my/path/*</url-pattern>
</filter-mapping>
Using my firefox, if I access my url via the address bar, it hits the server the first time but uses the cache during succeeding calls. However, if the url is inside my page ( i.e. <img src="..."/>
), it seems to hit the server all the time.
[EDIT] After a few more testing, accessing my image via the address bar does not work all the time. But caching does seem to work more often with it than . As to whether it really, I am not sure.
Additional Info:
my path is something like /my/path?then=some&query=strings. Notice that it doesn't have an extension (i.e. gif, png, jpeg ) but it's mimetype is set properly ( image/gif, image/png, image/jpeg ). I am not sure if the lack of extension or the presence of the query strings have any impact. (Also, another note. though my url have query strings, I am using the same uri + query string over and over again with my tests).
Any ideas why?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要特别查找请求中的任何 Cache-Control 标头。 如果请求显示类似 Cache-Control: no-cache 或 Cache-Control: max-age=0 的内容,则缓存无法提供缓存副本。 或者,如果响应的 Expires 标头在时间上太接近,则无法长时间缓存。
标头和说明的完整列表位于 HTTP 1.1 规范中。 请参阅 HTTP 中的缓存 (13) 和 标头字段定义 (14)
Firebug 插件 是使用 Firefox 检查请求和响应标头的一种好方法。
另请注意您如何使用 Firefox。 点击刷新按钮相当于说 Cache-Control: no-cache —— 它表示您想要尽可能最新的副本,这将带您一路返回原始 Web 服务器。
You want to especially look for any Cache-Control header in your request. If a request says something like Cache-Control: no-cache or Cache-Control: max-age=0, then caches can't serve up a cached copy. Or if the response has an Expires header that's too close in time, then it can't be cached for long.
The complete list of headers and explanations is in the HTTP 1.1 specification. See Caching in HTTP (13) and Header Field Definitions (14)
The Firebug plug-in is one good way to check request and response headers using Firefox.
Also watch out how you're using Firefox. Hitting the refresh button is equivalent to saying Cache-Control: no-cache -- it says you want the freshest copy possible, which takes you all the way back to your origin web server.
我会调查正在发送的 HTTP 请求 - 特别是为该图像请求发送的 HTTP 标头。 您可以使用 Firefox 插件,和/或检查标题Servlet 端(在 HttpServletRequest 对象中)
I would investigate the HTTP request being sent - particularly the HTTP headers being sent for that image request. You can use a Firefox plugin, and/or check the headers at the servlet end (in the
HttpServletRequest
object)