JDK中DNS缓存的分析
生活随笔
收集整理的這篇文章主要介紹了
JDK中DNS缓存的分析
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在JAVA中使用InetAddress.getByName(String host) 方法來獲取給定hostname的IP地址。為了減少DNS解析的請求次數(shù),提高解析效率,InetAddress中提供cache來緩存解析結(jié)果。
下面就此cache進行簡單的分析:
該緩存實現(xiàn)比較簡單,巧妙的利用LinkedHashMap的特性來進行過期條目的檢測和移除。
/*** Represents a cache entry*/static final class CacheEntry {CacheEntry(Object address, long expiration) {this.address = address;this.expiration = expiration;}Object address;long expiration;}//CacheEntry實例代表一個緩存記錄,一個記錄中包括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 保存了記錄的插入順序,當遍歷LindedHashMap時得到的數(shù)據(jù)是最先插入的數(shù)據(jù),此特性很重要在put方法中有所體現(xiàn)}private int getPolicy() {//獲取配置的緩存策略 0: 不緩存 -1: 代表永久緩存 >=1:代表緩存的時間(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的時候都對緩存記錄做一個清理,由于每個條目的過期時間是一樣的,所以先插入的記錄就先到期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) {//命中緩存條目后先判斷是否過期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缓存的分析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 河流Shader
- 下一篇: Careercup - Google面试