Browse Source

Only cache JarFile URL keys that are cheap to lookup

Update `JarFileUrlKey` so that only URLs that have a cheap `equals()`
method call are cached. This should prevent expensive DNS lookups from
being performed.

Fixes gh-46015
3.3.x
Phillip Webb 6 months ago
parent
commit
206785f838
  1. 10
      spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/jar/JarFileUrlKey.java

10
spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/jar/JarFileUrlKey.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -41,6 +41,9 @@ final class JarFileUrlKey { @@ -41,6 +41,9 @@ final class JarFileUrlKey {
* @return a {@link JarFileUrlKey} instance
*/
static String get(URL url) {
if (!isCachableUrl(url)) {
return create(url);
}
Map<URL, String> cache = (JarFileUrlKey.cache != null) ? JarFileUrlKey.cache.get() : null;
if (cache == null) {
cache = new ConcurrentHashMap<>();
@ -49,6 +52,11 @@ final class JarFileUrlKey { @@ -49,6 +52,11 @@ final class JarFileUrlKey {
return cache.computeIfAbsent(url, JarFileUrlKey::create);
}
private static boolean isCachableUrl(URL url) {
// Don't cache URL that have a host since equals() will perform DNS lookup
return url.getHost() == null || url.getHost().isEmpty();
}
private static String create(URL url) {
StringBuilder value = new StringBuilder();
String protocol = url.getProtocol();

Loading…
Cancel
Save