Java DNS cache时效
已知Java的InetAddress会在其自己的缓存中缓存dns分析结果。
缓存策略有两种:一种是缓存已成功解析的IP地址,另一种是缓存失败状态。
这两个策略的时效配置都位于名为%JRE%libsecurityjava.security的文件中。
TTL=Time to Live
networkaddress.cache.ttl
缓存成功解析的ip,如果在缓存经过时间内再次lookup,则直接返回缓存结果。
缓存时间:
1 )如果启用了Javasecurity manager,则为持久性缓存。 (PS: jboss、tomcat等app server默认情况下未启用javasecurity manager。 资料请参照这里)
2、如果未启用,缺省为高速缓存30秒。 3 )缓存时间为0表示不缓存,-1表示永久缓存,正数表示缓存时间(秒)。
缓存时间的说明位于名为%JRE%libsecurityjava.security的文件中。
#
# the Java-levelnamelookupcachepolicyforsuccessfullookups 3360
#
# anynegativevalue : caching forever
# anypositivevalue : thenumberofsecondstocacheanaddressfor
# zero: do not cache
#
#defaultvalueisforever(forever ).For security reasons,this
# cachingismadeforeverwhenasecuritymanagerisset.whenas ecurity
# manager is not set,thedefaultbehavioristocachefor 30 seconds。
#
# note :设置thistoanythingotherthanthedefaultvaluecanhave
# serioussecurityimplications.donotsetitunless
# youaresureyouarenotexposedtodnsspoofingattack。
#
#networkaddress.cache.ttl=-1
network address.cache.negative.TTL (default :10 ) )。
如果缓存失败导致在缓存经过时间内再次lookup,则直接返回错误(减轻了DNS服务的压力)。 默认10秒。
将缓存时间设置为0表示不缓存,设置为-1表示永久缓存,正数表示缓存时间(秒)。
在名为%JRE%libsecurityjava.security的文件中也可以找到此配置:
# the Java-levelnamelookupcachepolicyforfailedlookups :
#
# anynegativevalue : cache forever
# anypositivevalue : thenumberofsecondstocachenegativelookupresults
# zero: do not cache
#
# insomemicrosoftwindowsnetworkingenvironmentsthatemploy
# thewinsnameserviceinadditiontodns,name service lookups
# thatfailmaytakeanoticeablylongtimetoreturn (approx.5 seconds )
# forthisreasonthedefaultcachingpolicyistomaintainthese
# results for 10 seconds
#
#
network address.cache.negative.TTL=10
除了更改java.security的配置以外。 也可以在启动jvm时通过添加一行-Dsun.NET.inetaddr.ttl=value参数来设置ttl。
除了这些官方提供的扩展手段外,还可以通过hack inet address的cache机制自由调整cache策略。