Skip to content

Commit e52d5ec

Browse files
author
JemyCheung
committed
v7.5.1
1 parent a054de7 commit e52d5ec

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

library/src/main/java/com/qiniu/android/http/DnsPrefetcher.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Iterator;
2424
import java.util.List;
2525
import java.util.concurrent.ConcurrentHashMap;
26+
import java.util.concurrent.atomic.AtomicReference;
2627

2728
/**
2829
* <p>
@@ -37,7 +38,7 @@ public class DnsPrefetcher {
3738

3839
private static ConcurrentHashMap<String, List<InetAddress>> mConcurrentHashMap = new ConcurrentHashMap<String, List<InetAddress>>();
3940
private static List<String> mHosts = new ArrayList<String>();
40-
private static DnsCacheKey mDnsCacheKey = null;
41+
private static AtomicReference mDnsCacheKey = new AtomicReference();
4142

4243
private DnsPrefetcher() {
4344

@@ -307,8 +308,11 @@ public static boolean checkRePrefetchDns(String token, Configuration config) {
307308

308309
if (currentTime == null || localip == null || akScope == null)
309310
return true;
310-
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(mDnsCacheKey.getCurrentTime())) / 1000;
311-
if (!mDnsCacheKey.getLocalIp().equals(localip) || cacheTime > config.dnsCacheTimeMs || !mDnsCacheKey.getAkScope().equals(akScope)) {
311+
DnsCacheKey dnsCacheKey = (DnsCacheKey) mDnsCacheKey.get();
312+
if (dnsCacheKey == null || dnsCacheKey.getCurrentTime() == null)
313+
return true;
314+
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(dnsCacheKey.getCurrentTime())) / 1000;
315+
if (!localip.equals(dnsCacheKey.getLocalIp()) || cacheTime > config.dnsCacheTimeMs || !akScope.equals(dnsCacheKey.getAkScope())) {
312316
return true;
313317
}
314318

@@ -350,7 +354,7 @@ public static boolean recoverCache(Configuration config) {
350354
if (!cacheKey.getLocalIp().equals(localip) || cacheTime > config.dnsCacheTimeMs) {
351355
return true;
352356
}
353-
mDnsCacheKey = cacheKey;
357+
mDnsCacheKey.set(cacheKey);
354358
return recoverDnsCache(data);
355359
}
356360

@@ -374,7 +378,7 @@ public static void startPrefetchDns(String token, Configuration config) {
374378
recorder = new DnsCacheFile(Config.dnscacheDir);
375379
dnsPrefetcher = DnsPrefetcher.getDnsPrefetcher().init(token, config);
376380
//确认预取结束后,需要更新缓存mDnsCacheKey
377-
mDnsCacheKey = dnsCacheKey;
381+
mDnsCacheKey.set(dnsCacheKey);
378382
} catch (IOException e) {
379383
e.printStackTrace();
380384
return;

0 commit comments

Comments
 (0)