JSF2 静态资源缓存 [英] JSF2 Static resource caching
问题描述
Glassfish 3.1.2、Mojarra 2.1.6、SSL 激活
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-实践/缓存
也是一篇关于资源缓存的好文章.在我们的应用服务器中,SSL 被激活.我们看到静态资源(图像、脚本、css)没有被缓存.
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.
这是我的测试过滤器:
@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);
}
- 过期:好的.它是一种不会改变的静态资源,因此我们将到期日期设置为未来一个月.
- 最后修改时间:不确定.我读过将其设置为过去也会影响缓存
- 缓存控制:好的.允许安全缓存.安全影响?
此设置是否有任何影响?我还阅读了很多应该通过过滤器禁用缓存的帖子.我看到的唯一问题是用户可能会在新版本上遇到问题.样式和脚本可以在新版本中更改,但浏览器会忽略新版本并使用缓存中的文件.
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.
推荐答案
你只需要设置Cache-Control
.Expires
和 Last-Modified
已已经默认设置ResourceHandler
(并覆盖过滤器设置的值).Last-Modified
必须表示相关资源的最后修改时间戳.您可以通过 servletContext.getResource(path).openConnection().getLastModified()
获取它.但无论如何你都不需要在这里设置它.只需让 ResourceHandler
处理即可.
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.
至于版本控制,只要正确使用资源库即可.您可以在库文件夹中放置一个匹配正则表达式模式 d+(_d+)*
的版本文件夹,ResourceHandler
将提供最新的.
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.
例如
/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 library="libraryname" name="js/file.js" />
这篇关于JSF2 静态资源缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!