Browse Source

Add configuration property for Tomcat's static resource cache max size

See gh-47229

Signed-off-by: Kevin Zittritsch <kevin.zittritsch@vtinfo.com>
pull/47268/head
Kevin Zittritsch 3 months ago committed by Stéphane Nicoll
parent
commit
2314d15430
  1. 13
      module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java
  2. 4
      module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java
  3. 10
      module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java

13
module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java

@ -694,6 +694,11 @@ public class TomcatServerProperties { @@ -694,6 +694,11 @@ public class TomcatServerProperties {
*/
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 class TomcatServerProperties { @@ -707,6 +712,14 @@ public class TomcatServerProperties {
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;
}

4
module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java

@ -384,6 +384,10 @@ public class TomcatWebServerFactoryCustomizer @@ -384,6 +384,10 @@ public class TomcatWebServerFactoryCustomizer
long ttl = resource.getCacheTtl().toMillis();
context.getResources().setCacheTtl(ttl);
}
if (resource.getCacheMaxSize() != null) {
long cacheMaxSize = resource.getCacheMaxSize().toKilobytes();
context.getResources().setCacheMaxSize(cacheMaxSize);
}
}
}));
}

10
module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java

@ -332,6 +332,16 @@ class TomcatWebServerFactoryCustomizerTests { @@ -332,6 +332,16 @@ class TomcatWebServerFactoryCustomizerTests {
});
}
@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");

Loading…
Cancel
Save