【问题标题】:Apache cache specific images and cssApache 缓存特定图像和 css
【发布时间】:2011-12-16 15:58:15
【问题描述】:

我们的网站使用 tomcat 和 apache 运行,并希望仅在 apache 级别缓存特定的 jpg、gif 图像以减少 tomcat 负载。

关于 CSS 和 Javascript,都可以缓存。

在部署更改的图像、css 和 javascripts 后,它应该会自动加载。

我正在尝试获取此配置,但找不到任何配置。有人可以分享示例配置吗?

只缓存特定的图像对我们来说非常重要,而且它也很紧急。

【问题讨论】:

    标签: image apache caching


    【解决方案1】:

    在tomcat应用context.xml中添加:

    disableCacheProxy="false" securePagesWithPragma="false"
    

    后跟以下任何一项:

    1.使用jsp:

    • 创建一个新的 jsp 例如。 “nocache.jsp”,内容如下:

      <meta http-equiv="pragma" content="no-cache">  
      <meta http-equiv="Cache-Control" content="no-store">    <!-- HTTP 1.1 -->  
      <meta http-equiv="Expires" content="0">
      
    • 在所有你不想缓存的jsp中包含这个jsp:

      &lt;jsp:include page="../nocache.jsp" /&gt;

    2.使用过滤器:

    • 创建一个新的过滤器类 - “CacheHeaderFilter”来处理不被缓存的类,如下所示:

      public void doFilter( ServletRequest request, ServletResponse response,   FilterChain filterChain) throws IOException, ServletException {   
          HttpServletResponse httpResponse = (HttpServletResponse)response;  
          httpResponse.setHeader("Cache-Control","no-cache");
          httpResponse.setHeader("Pragma","no-cache");  
          httpResponse.setDateHeader ("Expires", 0);  
          filterChain.doFilter(request, response);  
      }
      
    • 在应用程序 web.xml 中,配置此过滤器并指定不缓存的 URL,如下所示:

      <filter>
          <filter-name>CacheFilter</filter-name>
          <filter-class>com.org.CacheHeaderFilter</filter-class>
      </filter>  
      
      <filter-mapping>
          <filter-name>CacheFilter</filter-name>
          <url-pattern>*.action</url-pattern>
      </filter-mapping>`
      

    【讨论】:

      猜你喜欢
      • 2013-08-16
      • 2011-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 1970-01-01
      • 2011-11-24
      相关资源
      最近更新 更多