JDK中DNS缓存的分析
生活随笔
收集整理的這篇文章主要介紹了
JDK中DNS缓存的分析
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在JAVA中使用InetAddress.getByName(String host) 方法來(lái)獲取給定hostname的IP地址。為了減少DNS解析的請(qǐng)求次數(shù),提高解析效率,InetAddress中提供cache來(lái)緩存解析結(jié)果。
下面就此cache進(jìn)行簡(jiǎn)單的分析:
該緩存實(shí)現(xiàn)比較簡(jiǎn)單,巧妙的利用LinkedHashMap的特性來(lái)進(jìn)行過(guò)期條目的檢測(cè)和移除。
/*** Represents a cache entry*/static final class CacheEntry {CacheEntry(Object address, long expiration) {this.address = address;this.expiration = expiration;}Object address;long expiration;}//CacheEntry實(shí)例代表一個(gè)緩存記錄,一個(gè)記錄中包括address(IP 地址) 和 expiration/*** A cache that manages entries based on a policy specified* at creation time.*/static final class Cache {private LinkedHashMap cache;private Type type;enum Type {Positive, Negative};//此緩存只提供兩種緩存類型 Positive: DNS解析成功 Negative:DNS解析失敗/*** Create cache*/public Cache(Type type) {this.type = type;cache = new LinkedHashMap();//LinkedHashMap 保存了記錄的插入順序,當(dāng)遍歷LindedHashMap時(shí)得到的數(shù)據(jù)是最先插入的數(shù)據(jù),此特性很重要在put方法中有所體現(xiàn)}private int getPolicy() {//獲取配置的緩存策略 0: 不緩存 -1: 代表永久緩存 >=1:代表緩存的時(shí)間(unit: second)if (type == Type.Positive) {return InetAddressCachePolicy.get();} else {return InetAddressCachePolicy.getNegative();}}/*** Add an entry to the cache. If there's already an* entry then for this host then the entry will be* replaced.*/public Cache put(String host, Object address) {int policy = getPolicy();if (policy == InetAddressCachePolicy.NEVER) {return this;}// purge any expired entriesif (policy != InetAddressCachePolicy.FOREVER) {// As we iterate in insertion order we can// terminate when a non-expired entry is found.LinkedList expired = new LinkedList();Iterator i = cache.keySet().iterator();//每次put的時(shí)候都對(duì)緩存記錄做一個(gè)清理,由于每個(gè)條目的過(guò)期時(shí)間是一樣的,所以先插入的記錄就先到期long now = System.currentTimeMillis();while (i.hasNext()) {String key = (String)i.next();CacheEntry entry = (CacheEntry)cache.get(key);if (entry.expiration >= 0 && entry.expiration < now) {expired.add(key);} else {break;}}i = expired.iterator();while (i.hasNext()) {cache.remove(i.next());}}// create new entry and add it to the cache// -- as a HashMap replaces existing entries we// don't need to explicitly check if there is// already an entry for this host.long expiration;if (policy == InetAddressCachePolicy.FOREVER) {expiration = -1;} else {expiration = System.currentTimeMillis() + (policy * 1000);}CacheEntry entry = new CacheEntry(address, expiration);cache.put(host, entry);return this;}/*** Query the cache for the specific host. If found then* return its CacheEntry, or null if not found.*/public CacheEntry get(String host) {int policy = getPolicy();if (policy == InetAddressCachePolicy.NEVER) {return null;}CacheEntry entry = (CacheEntry)cache.get(host);// check if entry has expiredif (entry != null && policy != InetAddressCachePolicy.FOREVER) {//命中緩存條目后先判斷是否過(guò)期if (entry.expiration >= 0 &&entry.expiration < System.currentTimeMillis()) {cache.remove(host);entry = null;}}return entry;}}?
轉(zhuǎn)載于:https://www.cnblogs.com/cruze/p/3707011.html
總結(jié)
以上是生活随笔為你收集整理的JDK中DNS缓存的分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。