Right out of the box cache-control header filter?

前端 未结 3 1654
一整个雨季
一整个雨季 2020-12-31 23:12

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 Filt

相关标签:
3条回答
  • 2020-12-31 23:21

    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>
    
    0 讨论(0)
  • 2020-12-31 23:24

    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>
    
    0 讨论(0)
  • 2020-12-31 23:28

    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.

    0 讨论(0)
提交回复
热议问题