JSF2静态资源缓存 [英] JSF2 Static resource caching

查看:197
本文介绍了JSF2静态资源缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Glassfish 3.1.2,Mojarra 2.1.6,激活SSL

我有一个关于静态资源缓存的问题。我读了一些关于这个帖子,但我不知道我们应该使用哪些选项。这



https:/ /developers.google.com/speed/docs/best-practices/caching



也是一篇关于资源缓存的好文章。在我们的应用服务器中,SSL被激活。我们看到静态资源(图像,脚本,CSS)没有被缓存。



这是我的测试过滤器:

$ p $ @Override
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException {
if(request instanceof HttpServletRequest&&& response instanceof HttpServletResponse){
HttpServletRequest httpRequest =(HttpServletRequest)请求;
HttpServletResponse httpResponse =(HttpServletResponse)响应;
String uri = httpRequest.getRequestURI(); $()
if(GET_METHOD.equalsIgnoreCase(httpRequest.getMethod())& uri.contains(ResourceHandler.RESOURCE_IDENTIFIER)){
httpResponse.setDateHeader(Expires,System.currentTimeMillis()+ 2419200000L) ; //将来1个月
httpResponse.setDateHeader(Last-Modified,System.currentTimeMillis() - 2419200000L); //过去1个月
httpResponse.setHeader(Cache-Control,public); //安全缓存
}
}
chain.doFilter(request,response);




  • 过期:好的。这是一个静态的资源,不会改变,所以我们在将来设置一个月的到期日期。

  • 上次修改时间:不确定。我已经读过,将其设置为过去也影响缓存

  • Cache-Control:Ok。允许安全缓存。安全性的影响?



这个设置是否有影响?我也读了很多帖子,应该通过过滤器禁用缓存。我看到的唯一问题是用户可能在新版本中遇到问题。风格和脚本可以在新版本中改变,但浏览器忽略新的,并使用缓存文件。

只需要设置 Cache-Control Expires Last-Modified 已被默认设置 ResourceHandler (并会覆盖您的过滤器设置的值)。 Last-Modified 必须表示正在讨论的资源的最后修改时间戳。您可以通过 servletContext.getResource(path).openConnection()。getLastModified()获取它。不过你不需要在这里设置它。只要让 ResourceHandler 处理即可。



至于版本控制,请正确使用资源库。您可以在库文件夹中放置一个与正则表达式模式 \d +(_ \ d +)* 匹配的版本文件夹,并在 ResourceHandler 将服务最新的。



例如

  /resources/libraryname/1_0/js/file.js 
/ resources / libraryname / 1_1 / js / file .js
/resources/libraryname/1_2/js/file.js

然后从 1_2

 < h:outputScript库=librarynamename =js / file.js/> 


Glassfish 3.1.2, Mojarra 2.1.6, SSL activated

I have an question about static resource caching. I´ve read some posts about this but I´m not sure which options we should use. This

https://developers.google.com/speed/docs/best-practices/caching

is also an good article about resource caching. Within our application server SSL is activated. We see that static resources (images, scripts, css) are not cached.

Here is my test filter:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        String uri = httpRequest.getRequestURI();
        if (GET_METHOD.equalsIgnoreCase(httpRequest.getMethod()) && uri.contains(ResourceHandler.RESOURCE_IDENTIFIER)) {
            httpResponse.setDateHeader("Expires", System.currentTimeMillis() + 2419200000L); // 1 month in future.
            httpResponse.setDateHeader("Last-Modified", System.currentTimeMillis() - 2419200000L); // 1 month in past.
            httpResponse.setHeader("Cache-Control", "public"); // Secure caching
        }
    }
    chain.doFilter(request, response);
}

  • Expires: Ok. It´s an static resource that doesn´t change, so we set the expiration date one month in the future.
  • Last Modified: Not sure. I´ve read that setting this to the past has also impaces on caching
  • Cache-Control: Ok. Allow secure caching. Security impacts?

Are there any impacts with this settings? I´ve also read a lot of posts where caching should be disabled through an filter. The only problem I see is that users could have a problem on a new release. Styles and Scripts could be changed in the new release but the browser ignore the new one and use the files from cache.

解决方案

You only need to set Cache-Control. The Expires and Last-Modified are already set by default ResourceHandler (and would override the values set by your filter). The Last-Modified must represent the last modified timestamp of the resource in question. You can obtain it by servletContext.getResource(path).openConnection().getLastModified(). But you don't need to set it here anyway. Just let ResourceHandler handle.

As to versioning, just use resource libraries rightly. You can put a version folder matching regex pattern \d+(_\d+)* in the library folder and the ResourceHandler will serve the newest.

E.g.

/resources/libraryname/1_0/js/file.js
/resources/libraryname/1_1/js/file.js
/resources/libraryname/1_2/js/file.js

The following will then get the one from 1_2:

<h:outputScript library="libraryname" name="js/file.js" />

这篇关于JSF2静态资源缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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