@@ -36,6 +36,7 @@ public class DnsPrefetcher {
3636
3737 private static ConcurrentHashMap <String , List <InetAddress >> mConcurrentHashMap = new ConcurrentHashMap <String , List <InetAddress >>();
3838 private static List <String > mHosts = new ArrayList <String >();
39+ private static DnsCacheKey mDnsCacheKey = null ;
3940
4041 private DnsPrefetcher () {
4142
@@ -52,10 +53,32 @@ public static DnsPrefetcher getDnsPrefetcher() {
5253 return dnsPrefetcher ;
5354 }
5455
56+ /**
57+ * 不用判断是否预取,每次调用UploadManager都直接预取一次。
58+ * uploadManager只会new一次,但是uploadManager.put方法有多次,如果期间网络发生变化
59+ * 这个方法预取的结果应该被ConcurrentHashMap自动覆盖
60+ */
61+ public void localFetch () {
62+ List <String > localHosts = new ArrayList <String >();
63+ //local
64+ List <ZoneInfo > listZoneinfo = getLocalZone ();
65+ for (ZoneInfo zone : listZoneinfo ) {
66+ for (String host : zone .upDomainsList ) {
67+ localHosts .add (host );
68+ }
69+ }
70+ localHosts .add (Config .preQueryHost );
71+
72+ if (localHosts != null && localHosts .size () > 0 )
73+ preFetch (localHosts );
74+ }
75+
76+
5577 public DnsPrefetcher init (String token ) throws UnknownHostException {
5678 this .token = token ;
57- preHosts ();
58- preFetch ();
79+ List <String > preHosts = preHosts ();
80+ if (preHosts != null && preHosts .size () > 0 )
81+ preFetch (preHosts );
5982 return this ;
6083 }
6184
@@ -86,43 +109,47 @@ public List<InetAddress> getInetAddressByHost(String host) {
86109 return mConcurrentHashMap .get (host );
87110 }
88111
89- private void preHosts () {
112+ private List < String > preHosts () {
90113 HashSet <String > set = new HashSet <String >();
91-
114+ List < String > preHosts = new ArrayList <>();
92115 //preQuery sync
93116 ZoneInfo zoneInfo = getPreQueryZone ();
94117 if (zoneInfo != null ) {
95118 for (String host : zoneInfo .upDomainsList ) {
96119 if (set .add (host ))
97- mHosts .add (host );
120+ preHosts .add (host );
98121 }
99122 }
123+
100124 //local
101125 List <ZoneInfo > listZoneinfo = getLocalZone ();
102126 for (ZoneInfo zone : listZoneinfo ) {
103127 for (String host : zone .upDomainsList ) {
104128 if (set .add (host ))
105- mHosts .add (host );
129+ preHosts .add (host );
106130 }
107131 }
108132 if (set .add (Config .preQueryHost ))
109- mHosts .add (Config .preQueryHost );
133+ preHosts .add (Config .preQueryHost );
134+ return preHosts ;
110135 }
111136
112137
113- private void preFetch () {
138+ private void preFetch (List < String > fetchHost ) {
114139 List <String > rePreHosts = new ArrayList <String >();
115- for (String host : mHosts ) {
140+ for (String host : fetchHost ) {
116141 List <InetAddress > inetAddresses = null ;
117142 try {
118143 inetAddresses = okhttp3 .Dns .SYSTEM .lookup (host );
119144 mConcurrentHashMap .put (host , inetAddresses );
145+ mHosts .add (host );
120146 } catch (UnknownHostException e ) {
121147 e .printStackTrace ();
122148 rePreHosts .add (host );
123149 }
124150 }
125- rePreFetch (rePreHosts , null );
151+ if (rePreHosts .size () > 0 )
152+ rePreFetch (rePreHosts , null );
126153 }
127154
128155 /**
@@ -151,6 +178,7 @@ private boolean rePreFetch(String host, Dns customeDns) {
151178 inetAddresses = customeDns .lookup (host );
152179 }
153180 mConcurrentHashMap .put (host , inetAddresses );
181+ mHosts .add (host );
154182 return true ;
155183 } catch (UnknownHostException e ) {
156184 e .printStackTrace ();
@@ -264,6 +292,30 @@ public boolean equals(Object obj) {
264292 * @return true:重新预期并缓存, false:不需要重新预取和缓存
265293 */
266294 public static boolean checkRePrefetchDns (String token , Configuration config ) {
295+ if (mDnsCacheKey == null )
296+ return true ;
297+
298+ String currentTime = String .valueOf (System .currentTimeMillis ());
299+ String localip = AndroidNetwork .getHostIP ();
300+ String akScope = StringUtils .getAkAndScope (token );
301+
302+ if (currentTime == null || localip == null || akScope == null )
303+ return true ;
304+ long cacheTime = (Long .parseLong (currentTime ) - Long .parseLong (mDnsCacheKey .getCurrentTime ())) / 1000 ;
305+ if (!mDnsCacheKey .getLocalIp ().equals (localip ) || cacheTime > config .dnsCacheTimeMs || !mDnsCacheKey .getAkScope ().equals (akScope )) {
306+ return true ;
307+ }
308+
309+ return false ;
310+ }
311+
312+ /**
313+ * uploadManager初始化时,加载本地缓存到内存
314+ *
315+ * @param config
316+ * @return 如果不存在缓存返回true,需要重新预取;如果存在且满足使用,返回false
317+ */
318+ public static boolean recoverCache (Configuration config ) {
267319 Recorder recorder = null ;
268320 try {
269321 recorder = new DnsCacheFile (Config .dnscacheDir );
@@ -285,15 +337,14 @@ public static boolean checkRePrefetchDns(String token, Configuration config) {
285337
286338 String currentTime = String .valueOf (System .currentTimeMillis ());
287339 String localip = AndroidNetwork .getHostIP ();
288- String akScope = StringUtils .getAkAndScope (token );
289340
290- if (currentTime == null || localip == null || akScope == null )
341+ if (currentTime == null || localip == null )
291342 return true ;
292343 long cacheTime = (Long .parseLong (currentTime ) - Long .parseLong (cacheKey .getCurrentTime ())) / 1000 ;
293- if (!cacheKey .getLocalIp ().equals (localip ) || cacheTime > config .dnsCacheTimeMs || ! cacheKey . getAkScope (). equals ( akScope ) ) {
344+ if (!cacheKey .getLocalIp ().equals (localip ) || cacheTime > config .dnsCacheTimeMs ) {
294345 return true ;
295346 }
296-
347+ mDnsCacheKey = cacheKey ;
297348 return recoverDnsCache (data );
298349 }
299350
@@ -308,12 +359,16 @@ public static void startPrefetchDns(String token, Configuration config) {
308359 String akScope = StringUtils .getAkAndScope (token );
309360 if (currentTime == null || localip == null || akScope == null )
310361 return ;
311- String cacheKey = new DnsCacheKey (currentTime , localip , akScope ).toString ();
362+ DnsCacheKey dnsCacheKey = new DnsCacheKey (currentTime , localip , akScope );
363+ String cacheKey = dnsCacheKey .toString ();
364+
312365 Recorder recorder = null ;
313366 DnsPrefetcher dnsPrefetcher = null ;
314367 try {
315368 recorder = new DnsCacheFile (Config .dnscacheDir );
316369 dnsPrefetcher = DnsPrefetcher .getDnsPrefetcher ().init (token );
370+ //确认预取结束后,需要更新缓存mDnsCacheKey
371+ mDnsCacheKey = dnsCacheKey ;
317372 } catch (IOException e ) {
318373 e .printStackTrace ();
319374 return ;
@@ -352,4 +407,8 @@ public static boolean recoverDnsCache(byte[] data) {
352407 DnsPrefetcher .getDnsPrefetcher ().setHosts (list );
353408 return false ;
354409 }
410+
411+ private void setmDnsCacheKey (DnsCacheKey mDnsCacheKey ) {
412+ mDnsCacheKey = mDnsCacheKey ;
413+ }
355414}
0 commit comments