Skip to content

Commit 2314d15

Browse files
Kevin Zittritschsnicoll
authored andcommitted
Add configuration property for Tomcat's static resource cache max size
See gh-47229 Signed-off-by: Kevin Zittritsch <[email protected]>
1 parent cdd39ba commit 2314d15

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-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 @Nullable DataSize cacheMaxSize;
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 @Nullable DataSize getCacheMaxSize() {
716+
return this.cacheMaxSize;
717+
}
718+
719+
public void setCacheMaxSize(@Nullable 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ private void customizeStaticResources(ConfigurableTomcatWebServerFactory factory
384384
long ttl = resource.getCacheTtl().toMillis();
385385
context.getResources().setCacheTtl(ttl);
386386
}
387+
if (resource.getCacheMaxSize() != null) {
388+
long cacheMaxSize = resource.getCacheMaxSize().toKilobytes();
389+
context.getResources().setCacheMaxSize(cacheMaxSize);
390+
}
387391
}
388392
}));
389393
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,16 @@ void customStaticResourceAllowCaching() {
332332
});
333333
}
334334

335+
@Test
336+
void customStaticResourceCacheMaxSize() {
337+
bind("server.tomcat.resource.cache-max-size=4096KB");
338+
customizeAndRunServer((server) -> {
339+
Tomcat tomcat = server.getTomcat();
340+
Context context = (Context) tomcat.getHost().findChildren()[0];
341+
assertThat(context.getResources().getCacheMaxSize()).isEqualTo(4096L);
342+
});
343+
}
344+
335345
@Test
336346
void customStaticResourceCacheTtl() {
337347
bind("server.tomcat.resource.cache-ttl=10000");

0 commit comments

Comments
 (0)