簡體   English   中英

Tomcat:緩存控制

[英]Tomcat: Cache-Control

Jetty 有一個CacheControl 參數(可以指定為 webdefault.xml),它決定了客戶端的緩存行為(通過影響發送到客戶端的標頭)。

Tomcat 有類似的選項嗎? 簡而言之,我想關閉由 tomcat 服務器和/或特定 webapp 提供的所有頁面的緩存?

更新

請注意,我不是指服務器端緩存。 我希望服務器告訴所有客戶端(瀏覽器)不要使用他們自己的緩存並始終從服務器獲取內容。 我想一次為所有資源執行此操作,包括靜態資源(.css、.js 等)。

由於 Tomcat 7 提供了一個容器,過期過濾器可能會有所幫助。 看:

ExpiresFilter 是Apache mod_expires的 Java Servlet API 端口。 此過濾器控制服務器響應中的Expires HTTP 標頭和Cache-Control HTTP 標頭的max-age指令的設置。 到期日期可以設置為相對於上次修改源文件的時間或客戶端訪問的時間。

<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 days</param-value>
    </init-param>
    <init-param>
        <param-name>ExpiresByType text/css</param-name>
        <param-value>access plus 10 hours</param-value>
    </init-param>
    <init-param>
        <param-name>ExpiresByType application/javascript</param-name>
        <param-value>access plus 10 minutes</param-value>
    </init-param>
    <!-- Let everything else expire immediately -->
    <init-param>
        <param-name>ExpiresDefault</param-name>
        <param-value>access plus 0 seconds</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>ExpiresFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

與上面的帖子類似,除了該代碼存在一些問題。 這將禁用所有瀏覽器緩存:

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

public class CacheControlFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {

        HttpServletResponse resp = (HttpServletResponse) response;
        resp.setHeader("Expires", "Tue, 03 Jul 2001 06:00:00 GMT");
        resp.setDateHeader("Last-Modified", new Date().getTime());
        resp.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
        resp.setHeader("Pragma", "no-cache");

        chain.doFilter(request, response);
    }

}

然后按照Stu Thompson's answer 中的描述映射到 web.xml 中。

我不相信有配置可以做到這一點。 但是編寫一個過濾器來設置基於每個 webapp 的 Cache-Control 標頭應該不會很費力。 例如:

public class test implements Filter {

        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                throws IOException, ServletException {

            chain.doFilter(request, response);
            ((StatusResponse)response).setHeader("Cache-Control",
                    "max-age=0, private, must-revalidate");
        }

        public void destroy() {}

        public void init(FilterConfig arg0) throws ServletException {}
}

您可以將此代碼段放入您的 web 應用程序的web.xml文件中。

<filter>
    <filter-name>SetCacheControl</filter-name>
    <filter-class>ch.dietpizza.cacheControlFilter</filter-class>
</filter>                       
<filter-mapping>
    <filter-name>SetCacheControl</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

實際上,Tomcat 配置中有幾個元素直接影響到這一點。 例如,請參閱http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html 上的文檔。

Atlassian 建議使用以下兩條語句來啟用瀏覽器端緩存,以便 Microsoft Internet Explorer 能夠正確下載和查看附加文檔:

<Valve className="org.apache.catalina.authenticator.FormAuthenticator" securePagesWithPragma="false" />
<Valve className="org.apache.catalina.authenticator.NonLoginAuthenticator" securePagesWithPragma="false" />

可能這就是你要找的:

http://tomcat.apache.org/tomcat-6.0-doc/config/context.html#Context%20Parameters

    cachingAllowed : If the value of this flag is true, the cache for static

 resources will be used. If not specified, the default value of the flag is true.

更改此標志后,還要刪除 /work/Catalina/localhost 中的應用程序緩存文件夾。

我知道的唯一參數是<Valve>元素上的disableProxyCaching 這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM