diff --git a/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java b/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java index 7bac4eb..c4b4380 100644 --- a/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java +++ b/src/main/java/com/tencent/msdk/dns/core/cache/Cache.java @@ -39,16 +39,17 @@ public static Cache getInstance() { public void readFromDb() { if (getCachedIpEnable()) { List allCache = cacheDbHelper.getAll(); - ArrayList expired = new ArrayList<>(); + ArrayList expiredList = new ArrayList<>(); for (LookupCache lookupCache : allCache) { mHostnameIpsMap.put(lookupCache.hostname, lookupCache.lookupResult); if (lookupCache.isExpired()) { - expired.add(lookupCache); + expiredList.add(lookupCache.hostname); } } + String [] expiredHosts = expiredList.toArray(new String[expiredList.size()]); // 内存读取后,清空本地已过期的缓存 - cacheDbHelper.deleteLookupCaches(expired); + cacheDbHelper.delete(expiredHosts); } } @@ -88,7 +89,7 @@ public void add(String hostname, LookupResult lookupResult) { DnsLog.d("Cache %s for %s", lookupResult, hostname); mHostnameIpsMap.put(hostname, lookupResult); if (getCachedIpEnable()) { - cacheDbHelper.insertLookupCache(new LookupCache(hostname, lookupResult)); + cacheDbHelper.insert(new LookupCache(hostname, lookupResult)); } } diff --git a/src/main/java/com/tencent/msdk/dns/core/cache/CacheDbHelper.java b/src/main/java/com/tencent/msdk/dns/core/cache/database/CacheDbHelper.java similarity index 75% rename from src/main/java/com/tencent/msdk/dns/core/cache/CacheDbHelper.java rename to src/main/java/com/tencent/msdk/dns/core/cache/database/CacheDbHelper.java index 0bf092a..53180ea 100644 --- a/src/main/java/com/tencent/msdk/dns/core/cache/CacheDbHelper.java +++ b/src/main/java/com/tencent/msdk/dns/core/cache/database/CacheDbHelper.java @@ -1,26 +1,26 @@ -package com.tencent.msdk.dns.core.cache; +package com.tencent.msdk.dns.core.cache.database; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.text.TextUtils; import com.tencent.msdk.dns.base.log.DnsLog; -import com.tencent.msdk.dns.core.cache.database.LookupCache; -import com.tencent.msdk.dns.core.cache.database.LookupResultConverter; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class CacheDbHelper extends SQLiteOpenHelper { public static final int DATABASE_VERSION = 1; - public static final String DATABASE_NAME = "LookupCache.db"; + public static final String DATABASE_NAME = "LookupResult.db"; - private SQLiteDatabase mDb; + private static final Object mLock = new Object(); - private final Object mLock = new Object(); + private SQLiteDatabase mDb; static class DB { static final String TABLE_NAME = "lookupDB"; @@ -42,19 +42,18 @@ public CacheDbHelper(Context context) { private SQLiteDatabase getDb() { if (mDb == null) { - try{ + try { mDb = getWritableDatabase(); - } catch (Exception e){ + } catch (Exception e) { } } return mDb; } - public List readFromDb() { + public List getAll() { synchronized (mLock) { ArrayList lists = new ArrayList<>(); - SQLiteDatabase db = null; Cursor cursor = null; @@ -64,7 +63,6 @@ public List readFromDb() { if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); do { - DnsLog.d("testt----" + cursor.getString(cursor.getColumnIndex(DB.HOST))+ "result ---" + LookupResultConverter.toLookupResult(cursor.getBlob(cursor.getColumnIndex(DB.RESULT)))); lists.add(new LookupCache( cursor.getString(cursor.getColumnIndex(DB.HOST)), LookupResultConverter.toLookupResult(cursor.getBlob(cursor.getColumnIndex(DB.RESULT))) @@ -85,7 +83,7 @@ public List readFromDb() { } } - public void insertLookupCache(LookupCache lookupCache) { + public void insert(LookupCache lookupCache) { synchronized (mLock) { SQLiteDatabase db = null; try { @@ -94,7 +92,7 @@ public void insertLookupCache(LookupCache lookupCache) { ContentValues cv = new ContentValues(); cv.put(DB.HOST, lookupCache.hostname); cv.put(DB.RESULT, LookupResultConverter.fromLookupResult(lookupCache.lookupResult)); - db.insert(DB.TABLE_NAME, null, cv); + db.insertWithOnConflict(DB.TABLE_NAME, null, cv, SQLiteDatabase.CONFLICT_REPLACE); db.setTransactionSuccessful(); } catch (Exception e) { DnsLog.e("insert lookupCache fail " + e); @@ -109,27 +107,29 @@ public void insertLookupCache(LookupCache lookupCache) { } } - public void deleteLookupCaches(ArrayList lookupCaches) { - for(LookupCache lookupCache: lookupCaches) { - delete(lookupCache.hostname); - } + public void delete(String hostname) { + delete(new String[]{hostname}); } - public void delete(String hostname) { - synchronized (mLock) { - SQLiteDatabase db = null; - try { - db = getDb(); - db.beginTransaction(); - db.delete(DB.TABLE_NAME, DB.HOST + "= ? ", new String[] {hostname}); - db.setTransactionSuccessful(); - } catch (Exception e) { - DnsLog.e("delete by hostname fail" + e); - } finally { - if (db != null) { - try { - db.endTransaction(); - } catch (Exception ignored) { + public void delete(String[] hosts) { + if (hosts.length > 0) { + synchronized (mLock) { + SQLiteDatabase db = null; + try { + db = getDb(); + db.beginTransaction(); + db.delete(DB.TABLE_NAME, DB.HOST + " IN (" + TextUtils.join(",", Collections.nCopies(hosts.length, + "?")) + ")", + hosts); + db.setTransactionSuccessful(); + } catch (Exception e) { + DnsLog.e("delete by hostname fail" + e); + } finally { + if (db != null) { + try { + db.endTransaction(); + } catch (Exception ignored) { + } } } } @@ -161,7 +161,7 @@ public void clear() { public void onCreate(SQLiteDatabase db) { try { db.execSQL(DB.SQL_CREATE_ENTRIES); - }catch (Exception e) { + } catch (Exception e) { DnsLog.e("create db fail " + e); } } diff --git a/src/main/java/com/tencent/msdk/dns/core/cache/database/LookupCache.java b/src/main/java/com/tencent/msdk/dns/core/cache/database/LookupCache.java index 881aff1..d81a612 100644 --- a/src/main/java/com/tencent/msdk/dns/core/cache/database/LookupCache.java +++ b/src/main/java/com/tencent/msdk/dns/core/cache/database/LookupCache.java @@ -17,9 +17,6 @@ public LookupCache(@NonNull String mHostname, LookupResult mLookupResult) { this.lookupResult = mLookupResult; } - public LookupCache() { - } - public boolean isExpired() { AbsRestDns.Statistics stat = (AbsRestDns.Statistics) lookupResult.stat; if (stat != null) { diff --git a/src/main/java/com/tencent/msdk/dns/report/Session.java b/src/main/java/com/tencent/msdk/dns/report/Session.java index b132df6..962e909 100644 --- a/src/main/java/com/tencent/msdk/dns/report/Session.java +++ b/src/main/java/com/tencent/msdk/dns/report/Session.java @@ -1,7 +1,5 @@ package com.tencent.msdk.dns.report; -import com.tencent.msdk.dns.base.log.DnsLog; - import java.util.Random; public class Session { @@ -15,7 +13,6 @@ public static void setSessionId() { buf[i] = sampleAlphabet.charAt(random.nextInt(sampleAlphabet.length())); } sessionId = new String(buf); - DnsLog.d("hello sessionId: " + sessionId); } public static String getSessionId() {