Skip to content

Commit aebecb3

Browse files
committed
Polish contribution
See gh-47229
1 parent 2314d15 commit aebecb3

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ public static class Resource {
697697
/**
698698
* Maximum size of the static resource cache.
699699
*/
700-
private @Nullable DataSize cacheMaxSize;
700+
private DataSize cacheMaxSize = DataSize.ofMegabytes(10);
701701

702702
/**
703703
* Time-to-live of the static resource cache.
@@ -712,11 +712,11 @@ public void setAllowCaching(boolean allowCaching) {
712712
this.allowCaching = allowCaching;
713713
}
714714

715-
public @Nullable DataSize getCacheMaxSize() {
715+
public DataSize getCacheMaxSize() {
716716
return this.cacheMaxSize;
717717
}
718718

719-
public void setCacheMaxSize(@Nullable DataSize cacheMaxSize) {
719+
public void setCacheMaxSize(DataSize cacheMaxSize) {
720720
this.cacheMaxSize = cacheMaxSize;
721721
}
722722

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,12 @@ 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);
386388
}
387-
if (resource.getCacheMaxSize() != null) {
388-
long cacheMaxSize = resource.getCacheMaxSize().toKilobytes();
389-
context.getResources().setCacheMaxSize(cacheMaxSize);
390-
}
391389
}
392390
}));
393391
}

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

Lines changed: 13 additions & 1 deletion
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");
@@ -334,7 +346,7 @@ void customStaticResourceAllowCaching() {
334346

335347
@Test
336348
void customStaticResourceCacheMaxSize() {
337-
bind("server.tomcat.resource.cache-max-size=4096KB");
349+
bind("server.tomcat.resource.cache-max-size=4MB");
338350
customizeAndRunServer((server) -> {
339351
Tomcat tomcat = server.getTomcat();
340352
Context context = (Context) tomcat.getHost().findChildren()[0];

0 commit comments

Comments
 (0)