Tomcat性能調優 通過ExpiresFilter設置資源緩存
【官方文檔】
http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Expires_Filter
【簡介】
ExpiresFilter是Java servlet API 當中的一部分,它負責控制設置response當中的響應頭(Expires) 和 ( Cache-Control的max-age),過期時間可以設置為相對於源文件的最后修改時間,或者瀏覽器的訪問時間。
這些響應頭指示瀏覽器控制文檔的緩存,如果使用了緩存,那么瀏覽器在下一次獲取文檔(HTML)的時候就會從本地緩存中獲取,而不是訪問實際的資源服務器,除非超過失效時間。關於HTTP頭控制客戶端緩存的更多介紹請參見我的另外一篇文章,http://www.cnblogs.com/daxin/p/3981553.html或者可以直接查閱HTTP協議(see RFC 2616 section 14.9)。
【例子】
Basic configuration to add 'Expires' and 'Cache-Control: max-age=' headers to images, css and javascript.
下面的例子針對所有的 IMAGE CSS JAVASCRIPT 輸出進行控制,添加HTTP頭設置客戶端緩存。
<filter>
<filter-name>ExpiresFilter</filter-name>
<filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
<init-param>
<param-name>ExpiresByType image</param-name>
<param-value>access plus 10 minutes</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType text/css</param-name>
<param-value>access plus 10 minutes</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType application/javascript</param-name>
<param-value>access plus 10 minutes</param-value>
</init-param>
</filter> ... <filter-mapping>
<filter-name>ExpiresFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
【語法介紹】
<init-param>
<param-name>ExpiresDefault</param-name>
<param-value><base> [plus] {<num> <type>}*</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType type</param-name>
<param-value><base> [plus] {<num> <type>}*</param-value>
</init-param>
<init-param>
<param-name>ExpiresByType type;encoding</param-name>
<param-value><base> [plus] {<num> <type>}*</param-value>
</init-param>
base可以設置為:
access 訪問時間。now(equivalent to 'access') 當前時間,相當於訪問時間。modification 文件的最后修改時間。
plus的值是可選的,可以不填,沒什么意義。
num必須是一個integer。
type的值可以設置為:不一一解釋了一看就明白了。
yearsmonthsweeksdayshoursminutesseconds
下面的例子設置所有的MIME類型的輸出被客戶端緩存一個月,即緩存過期時間為一個月。ExpiresDefault代表所有的MIME類型 比如 text/plain image/png application/javascript等等。
<init-param>
<param-name>ExpiresDefault</param-name>
<param-value>access plus 1 month</param-value>
</init-param>
<init-param>
<param-name>ExpiresDefault</param-name>
<param-value>access plus 4 weeks</param-value>
</init-param>
<init-param>
<param-name>ExpiresDefault</param-name>
<param-value>access plus 30 days</param-value>
</init-param>
過期時間還可以使用子句進行調整,例如:
<init-param>
<param-name>ExpiresByType text/html</param-name>
<param-value>access plus 1 month 15 days 2 hours</param-value> //緩存到期時間設置為當前時間 加上 1個月 15天 2小時
</init-param>
<init-param>
<param-name>ExpiresByType image/gif</param-name>
<param-value>modification plus 5 hours 3 minutes</param-value> //緩存到期時間設置為文件的修改時間 加上 5小時 3分鍾
</init-param>
【什么樣的情況response會被設置緩存頭】
1、no expiration header is defined (Expires header or the max-age directive of the Cache-Control header)
也就是說這倆個響應頭沒有被定義情況過濾器才會設置,如果我們已經設置了該響應頭,那么就不會再重復設置了。
2、the response status code is not excluded by the directive ExpiresExcludedResponseStatusCodes
response的status沒有包含在ExpiresExcludedResponseStatusCodes這個屬性的配置當中。
這個屬性可以在web.xml當中配置過濾器當中配置,定義哪些狀態嗎不需要設置緩存頭,默認304的時候就不會被設置。
3、根據ExpiresByType 指定的 Content-Type 或者 使用了ExpiresDefault
