Merge branch '3.5.x'

Closes gh-47254
This commit is contained in:
Stéphane Nicoll 2025-09-17 15:36:10 +02:00
commit 35bceeead2
3 changed files with 9 additions and 10 deletions

View File

@ -702,7 +702,7 @@ public class TomcatServerProperties {
/**
* Time-to-live of the static resource cache.
*/
private @Nullable Duration cacheTtl;
private Duration cacheTtl = Duration.ofSeconds(5);
public boolean isAllowCaching() {
return this.allowCaching;
@ -720,11 +720,11 @@ public class TomcatServerProperties {
this.cacheMaxSize = cacheMaxSize;
}
public @Nullable Duration getCacheTtl() {
public Duration getCacheTtl() {
return this.cacheTtl;
}
public void setCacheTtl(@Nullable Duration cacheTtl) {
public void setCacheTtl(Duration cacheTtl) {
this.cacheTtl = cacheTtl;
}

View File

@ -380,12 +380,8 @@ public class TomcatWebServerFactoryCustomizer
factory.addContextCustomizers((context) -> context.addLifecycleListener((event) -> {
if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
context.getResources().setCachingAllowed(resource.isAllowCaching());
long cacheMaxSize = resource.getCacheMaxSize().toKilobytes();
context.getResources().setCacheMaxSize(cacheMaxSize);
if (resource.getCacheTtl() != null) {
long ttl = resource.getCacheTtl().toMillis();
context.getResources().setCacheTtl(ttl);
}
context.getResources().setCacheMaxSize(resource.getCacheMaxSize().toKilobytes());
context.getResources().setCacheTtl(resource.getCacheTtl().toMillis());
}
}));
}

View File

@ -16,6 +16,7 @@
package org.springframework.boot.tomcat.autoconfigure;
import java.time.Duration;
import java.util.Locale;
import java.util.function.Consumer;
@ -331,6 +332,8 @@ class TomcatWebServerFactoryCustomizerTests {
assertThat(properties.getResource().isAllowCaching()).isEqualTo(context.getResources().isCachingAllowed());
assertThat(properties.getResource().getCacheMaxSize())
.isEqualTo(DataSize.ofKilobytes(context.getResources().getCacheMaxSize()));
assertThat(properties.getResource().getCacheTtl())
.isEqualTo(Duration.ofMillis(context.getResources().getCacheTtl()));
});
}
@ -356,7 +359,7 @@ class TomcatWebServerFactoryCustomizerTests {
@Test
void customStaticResourceCacheTtl() {
bind("server.tomcat.resource.cache-ttl=10000");
bind("server.tomcat.resource.cache-ttl=10s");
customizeAndRunServer((server) -> {
Tomcat tomcat = server.getTomcat();
Context context = (Context) tomcat.getHost().findChildren()[0];