【问题标题】:web app caching on JBOSS server - cache not usedJBOSS 服务器上的 Web 应用缓存 - 未使用缓存
【发布时间】:2014-08-12 22:52:27
【问题描述】:

大家,

我的 webapp 处理浏览器缓存的方式似乎有问题。我试着用谷歌搜索了一下,但没有找到任何相关的东西。

为了处理缓存,我将以下代码放入我的 web.xml 中:

<filter>
    <filter-name>Cache filter</filter-name> 
    <filter-class>My_personnal_class</filter-class> 
</filter>
<filter-mapping>
    <filter-name>Cache filter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping>

并使用这个类:

public class my_personnal_class implements Filter {

public void destroy() {}

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)throws IOException,
ServletException {
    long time = 5 * 1000; // 5 secondes

    if (((HttpServletRequest)req).getRequestURI().indexOf("/a path/") != -1) {
        time= 20 * 60 * 1000; // 20 minutes
    }
    if (((HttpServletRequest)req).getRequestURI().indexOf("/yet another path/") != -1) {
        time= 20 * 60 * 1000; // 20 minutes
    }
    temps = temps + System.currentTimeMillis();
    ((HttpServletResponse)resp).setDateHeader("Expires", time);

    // Now let the request go through other filters and the servlet
    chain.doFilter(req, resp);
}

public void init(FilterConfig filterConfig)
throws ServletException {}

这样,我确实将缓存过期日期设置为 20 分钟。 (我可以在值得信赖的老萤火虫的网络标签中看到它。)

问题是缓存似乎没有被使用。当我重新加载页面时,服务器不会在缓存中获取数据,而是向我发送“304 - 未修改”响应。 另外,如果我修改了一个资源(比如说一个 HTML 页面),浏览器会去服务器上获取它(完全!200 响应,我会显示新版本),即使到期日期尚未满足。

我知道 304 响应对于带宽来说没什么大不了的,但仍然......我想知道这种行为是否正常。我做错了什么(或理解错了)?

任何关于这件事的信息都会有很大的帮助。

谢谢。

【问题讨论】:

    标签: java caching web-applications


    【解决方案1】:

    好的,我终于明白了,上面的代码运行正常。

    当您重新加载页面时,浏览器将尝试与服务器检查每个资源。然后,他将使用 etag 将缓存内容与服务器上的内容进行比较,并发出 304 响应。

    因此,永远不要通过重新加载页面 (f5) 来测试他的 http 缓存设置。只需点击链接或使用书签即可。

    一个容易犯的错误。

    【讨论】:

      猜你喜欢
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多