在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缓存的分析的更多相关文章

  1. 好系统重装助手教你清理win7系统中DNS缓存

    在我们使用电脑的过程中,有时候一个经常用的网页突然打不开了,遇到这种情况,清理一下DNS缓存就可以解决了.如何清理DNS缓存?小编这就给大家说一种最简单的方法. 1.组合键:win+R,输入cmd,点 ...

  2. 浏览器的DNS缓存

    通过设置hosts文件可以强制指定域名对应的IP,当修改hosts文件,想要浏览器生效,最直接的方法关闭浏览器后重新开启:如果不想重启浏览器,只需要清空浏览器的DNS缓存即可.清空DNS缓存在chro ...

  3. 装饰模式和它在JDK中的实现

    对装饰者模式的一个通俗的理解就是:一个东西A包装了另外一个东西B,A在B的功能基础上又扩展了新的功能,但是对外提供的接口不变 装饰者模式(Decorator)的定义: 动态地给一个对象添加一些额外的职 ...

  4. Java动态替换InetAddress中DNS的做法简单分析1

    在java.net包描述中, 简要说明了一些关键的接口. 其中负责networking identifiers的是Addresses. 这个类的具体实现类是InetAddress, 底层封装了Inet ...

  5. Java7中的ForkJoin并发框架初探(中)——JDK中实现简要分析

    原文发表于 2013 年 8 月 28 日 由 三石 根据前文描述的Doug Lea的理论基础,在JDK1.7中已经给出了Fork Join的实现.在Java SE 7的API中,多了ForkJoin ...

  6. [转]Java7中的ForkJoin并发框架初探(中)——JDK中实现简要分析

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp85   根据前文描述的Doug Lea的理论基础,在JDK1.7中已经给 ...

  7. 智能dns或CDN应用中,如何很好的解决DNS缓存问题? - 知乎

    智能dns或CDN应用中,如何很好的解决DNS缓存问题? - 知乎 LISP路由器

  8. 更新域名解析以后,IP在cmd中ping不正确,清理DNS缓存

    1清除ARP缓存,cmd下使用命令arp -d*代替执行. 2清除NETBT,cmd下使用命令nbtstat -R代替执行. 再清除DNS缓存,cmd下使用命令ipconfig /flushdns代替 ...

  9. chrome中清除dns缓存

    chrome中清除dns缓存 http://rss.code-mire.com/item/1005.htm web开发经常要做各种host绑定的切换,firefox下有个DNS Flusher插件,但 ...

随机推荐

  1. Codeforces Round #198 (Div. 2) —— D

    昨天想了一下D题,有点思路不过感觉很麻烦,就懒得去敲了: 今天上午也想了一下,还是没有结果,看了一下官方题解,证明得很精彩: 这道题目其实就是一道裸地最大上升子序列的题: 看到这里,直接怒码···· ...

  2. POJ 2886 Who Gets the Most Candies?(反素数+线段树)

    点我看题目 题意 :n个小盆友从1到n编号按顺时针编号,然后从第k个开始出圈,他出去之后如果他手里的牌是x,如果x是正数,那下一个出圈的左手第x个,如果x是负数,那出圈的是右手第-x个,游戏中第p个离 ...

  3. PYTHON常见数据类型示例

    shoplist = ['apple', 'mango', 'carrot', 'banana'] print('I have ', len(shoplist), ' items to purchas ...

  4. WebApp触屏版网站开发要点

    所谓的触屏版网站其实也是WebApp的一种展示形式,主要是依赖HTML+CSS+Javascript这三个关键因素来实现,相比较原生客户端程序来说优点就是开发周期短.升级简单.维护成本低,因为从根本上 ...

  5. 关于java IO 过程当中同时读写的问题

    今天在写一个linux的java守护进程的时候,无意间就用到了java同时读写的功能. 看错误代码: package cn.sunchuanzhen.main; import java.io.Buff ...

  6. Beta Round #9 (酱油杯noi考后欢乐赛)最大伤害

    题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFno ...

  7. Java如何将Exception.printStackTrace()转换为String输出

    package com.test1; import java.io.PrintWriter; import java.io.StringWriter; public class T010 { /** ...

  8. CF -- 414A

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; int mai ...

  9. Linux学习笔记31——网络信息

    一 主机数据库函数 #include <netdb.h> struct hostent *gethostbyaddr(const void *addr, //地址 size_t len, ...

  10. HTML5与CSS3权威指南.pdf5

    第9章 通信API 跨文档消息传输 HTML5提供了网页文档之间互相接收与发送信息的功能,不仅同源(域+端口号)的Web网页之间可以互相通信,甚至可以实现跨域通信 想要接受从其他窗口发过来的信息,要对 ...