在通过DNS查找域名的过程中,可能会经过多台中间DNS服务器才能找到指定的域名,因此,在DNS服务器上查找域名是非常昂贵的操作。在Java中为了缓解这个问题,提供了DNS缓存。当InetAddress类第一次使用某个域名(如www.google.com)创建InetAddress对象后,JVM就会将这个域名和它从DNS上获得的信息(如IP地址)都保存在DNS缓存中。当下一次InetAddress类再使用这个域名时,就直接从DNS缓存里获得所需的信息,而无需再访问DNS服务器。DNS缓存在默认时将永远保留曾经访问过的域名信息,但我们可以修改这个默认值。
LINUX/android/libcore/luni/src/main/java/java/net/AddressCache.java
//The TTL for the Java-level cache is short, just 2s.
privatestaticfinallongTTL_NANOS= 2 * 1000000000L;
InetAddress.getByName(“www.baidu.com”);
在这个时间内,断网了还能返回结果,超过后会抛异常。没网络也会异常。
当前类型网络连接断开后会把map里面缓存给清除,所有在解MMIOT的bug的时候就自己写个map,把mms的缓存到自己写的map中,永远不删除,因为还有个时间的判断,所以不影响
package java.net;
import libcore.util.BasicLruCache;
/**
* Implements caching for {@code InetAddress}. We use a unified cache for both positive and negative
* cache entries.
*
* TODO: benchmark and optimize InetAddress until we get to the point where we can just rely on
* the C library level caching. The main thing caching at this level buys us is avoiding repeated
* conversions from 'struct sockaddr's to InetAddress[].
*/
class AddressCache {
/**
* When the cache contains more entries than this, we start dropping the oldest ones.
* This should be a power of two to avoid wasted space in our custom map.
*/
private static final int MAX_ENTRIES = 16;
// Aaron@20150514 change to fix MMS bug. Default value is 2S. 2.13.4 MMSC
// URL DNS HISTORY
// The TTL for the Java-level cache is short, just 2s.
// Aaron@20150515 change for MMIOT TTL bug
private static final long TTL_VZW_MMS = 15 * 60 * 1000000000L;
private static final long TTL_NANOS = 2 * 1000000000L;
// end
// The actual cache.
private final BasicLruCache<AddressCacheKey, AddressCacheEntry> cache
= new BasicLruCache<AddressCacheKey, AddressCacheEntry>(MAX_ENTRIES);
// Aaron@20150514 add for MMIOT TTL bug
private final BasicLruCache<AddressCacheKey, AddressCacheEntry> mcache = new BasicLruCache<AddressCacheKey, AddressCacheEntry>(
MAX_ENTRIES);
// end
static class AddressCacheKey {
private final String mHostname;
private final int mNetId;
AddressCacheKey(String hostname, int netId) {
mHostname = hostname;
mNetId = netId;
}
@Override public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof AddressCacheKey)) {
return false;
}
AddressCacheKey lhs = (AddressCacheKey) o;
return mHostname.equals(lhs.mHostname) && mNetId == lhs.mNetId;
}
@Override public int hashCode() {
int result = 17;
result = 31 * result + mNetId;
result = 31 * result + mHostname.hashCode();
return result;
}
}
static class AddressCacheEntry {
// Either an InetAddress[] for a positive entry,
// or a String detail message for a negative entry.
final Object value;
/**
* The absolute expiry time in nanoseconds. Nanoseconds from System.nanoTime is ideal
* because -- unlike System.currentTimeMillis -- it can never go backwards.
*
* We don't need to worry about overflow with a TTL_NANOS of 2s.
*/
final long expiryNanos;
AddressCacheEntry(Object value) {
this.value = value;
// Aaron@20150514 change for MMIOT TTL bug
if (InetAddress.isMMSconnect) {
this.expiryNanos = System.nanoTime() + TTL_VZW_MMS;
} else
// end
this.expiryNanos = System.nanoTime() + TTL_NANOS;
}
}
/**
* Removes all entries from the cache.
*/
public void clear() {
cache.evictAll();
}
/**
* Returns the cached InetAddress[] for 'hostname' on network 'netId'. Returns null
* if nothing is known about 'hostname'. Returns a String suitable for use as an
* UnknownHostException detail message if 'hostname' is known not to exist.
*/
public Object get(String hostname, int netId) {
AddressCacheEntry entry = cache.get(new AddressCacheKey(hostname, netId));
// Aaron@20150514 add for MMIOT TTL bug
if (InetAddress.isMMSconnect) {
entry = mcache.get(new AddressCacheKey(hostname, netId));
}
// end
// Do we have a valid cache entry?
if (entry != null && entry.expiryNanos >= System.nanoTime()) {
return entry.value;
}
// Either we didn't find anything, or it had expired.
// No need to remove expired entries: the caller will provide a replacement shortly.
return null;
}
/**
* Associates the given 'addresses' with 'hostname'. The association will expire after a
* certain length of time.
*/
public void put(String hostname, int netId, InetAddress[] addresses) {
cache.put(new AddressCacheKey(hostname, netId), new AddressCacheEntry(addresses));
// Aaron@20150514 add for MMIOT TTL bug
if (InetAddress.isMMSconnect) {
mcache.put(new AddressCacheKey(hostname, netId),
new AddressCacheEntry(addresses));
}
// end
}
/**
* Records that 'hostname' is known not to have any associated addresses. (I.e. insert a
* negative cache entry.)
*/
public void putUnknownHost(String hostname, int netId, String detailMessage) {
cache.put(new AddressCacheKey(hostname, netId), new AddressCacheEntry(detailMessage));
}
}