Right out of the box cache-control header filter?

耗尽温柔 提交于 2019-12-18 13:27:19

问题


Is there a right of the box cache-control response header filter that will enable me to set those cache headers on my static resources without me having to build my own Filter? It seems like such a common task. Is there a Spring filter ? I am currently using Tomcat 6.0 and using Spring's ShallowEtagHeaderFilter to set etag to my resources but I need to also add the cache-control headers.


回答1:


Use mvc:resources for static files and mvc:interceptors with WebContentInterceptor for non-static files e.g.

  <!-- cache for one month -->
  <mvc:resources location="/css/" mapping="/css/**" cache-period="2592000"/>

  <!-- don't send any cache headers, rely on last-modified timestamps only -->
  <mvc:resources location="/img/" mapping="/img/**"/>
  <mvc:resources location="/js/" mapping="/js/**"/>

  <mvc:interceptors>
    <mvc:interceptor>
      <mvc:mapping path="/**/*.htm" />
        <bean id="responseCachingFilter" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
          <property name="cacheSeconds" value="0" />
          <property name="useExpiresHeader" value="true" />
          <property name="useCacheControlHeader" value="true" />
          <property name="useCacheControlNoStore" value="true" />
          <property name="cacheMappings">
          <props>
            <!-- cache for one month -->
            <prop key="/**/*.htm">2592000</prop>
          </props>
        </property>
      </bean>
    </mvc:interceptor>
  </mvc:interceptors>



回答2:


Details on Paul's solution:

 public class ResponseCachingFilter extends WebContentInterceptor implements
            Filter {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            try {
                this.preHandle((HttpServletRequest) request,
                        (HttpServletResponse) response, chain);
            } catch (Exception e) {
                throw new ServletException(e);
            }
            chain.doFilter(request, response);
        }
...

web.xml:

<filter>
    <filter-name>responseCachingFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>responseCachingFilter</filter-name>
    <url-pattern>*.js</url-pattern>
    <url-pattern>*.html</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.jpg</url-pattern>
    <url-pattern>*.gif</url-pattern>
    <url-pattern>*.css</url-pattern>
</filter-mapping>

and in the (top level, i.e., not mvc-servlet) application context:

<bean id="responseCachingFilter" class="lala.ResponseCachingFilter">
    <property name="cacheSeconds" value="0" />
    <property name="useExpiresHeader" value="true" />
    <property name="useCacheControlHeader" value="true" />
    <property name="useCacheControlNoStore" value="true" />
    <property name="cacheMappings">
        <props>
            <!-- cache for one month -->
            <prop key="/**/*.html">2592000</prop>
            <prop key="/**/*.htm">2592000</prop>
            <prop key="/**/*.jpg">2592000</prop>
            <prop key="/**/*.gif">2592000</prop>
            <prop key="/**/*.css">2592000</prop>
            <prop key="/**/*.js">2592000</prop>
        </props>
    </property>
</bean>



回答3:


Use DelegatingFilterProxy, pointing to your own impl of WebContentGenerator to handle the cache headers. The WebContentGenerator is dependency-injected into the DelegatingFilterProxy using Spring. Your impl will also implement Filter and call the appropriate cache-setting methods of WebContentGenerator from doFilter.



来源:https://stackoverflow.com/questions/3234907/right-out-of-the-box-cache-control-header-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!