开箱即用的缓存控制头过滤器?

时间:2010-07-13 07:15:52

标签: spring cache-control

是否有一个框缓存控制响应头过滤器的权限,使我能够在我的静态资源上设置这些缓存头,而无需构建我自己的过滤器?这似乎是一项共同的任务。有Spring过滤器吗?我目前正在使用Tomcat 6.0并使用Spring的ShallowEtagHeaderFilter将etag设置为我的资源,但我还需要添加缓存控制头。

3 个答案:

答案 0 :(得分:12)

对静态文件使用mvc:resources,对非静态文件使用带有WebContentInterceptor的mvc:interceptors,例如

  <!-- 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>

答案 1 :(得分:5)

保罗解决方案的详细信息:

 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>

并在(顶级,即非mvc-servlet)应用程序上下文中:

<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>

答案 2 :(得分:2)

使用DelegatingFilterProxy,指向您自己的WebContentGenerator impl来处理缓存标头。使用Spring将WebContentGenerator依赖注入到DelegatingFilterProxy中。您的impl还将实现Filter并从doFilter调用WebContentGenerator的相应缓存设置方法。