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

2016-12-28 21:50:11 +08:00
 goodryb

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

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

2821 次点击
所在节点    程序员
3 条回复
lhbc
2016-12-28 22:26:22 +08:00
有那么复杂吗?

dig @127.0.0.1 taobao.com
1 msec 内 为缓存
goodryb
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
2016-12-28 22:46:15 +08:00
把 /etc/resolv.conf 改成一个国外的 DNS 再测试一下,可能你现在配置的 DNS 服务器就很快,所以看不到明显的区别。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/330859

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX