V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
goodryb
V2EX  ›  程序员

如何验证 localhost 上的 DNS 缓存已经生效了?

  •  
  •   goodryb · 2016-12-28 21:50:11 +08:00 · 2802 次点击
    这是一个创建于 2681 天前的主题,其中的信息可能已经有所发展或是发生改变。

    linux 上可以用 nscd 做 DNS 缓存,想验证一下效果,就写了个简单的脚本

    dns.py

    import socket
    i = 0;
    while i < 1000:
       	i+=1;
       	ip = socket.gethostbyname("taobao.com")
    

    然后通过 time python dns.py 来执行

    [root@host ~]# time python dns.py
    
    real   	0m2.350s
    user   	0m0.045s
    sys    	0m0.058s
    [root@host ~]# service nscd start
    Starting nscd:                                             [  OK  ]
    [root@host ~]# time python dns.py
    
    real   	0m2.453s
    user   	0m0.040s
    sys    	0m0.057s
    

    nscd 的配置如下

    [root@host ~]# cat /etc/nscd.conf
    #logfile        /var/log/nscd.log
    threads         6
    max-threads     128
    server-user     nscd
    debug-level     5
    paranoia        no
    enable-cache    passwd      no
    enable-cache    group       no
    enable-cache    hosts       yes
    positive-time-to-live   hosts   5
    negative-time-to-live   hosts       20
    suggested-size  hosts       211
    check-files     hosts       yes
    persistent      hosts       yes
    shared          hosts       no
    max-db-size     hosts       33554432
    

    对比下来,好像没有起到什么效果

    3 条回复    2016-12-28 22:46:15 +08:00
    lhbc
        1
    lhbc  
       2016-12-28 22:26:22 +08:00
    有那么复杂吗?

    dig @127.0.0.1 taobao.com
    1 msec 内 为缓存
    goodryb
        2
    goodryb  
    OP
       2016-12-28 22:30:49 +08:00
    @lhbc 好像不行
    [root@host ~]# dig @127.0.0.1 taobao.com

    ; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.23.rc1.el6_5.1 <<>> @127.0.0.1 taobao.com
    ; (1 server found)
    ;; global options: +cmd
    ;; connection timed out; no servers could be reached
    XiaoxiaoPu
        3
    XiaoxiaoPu  
       2016-12-28 22:46:15 +08:00
    把 /etc/resolv.conf 改成一个国外的 DNS 再测试一下,可能你现在配置的 DNS 服务器就很快,所以看不到明显的区别。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2044 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 00:18 · PVG 08:18 · LAX 17:18 · JFK 20:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.