Skip to content

Commit e79859d

Browse files
author
YangSen-qn
committed
change dns cache style
1 parent 1514719 commit e79859d

File tree

3 files changed

+32
-52
lines changed

3 files changed

+32
-52
lines changed

library/src/main/java/com/qiniu/android/http/dns/DnsCacheInfo.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.qiniu.android.http.dns;
22

3+
import com.qiniu.android.utils.StringUtils;
4+
5+
import org.json.JSONArray;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
39
import java.net.InetAddress;
410
import java.util.HashMap;
511
import java.util.List;
@@ -8,12 +14,22 @@
814
/**
915
* Created by jemy on 2019/9/23.
1016
*/
11-
public class DnsCacheInfo {
17+
public class DnsCacheInfo implements java.io.Serializable {
18+
1219
public String currentTime;
1320
public String localIp;
1421
public ConcurrentHashMap<String, List<IDnsNetworkAddress>> info;
1522

16-
public DnsCacheInfo() {}
23+
public static DnsCacheInfo createDnsCacheInfoByJsonData(byte[] jsonData) {
24+
if (jsonData == null){
25+
return null;
26+
}
27+
DnsCacheInfo dnsCacheInfo = (DnsCacheInfo)StringUtils.toObject(jsonData);
28+
return dnsCacheInfo;
29+
}
30+
31+
public DnsCacheInfo() {
32+
}
1733

1834
public DnsCacheInfo(String currentTime, String localIp, ConcurrentHashMap<String, List<IDnsNetworkAddress>> info) {
1935
this.currentTime = currentTime;
@@ -49,6 +65,10 @@ public String cacheKey(){
4965
return localIp;
5066
}
5167

68+
public byte[] toJsonData(){
69+
return StringUtils.toByteArray(this);
70+
}
71+
5272
@Override
5373
public String toString() {
5474
return "{\"currentTime\":\"" + currentTime + "\", \"localIp\":\"" + localIp + "\"}";

library/src/main/java/com/qiniu/android/http/dns/DnsNetworkAddress.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.json.JSONException;
44
import org.json.JSONObject;
55

6-
class DnsNetworkAddress implements IDnsNetworkAddress{
6+
class DnsNetworkAddress implements IDnsNetworkAddress, java.io.Serializable {
77

88
private final String hostValue;
99
private final String ipValue;

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

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,6 @@ public boolean recoverCache(){
7272
return true;
7373
}
7474

75-
DnsCacheInfo cacheInfo = (DnsCacheInfo)StringUtils.toObject(data);
76-
if (cacheInfo == null){
77-
return true;
78-
}
79-
80-
setDnsCacheInfo(cacheInfo);
81-
8275
return recoverDnsCache(data);
8376
}
8477

@@ -233,29 +226,14 @@ private boolean preFetchHost(String preHost, Dns dns){
233226

234227
private boolean recoverDnsCache(byte[] data){
235228

236-
JSONObject addressInfo = null;
237-
try {
238-
addressInfo = new JSONObject(new String(data));
239-
} catch (JSONException e) {
229+
DnsCacheInfo dnsCacheInfo = DnsCacheInfo.createDnsCacheInfoByJsonData(data);
230+
if (dnsCacheInfo == null || dnsCacheInfo.info == null || dnsCacheInfo.info.size() == 0){
240231
return false;
241232
}
242233

243-
Iterator<String> hosts = addressInfo.keys();
244-
while (hosts.hasNext()){
245-
String host = hosts.next();
246-
ArrayList<IDnsNetworkAddress> addressList = new ArrayList<>();
247-
try {
248-
JSONArray addressJsonList = addressInfo.getJSONArray(host);
249-
for(int i=0; i<addressJsonList.length(); i++){
250-
JSONObject addressJson = addressJsonList.getJSONObject(i);
251-
DnsNetworkAddress address = DnsNetworkAddress.address(addressJson);
252-
addressList.add(address);
253-
}
254-
} catch (JSONException ignored) {
255-
}
256-
257-
addressDictionary.put(host, addressList);
258-
}
234+
addressDictionary.putAll(dnsCacheInfo.info);
235+
dnsCacheInfo.info = addressDictionary;
236+
setDnsCacheInfo(dnsCacheInfo);
259237

260238
return false;
261239
}
@@ -279,30 +257,12 @@ private boolean recorderDnsCache(){
279257

280258
setDnsCacheInfo(dnsCacheInfo);
281259

282-
return recorderDnsCache(recorder, dnsCacheInfo.cacheKey());
283-
}
284-
285-
private boolean recorderDnsCache(Recorder recorder, String cacheKey){
286-
287-
JSONObject addressInfo = new JSONObject();
288-
for (String key : addressDictionary.keySet()){
289-
List<IDnsNetworkAddress> addressModelList = addressDictionary.get(key);
290-
JSONArray addressJsonList = new JSONArray();
291-
292-
for (IDnsNetworkAddress address : addressModelList){
293-
if (address.getHostValue() != null && address.getIpValue() != null) {
294-
DnsNetworkAddress addressObject = (DnsNetworkAddress)address;
295-
JSONObject addressJson = addressObject.toJson();
296-
addressJsonList.put(addressJson);
297-
}
298-
}
299-
300-
try {
301-
addressInfo.put(key, addressJsonList);
302-
} catch (JSONException ignored) {}
260+
byte[] data = dnsCacheInfo.toJsonData();
261+
if (data == null){
262+
return false;
303263
}
264+
recorder.set(dnsCacheInfo.cacheKey(), data);
304265

305-
recorder.set(cacheKey, addressInfo.toString().getBytes());
306266
return true;
307267
}
308268

0 commit comments

Comments
 (0)