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插件,但 ...
随机推荐
- BZOJ 1725: [Usaco2006 Nov]Corn Fields牧场的安排
Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M<=12; 1<=N<=12),每一格都是一块正方形的土地.FJ打算在牧 ...
- java service
http://www.regexlab.com/zh/jar2exe/demo1.htm http://www.zhangxinxu.com/wordpress/?p=1058 http://www. ...
- UIKIT网页基本结构学习
没办法,哈哈,以后一段时间,如果公司没有招到合适的运维研发, 啥啥都要我一个人先顶上了~~~:) 也好,可以让人成长. UIKIT,BOOTSTRAP之类的前端,搞一个是有好处的,我们以前即然是用了U ...
- poj crane
#include<stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> #de ...
- 计算机中的大小端模式及C语言中如何鉴别他们
我的博客:www.while0.com 参考http://blog.csdn.net/ce123_zhouwei/article/details/6971544 写的很详细. 大小端主要是对数字类型来 ...
- perl post json数据
use LWP::UserAgent; use URI::Escape; use Net::Ping; use JSON qw(encode_json); use Socket; use Net::S ...
- 《IT运维之道》
本书共分为5篇,机遇篇.做事篇.处事篇.技能篇和高级篇,从不同的层面阐述了IT运维人员 应掌握的方法及相关知识与技能.本书作者深入浅出.化繁为简,将信息化服务中晦涩的IT标准规范.流程体系用浅显易懂的 ...
- 利用if else来运行咱们结婚吧
static void Main(string[] args) { while (true) { string ...
- .net 利用 GZipStream 压缩和解压缩
1.GZipStream 类 此类在 .NET Framework 2.0 版中是新增的. 提供用于压缩和解压缩流的方法和属性 2.压缩byte[] /// <summary> /// 压 ...
- HDU-- Buy Tickets
告知每次要插到第 i 个位置上,问最后它们的顺序是什么. 这一题,不是考线段树,是考如何想出用线段树...思维很巧妙,倒过来做的话就能确定此人所在的位置.... Buy Tickets Time ...