[java] Caching non-loopback ipv4 address of the local machine because host resolution is a slow operation
public class NetworkUtils {
+ private static String cachedNonLoopbackAddressOfThisMachine;
+
private final NetworkInterfaceProvider networkInterfaceProvider;
private volatile String hostname;
private volatile String address;
@@ -84,7 +86,10 @@ public String getPrivateLocalAddress() {
* @return A String representing the host name or non-loopback IP4 address of this machine.
*/
public String getNonLoopbackAddressOfThisMachine() {
- return getIp4NonLoopbackAddressOfThisMachine().getHostName();
+ if (cachedNonLoopbackAddressOfThisMachine == null) {
+ cachedNonLoopbackAddressOfThisMachine = getIp4NonLoopbackAddressOfThisMachine().getHostName();
+ }
+ return cachedNonLoopbackAddressOfThisMachine;
}
/**
