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

See gh-47229

Signed-off-by: Kevin Zittritsch <kevin.zittritsch@vtinfo.com>
This commit is contained in:
Kevin Zittritsch 2025-09-16 07:18:33 -04:00 committed by Stéphane Nicoll
parent cdd39bafd7
commit 2314d15430
3 changed files with 27 additions and 0 deletions

View File

@ -694,6 +694,11 @@ public class TomcatServerProperties {
*/ */
private boolean allowCaching = true; private boolean allowCaching = true;
/**
* Maximum size of the static resource cache.
*/
private @Nullable DataSize cacheMaxSize;
/** /**
* Time-to-live of the static resource cache. * Time-to-live of the static resource cache.
*/ */
@ -707,6 +712,14 @@ public class TomcatServerProperties {
this.allowCaching = allowCaching; this.allowCaching = allowCaching;
} }
public @Nullable DataSize getCacheMaxSize() {
return this.cacheMaxSize;
}
public void setCacheMaxSize(@Nullable DataSize cacheMaxSize) {
this.cacheMaxSize = cacheMaxSize;
}
public @Nullable Duration getCacheTtl() { public @Nullable Duration getCacheTtl() {
return this.cacheTtl; return this.cacheTtl;
} }

View File

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

View File

@ -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 @Test
void customStaticResourceCacheTtl() { void customStaticResourceCacheTtl() {
bind("server.tomcat.resource.cache-ttl=10000"); bind("server.tomcat.resource.cache-ttl=10000");