转载请注明出处:http://blog.csdn.net/droyon/article/details/35558595
1、总结:每一个表都有一个SettingsCache,例如:sSystemCaches.get(userHandle)、sSecureCaches.get(userHandler),然后检索system、secure表中内容,将这写内容写入cache中,最多无限制
class CachePrefetchThread extends Thread { private int mUserHandle; CachePrefetchThread(int userHandle) { super("populate-settings-caches"); mUserHandle = userHandle; } @Override public void run() { fullyPopulateCaches(mUserHandle); } }2、重要的成员变量
// Each defined user has their own settings protected final SparseArray<DatabaseHelper> mOpenHelpers = new SparseArray<DatabaseHelper>(); private static final SparseArray<SettingsCache> sSystemCaches = new SparseArray<SettingsCache>(); private static final SparseArray<SettingsCache> sSecureCaches = new SparseArray<SettingsCache>(); private static final SettingsCache sGlobalCache = new SettingsCache(TABLE_GLOBAL);
private void fullyPopulateCaches(final int userHandle) { DatabaseHelper dbHelper = mOpenHelpers.get(userHandle); // Only populate the globals cache once, for the owning user if (userHandle == UserHandle.USER_OWNER) { fullyPopulateCache(dbHelper, TABLE_GLOBAL, sGlobalCache); } fullyPopulateCache(dbHelper, TABLE_SECURE, sSecureCaches.get(userHandle)); fullyPopulateCache(dbHelper, TABLE_SYSTEM, sSystemCaches.get(userHandle)); }
// Slurp all values (if sane in number & size) into cache. private void fullyPopulateCache(DatabaseHelper dbHelper, String table, SettingsCache cache) { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor c = db.query( table, new String[] { Settings.NameValueTable.NAME, Settings.NameValueTable.VALUE }, null, null, null, null, null, "" + (MAX_CACHE_ENTRIES + 1) /* limit */); try { synchronized (cache) { cache.evictAll(); cache.setFullyMatchesDisk(true); // optimistic int rows = 0; while (c.moveToNext()) {//一直在插入,有多少,缓冲多少 rows++; String name = c.getString(0); String value = c.getString(1); cache.populate(name, value); } if (rows > MAX_CACHE_ENTRIES) { // Somewhat redundant, as removeEldestEntry() will // have already done this, but to be explicit: //有些多余,因为 removeEldestEntry() 会已经这样做,但是要做到明确: cache.setFullyMatchesDisk(false); Log.d(TAG, "row count exceeds max cache entries for table " + table); } if (LOCAL_LOGV) Log.d(TAG, "cache for settings table '" + table + "' rows=" + rows + "; fullycached=" + cache.fullyMatchesDisk()); } } finally { c.close(); } }