Skip to content

Commit 2e8eb52

Browse files
committed
Merge pull request #47229 from kzittritsch
* pr/47229: Polish contribution Add configuration property for Tomcat's static resource cache max size Closes gh-47229
2 parents cdd39ba + aebecb3 commit 2e8eb52

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,11 @@ public static class Resource {
694694
*/
695695
private boolean allowCaching = true;
696696

697+
/**
698+
* Maximum size of the static resource cache.
699+
*/
700+
private DataSize cacheMaxSize = DataSize.ofMegabytes(10);
701+
697702
/**
698703
* Time-to-live of the static resource cache.
699704
*/
@@ -707,6 +712,14 @@ public void setAllowCaching(boolean allowCaching) {
707712
this.allowCaching = allowCaching;
708713
}
709714

715+
public DataSize getCacheMaxSize() {
716+
return this.cacheMaxSize;
717+
}
718+
719+
public void setCacheMaxSize(DataSize cacheMaxSize) {
720+
this.cacheMaxSize = cacheMaxSize;
721+
}
722+
710723
public @Nullable Duration getCacheTtl() {
711724
return this.cacheTtl;
712725
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ private void customizeStaticResources(ConfigurableTomcatWebServerFactory factory
380380
factory.addContextCustomizers((context) -> context.addLifecycleListener((event) -> {
381381
if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
382382
context.getResources().setCachingAllowed(resource.isAllowCaching());
383+
long cacheMaxSize = resource.getCacheMaxSize().toKilobytes();
384+
context.getResources().setCacheMaxSize(cacheMaxSize);
383385
if (resource.getCacheTtl() != null) {
384386
long ttl = resource.getCacheTtl().toMillis();
385387
context.getResources().setCacheTtl(ttl);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,18 @@ void customRemoteIpValve() {
322322
assertThat(remoteIpValve.getTrustedProxies()).isEqualTo("proxy1|proxy2");
323323
}
324324

325+
@Test
326+
void resourceCacheMatchesDefault() {
327+
TomcatServerProperties properties = new TomcatServerProperties();
328+
customizeAndRunServer((server) -> {
329+
Tomcat tomcat = server.getTomcat();
330+
Context context = (Context) tomcat.getHost().findChildren()[0];
331+
assertThat(properties.getResource().isAllowCaching()).isEqualTo(context.getResources().isCachingAllowed());
332+
assertThat(properties.getResource().getCacheMaxSize())
333+
.isEqualTo(DataSize.ofKilobytes(context.getResources().getCacheMaxSize()));
334+
});
335+
}
336+
325337
@Test
326338
void customStaticResourceAllowCaching() {
327339
bind("server.tomcat.resource.allow-caching=false");
@@ -332,6 +344,16 @@ void customStaticResourceAllowCaching() {
332344
});
333345
}
334346

347+
@Test
348+
void customStaticResourceCacheMaxSize() {
349+
bind("server.tomcat.resource.cache-max-size=4MB");
350+
customizeAndRunServer((server) -> {
351+
Tomcat tomcat = server.getTomcat();
352+
Context context = (Context) tomcat.getHost().findChildren()[0];
353+
assertThat(context.getResources().getCacheMaxSize()).isEqualTo(4096L);
354+
});
355+
}
356+
335357
@Test
336358
void customStaticResourceCacheTtl() {
337359
bind("server.tomcat.resource.cache-ttl=10000");

0 commit comments

Comments
 (0)