Android DNS缓存时长的探索

这段时间在研究Android DNS缓存机制,其中有个小点就是关于DNS缓存的时长,就在这里记录一下了

Android的DNS缓存机制是双重的:

  1. Java层的快速缓存
  2. Native C层的缓存

1,Java层

先来看Java层的快速缓存,应用层如果想要解析DNS,基本上都是调用InetAddress.getByName(String host)接口,最终调用的就是lookupHostByName:

private static InetAddress[] lookupHostByName(String host, int netId)
        throws UnknownHostException {
    BlockGuard.getThreadPolicy().onNetwork();
    // Do we have a result cached?
    Object cachedResult = addressCache.get(host, netId);
    if (cachedResult != null) {
        if (cachedResult instanceof InetAddress[]) {
            // A cached positive result.
            return (InetAddress[]) cachedResult;
        } else {
            // A cached negative result.
            throw new UnknownHostException((String) cachedResult);
        }
    }
    ......
}
public Object get(String hostname, int netId) {
    AddressCacheEntry entry = cache.get(new AddressCacheKey(hostname, netId));
    // Do we have a valid cache entry?
    if (entry != null && entry.expiryNanos >= System.nanoTime()) {
        return entry.value;
    }
    // Either we didn't find anything, or it had expired.
    // No need to remove expired entries: the calle
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值