diff --git a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java index 202a1dc837b1..8cbcc61ec9fd 100644 --- a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java +++ b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java @@ -694,6 +694,11 @@ public static class Resource { */ private boolean allowCaching = true; + /** + * Maximum size of the static resource cache. + */ + private @Nullable DataSize cacheMaxSize; + /** * Time-to-live of the static resource cache. */ @@ -707,6 +712,14 @@ public void setAllowCaching(boolean allowCaching) { this.allowCaching = allowCaching; } + public @Nullable DataSize getCacheMaxSize() { + return this.cacheMaxSize; + } + + public void setCacheMaxSize(@Nullable DataSize cacheMaxSize) { + this.cacheMaxSize = cacheMaxSize; + } + public @Nullable Duration getCacheTtl() { return this.cacheTtl; } diff --git a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java index b315bc3ab43c..ca835acb4717 100644 --- a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java +++ b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java @@ -384,6 +384,10 @@ private void customizeStaticResources(ConfigurableTomcatWebServerFactory factory long ttl = resource.getCacheTtl().toMillis(); context.getResources().setCacheTtl(ttl); } + if (resource.getCacheMaxSize() != null) { + long cacheMaxSize = resource.getCacheMaxSize().toKilobytes(); + context.getResources().setCacheMaxSize(cacheMaxSize); + } } })); } diff --git a/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java b/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java index 9c0cf2f457a6..8b8c203b5291 100644 --- a/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java +++ b/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java @@ -332,6 +332,16 @@ void customStaticResourceAllowCaching() { }); } + @Test + void customStaticResourceCacheMaxSize() { + bind("server.tomcat.resource.cache-max-size=4096KB"); + customizeAndRunServer((server) -> { + Tomcat tomcat = server.getTomcat(); + Context context = (Context) tomcat.getHost().findChildren()[0]; + assertThat(context.getResources().getCacheMaxSize()).isEqualTo(4096L); + }); + } + @Test void customStaticResourceCacheTtl() { bind("server.tomcat.resource.cache-ttl=10000");