JDK中DNS缓存的分析
在JAVA中使用InetAddress.getByName(String host) 方法来获取给定hostname的IP地址。为了减少DNS解析的请求次数,提高解析效率,InetAddress中提供cache来缓存解析结果。
下面就此cache进行简单的分析:
该缓存实现比较简单,巧妙的利用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时得到的数据是最先插入的数据,此特性很重要在put方法中有所体现
} 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 entries if (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 expired
if (entry != null && policy != InetAddressCachePolicy.FOREVER) {//命中缓存条目后先判断是否过期
if (entry.expiration >= 0 &&
entry.expiration < System.currentTimeMillis()) {
cache.remove(host);
entry = null;
}
} return entry;
}
}
JDK中DNS缓存的分析的更多相关文章
- 好系统重装助手教你清理win7系统中DNS缓存
在我们使用电脑的过程中,有时候一个经常用的网页突然打不开了,遇到这种情况,清理一下DNS缓存就可以解决了.如何清理DNS缓存?小编这就给大家说一种最简单的方法. 1.组合键:win+R,输入cmd,点 ...
- 浏览器的DNS缓存
通过设置hosts文件可以强制指定域名对应的IP,当修改hosts文件,想要浏览器生效,最直接的方法关闭浏览器后重新开启:如果不想重启浏览器,只需要清空浏览器的DNS缓存即可.清空DNS缓存在chro ...
- 装饰模式和它在JDK中的实现
对装饰者模式的一个通俗的理解就是:一个东西A包装了另外一个东西B,A在B的功能基础上又扩展了新的功能,但是对外提供的接口不变 装饰者模式(Decorator)的定义: 动态地给一个对象添加一些额外的职 ...
- Java动态替换InetAddress中DNS的做法简单分析1
在java.net包描述中, 简要说明了一些关键的接口. 其中负责networking identifiers的是Addresses. 这个类的具体实现类是InetAddress, 底层封装了Inet ...
- Java7中的ForkJoin并发框架初探(中)——JDK中实现简要分析
原文发表于 2013 年 8 月 28 日 由 三石 根据前文描述的Doug Lea的理论基础,在JDK1.7中已经给出了Fork Join的实现.在Java SE 7的API中,多了ForkJoin ...
- [转]Java7中的ForkJoin并发框架初探(中)——JDK中实现简要分析
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp85 根据前文描述的Doug Lea的理论基础,在JDK1.7中已经给 ...
- 智能dns或CDN应用中,如何很好的解决DNS缓存问题? - 知乎
智能dns或CDN应用中,如何很好的解决DNS缓存问题? - 知乎 LISP路由器
- 更新域名解析以后,IP在cmd中ping不正确,清理DNS缓存
1清除ARP缓存,cmd下使用命令arp -d*代替执行. 2清除NETBT,cmd下使用命令nbtstat -R代替执行. 再清除DNS缓存,cmd下使用命令ipconfig /flushdns代替 ...
- chrome中清除dns缓存
chrome中清除dns缓存 http://rss.code-mire.com/item/1005.htm web开发经常要做各种host绑定的切换,firefox下有个DNS Flusher插件,但 ...
随机推荐
- 计算机中的大小端模式及C语言中如何鉴别他们
我的博客:www.while0.com 参考http://blog.csdn.net/ce123_zhouwei/article/details/6971544 写的很详细. 大小端主要是对数字类型来 ...
- “net.tcp://localhost:9000/ObtainData”处带有协定“"IObtainData"”的 ChannelDispatcher 无法打开其 IchannelListener。
http://stackoverflow.com/questions/1252791/how-to-solve-the-channeldispatcher-is-unable-to-open-its- ...
- statspack系列3
原文:http://jonathanlewis.wordpress.com/2006/12/27/analysing-statspack-3/ 作者:Jonathan Lewis 下面的例子中的结果并 ...
- POJ_1631_Bridging_Signals_(动态规划,LIS)
描述 http://poj.org/problem?id=1631 铁路左右相连,要求去掉一些边,使得剩下的边不交叉,求剩余边数的最大值. Bridging signals Time Limit: 1 ...
- 获得WCF Client端的本地端口 z
当WCF调用远程服务时,显示该调用的网速或流量.其中比较关键的一步就是需要获得WCF Client端的本地端口,原来以为是个简单的事情,结果查了1个多小时谷歌,硬是没找到好的法子,只有自己动手了. ...
- C#开发移动平台iOS、Android 与Windows
1.Xamarin http://www.csdn.net/article/2014-02-28/2818585-Xamarin-CSDN-mobile-develop
- JavaScript高级程序设计35.pdf
遍历 “DOM2级遍历和范围”模块定义了两个用于辅助完成顺序遍历DOM结构的类型:NodeIterator和TreeWalker,两个类型能够基于给定的起点对DOM结构执行深度优先(depth-fir ...
- Stand-up meeting
A stand-up meeting (or simply "stand-up") is a daily team-meeting held to provide a status ...
- JAVA Web项目的编码过滤器
首先写一个EncodeFilter的过滤类: package com.djtu.wy.common; import java.io.IOException;import javax.servlet.F ...
- Microsoft Visual Studio 2013如何设置查找头文件的路径