在Tomcat 6中启用缓存? [英] Enable caching in Tomcat 6?

查看:49
本文介绍了在Tomcat 6中启用缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提高页面加载速度.在Google Page Speed中,我遇到以下问题:

I need to increase the page load speed. In google page speed I have this issue:

以下可缓存资源的生存期很短.为以下资源指定至少未来一周的过期时间.

The following cacheable resources have a short freshness lifetime. Specify an expiration at least one week in the future for the following resources.

因此,我应该在标题中添加到期日期,以强制浏览器在页面中缓存静态内容.有什么解决办法吗?

So I should add expiration date to header to force browser to cache static content in the page. Is there any solution for this?

我使用的是tomcat 6.0.26.

I use tomcat 6.0.26.

推荐答案

使用spring框架的一种解决方案

您需要编写一个与此过滤器类似的过滤器:

You need to write a filter something similar to this one:

@WebFilter(dispatcherTypes = { YourDispatcherTypes }, urlPatterns = { "*.jsp","/yourresourcename/*", "oranyother"})
public class CacheHandlingFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain)
        throws ServletException, IOException {

    HttpServletRequest httpReq = (HttpServletRequest) request;

    HttpServletResponse httpResp = (HttpServletResponse) response;

    if(httpReq.getRequestURI().contains("/yourresourcename/")) {
        httpResp.setDateHeader("Expires", ProvideTimeForCacheHere);
        httpResp.setHeader("Cache-Control", "public, max-age=" + ProvideTimeForCacheHere);
    }

    filterChain.doFilter(request, response);
}
}

这篇关于在Tomcat 6中启用缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆