小编典典

Spring Boot如何控制Tomcat缓存?

tomcat

我正在将具有5年历史的带有JSP的Spring MVC应用程序移植到Spring
Boot。因此,根据http://docs.spring.io/spring-boot/docs/current-
SNAPSHOT/reference/htmlsingle/#boot-features-jsp-
limitations中的示例,我使用的是“战争”包装。

嵌入式tomcat启动。但是,日志中充满了缓存警告,如以下示例所示

2016-08-25 14:59:01.442  INFO 28884 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-08-25 14:59:01.456  INFO 28884 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-08-25 14:59:01.458  INFO 28884 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.4
2016-08-25 14:59:01.531  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/displaytag-1.2.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:01.531  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/decrex-maven-0.1.10-SNAPSHOT.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:01.531  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/spring-boot-actuator-1.4.0.RELEASE.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:01.531  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/validation-api-1.1.0.Final.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:01.532  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/lucene-backward-codecs-5.3.1.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:01.532  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/lucene-queries-5.3.1.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
.....
2016-08-25 14:59:05.121  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/jstl-1.2.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:05.139  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/classes/commons-logging.properties] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:05.139  INFO 28884 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-08-25 14:59:05.139  INFO 28884 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 7117 ms
.....
2016-08-25 15:02:03.960  INFO 28884 --- [ndardContext[]]] org.apache.catalina.webresources.Cache   : The background cache eviction process was unable to free [10] percent of the cache for Context [] - consider increasing the maximum size of the cache. After eviction approximately [9,251] KB of data remained in the cache.


我很乐意增加tomcat缓存,但是我没有找到在Spring Boot中控制它的方法。请 指教!!!


我在下面尝试了安迪·威尔金森的建议。也尝试在EmbeddedServletContainerCustomizer

@Component
public class ServletContainerCustomizer implements EmbeddedServletContainerCustomizer {

    private static final Log log = LogFactory.getLog(ServletContainerCustomizer.class);

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        if (TomcatEmbeddedServletContainerFactory.class.isAssignableFrom(container.getClass())) {

            int cacheSize = 256 * 1024;
            log.info("Customizing tomcat factory. New cache size (KB) is " + cacheSize);

            TomcatEmbeddedServletContainerFactory tomcatFactory = (TomcatEmbeddedServletContainerFactory) container;
            tomcatFactory.addContextCustomizers((context) -> {
                StandardRoot standardRoot = new StandardRoot(context);
                standardRoot.setCacheMaxSize(cacheSize);
            });

        }
    }

}

有关更改高速缓存大小的消息在日志中,但是 上面代码 对警告 没有影响


阅读 818

收藏
2020-06-16

共1个答案

小编典典

我有一段时间一直遇到相同的问题,但是安迪·威尔金森(Andy
Wilkinson)的提示使我走上了正确的轨道。对我有用的是像Andy一样设置缓存,但同时还要在上下文中显式设置资源。

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            final int cacheSize = 40 * 1024;
            StandardRoot standardRoot = new StandardRoot(context);
            standardRoot.setCacheMaxSize(cacheSize);
            context.setResources(standardRoot); // This is what made it work in my case.

            logger.info(String.format("New cache size (KB): %d", context.getResources().getCacheMaxSize()));
        }
    };
    return tomcatFactory;
}

希望这可以帮助!

2020-06-16