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插件,但 ...
随机推荐
- *string++优先级的问题
这个东西困扰了我几天,关于优先级问题确实是个恼人的东西,为了这个专门翻了C语言课本,得知 所有一目运算符都是第二级优先级 结合性是从右到左 那么*string++应该就是*(string++),也就是 ...
- AD域环境的搭建 基于Server 2008 R2
AD(Active Directory)即活动目录,微软的基础件.微软的很多产品如:Exchange Server,Lync Server,SharePoint Server,Forefront Se ...
- VS2013 取消 直接单击文件 然后直接打开
工具——选项——
- 【HDOJ】3416 Marriage Match IV
先求SPSS.然后遍历每条边,检查是否为最短路径的边,如果是(dis[v]==dis[u]+w)则加入到网络流中.最后Dinic最大流. /* 3416 */ #include <iostrea ...
- axis2 调用.net基于https的WebService接口
一.创建证书 1,在IE中访问WSDL的URL,弹出“安全警报”窗口,查看证书->详细信息标签页->复制到文件->下一步->下一步->指定文件名,将证书下载保存为.cer ...
- Light OJ 1060 - nth Permutation(组合数)
题目大意: 给你一个字符串,问这个字符串按照特定顺序排列之后,第n个字符串是哪个? 题目分析: 首先我们要会求解总个数.也就是共有len个字符,每个字符有ki个,那么总组合方式是多少种? 总组合方式就 ...
- 【转】Mac OS X 快捷键(完整篇)
原文网址:http://www.nooidea.com/2011/01/mac-os-x-keyboard-shortcuts.html 没有写到基本的一些组合,只是一些可能大家不太常用到但及其提高机 ...
- 【转】Android中intent传递对象和Bundle的用法
原文网址:http://blog.csdn.net/lixiang0522/article/details/8642202 android中的组件间传递的对象一般实现Parcelable接口,当然也可 ...
- HDOJ --- 1258
#include<map> #include<string> #include<cstdio> #include<cstring> #include&l ...
- python指定pypi的源地址 镜像地址
python pip源介绍: python装包一般都用easy_install 或者pip. 他们俩的原理跟apt-get差不多,都是去某个地址去下载你所指定的包. pip和easy_install默 ...