@@ -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