JSF高速缓存静态资源过滤器 [英] JSF Cache Static Resources Filter

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

问题描述

如何编写过滤器,该过滤器将按照Google的建议适当地缓存静态资源( https://developers.google.com/speed/docs/best-practices/caching ).

How do I write a filter which will appropriately cache static resources as recommended by Google (https://developers.google.com/speed/docs/best-practices/caching).

创建一个将最后修改日期设置为某个静态日期的过滤器是否足够(每次服务器重新启动时都会更改)?

Is it sufficient to create a filter which sets the last-modified date to some static date (this will change every time the server restarts)?

指定Expires或Cache-Control max-age之一很重要, 以及所有可缓存资源的Last-Modified或ETag之一.它是 冗余以同时指定Expires和Cache-Control:max-age或 同时指定Last-Modified和ETag.

It is important to specify one of Expires or Cache-Control max-age, and one of Last-Modified or ETag, for all cacheable resources. It is redundant to specify both Expires and Cache-Control: max-age, or to specify both Last-Modified and ETag.

上面的链接似乎表明您需要指定Expires或Cache-Control.为什么这样做有必要?

The link above seems to suggest you need to specify Expires or Cache-Control. Why is that necessary?

推荐答案

如何编写过滤器,该过滤器将按照Google的建议适当地缓存静态资源

如果您指的是JSF资源,则/resources文件夹中的文件完全由JSF内置资源处理程序处理(

If you mean with JSF resources the files in /resources folder which are fully handled by JSF builtin resource handler (and thus all referenced via <h:outputStylesheet>, <h:outputScript>, <h:graphicImage>, #{resource} and thus not via the plain HTML way), then you don't need to homegrow a filter for the job. The only thing which you need to do to satisfy Google recommendations is to set the Expires date a bit further in the future. It namely defaults to 7 days (604800000 milliseconds) while performance testing tools like Google Page Speed and Yahoo YSlow recommends a minimum of 30 days (2592000000 milliseconds).

在Mojarra中,您可以在web.xml中使用以下上下文参数进行设置:

In Mojarra, you can set it with the following context parameter in web.xml:

<context-param>
    <param-name>com.sun.faces.defaultResourceMaxAge</param-name>
    <param-value>2592000000</param-value> <!-- 30 days -->  
</context-param>

在MyFaces中,并添加以下内容:

And in MyFaces with the following one:

<context-param>
    <param-name>org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES</param-name>
    <param-value>2592000000</param-value> <!-- 30 days -->  
</context-param>


创建一个将上次修改日期设置为某个静态日期的过滤器是否足够(每次服务器重新启动时都会更改)?

您不需要也不应该设置Last-Modified. JSF资源处理程序已经自动执行此操作.如果由于更改了资源而要强制按资源重新加载,请使用资源库版本控制.另请参见>什么是JSF资源库以及如何使用它?

You don't and shouldn't need to set the Last-Modified. The JSF resource handler already does that automatically. If you'd like to force reloading by resources because you changed them, then use resource library versioning. See also What is the JSF resource library for and how should it be used?

请注意,每次服务器重新启动时更改它都是没有意义的,因为Expires标头仍然会告诉浏览器仅在一定时间后重新测试缓存的有效性.在浏览器实际请求资源之前,浏览器将永远不会注意到资源Last-Modified中的更改.唯一使浏览器难于完全重新请求资源的是URL的更改,通常是通过更改查询字符串参数值来实现的. JSF资源库版本控制可以做到这一点.

Note that changing it everytime the server restarts makes no sense as the Expires header would still keep telling the browser to re-test the validity of the cache after a certain period only. Until the browser actually requests the resource, the browser would never notice the change in the Last-Modified of a resource. The only thing which forces the browser hard to re-request the resource fully is a change in the URL, usually achieved by a changed query string parameter value. The JSF resource library versioning does exactly that.

还请注意, OmniFaces CombinedResourceHandler 使用资源的最后修改时间戳作为资源版本在查询字符串中,而不是在资源库版本中.因此,如果您正在使用它,则不一定需要资源库版本控制机制.

Also note that the OmniFaces CombinedResourceHandler uses the resource's last modified timestamp as "resource version" in the query string instead of the resource library version. So if you're using that, you don't necessarily need resource library versioning mechanism.

上面的链接似乎表明您需要指定Expires或Cache-Control.为什么这样做有必要?

Expires标头告诉浏览器何时通过条件GET请求重新测试缓存资源的有效性.因此,直到那个时候,浏览器都不会这样做,并且会继续使用缓存中的那个. Cache-Control告诉浏览器要使用哪种缓存策略.请注意,将其设置为例如no-cache而不是public,则Expires标头将无效.还要注意,缺少Cache-Control标头意味着public(由JSF资源完成).

The Expires header tells the browser when to re-test the validity of the cached resource by a conditional GET request. So, until that time the browser won't do that and will keep using the one in the cache. The Cache-Control tells the browser which caching strategy to use. Note that when it's set to e.g. no-cache instead of public, then the Expires header would have no effect. Also note that the absence of Cache-Control header implies public (as done by JSF resources).

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

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