SettingsProvider 它SettingsCache

时间:2024-01-21 17:59:21

转载请注明出处:http://blog.csdn.net/droyon/article/details/35558437

SettingsCache,如指出,SettingsProvider缓冲。这将缓冲所有当前请求访问key值及其value。此缓冲区处在内存中,读取效率较高。

SettingsProvider支持多用户概念,每一个用户都有至少三张表(System、Secure)。每一张表都存在一个SettingsCache。

1、在手机启动时,会将SettingsProvider相应的数据库中的数据表的内容缓冲进来。

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:
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();
}
}

2、SettingsCache extends LruCache<String, Bundle>

/**
* In-memory LRU Cache of system and secure settings, along with
* associated helper functions to keep cache coherent with the
* database.
* 在内存中缓存System,Secure的设置项,以及相关功能方法来保证和数据库的一致性。 */
private boolean mCacheFullyMatchesDisk = false; // has the whole database slurped. 标记内存LRU缓冲是否清空了整个数据库
cache.evictAll();//remove全部元素,回调entryRemoved(true,key。value)

3、putIfAbsent方法

 /**
* Atomic cache population, conditional on size of value and if
* we lost a race.
*
* @returns a Bundle to send back to the client from call(), even
* if we lost the race.
*/
public Bundle putIfAbsent(String key, String value) {
Bundle bundle = (value == null) ? NULL_SETTING : Bundle.forPair("value", value);
if (value == null || value.length() <= MAX_CACHE_ENTRY_SIZE) {//假设value为null,或者value的长度小于500字符
synchronized (this) {
if (get(key) == null) {
put(key, bundle);//bundle可能:NULL_SETTINGS、Bundle.forPair("value", value)
}
}
}//【value不为null,且value的长度大于500,直接返回bundle,bundle为Bundle.forPair("value", value)】
return bundle;
}

4、populate(填充)

/**
* Populates a key in a given (possibly-null) cache.
* 填充cache中的keyword
*/
public static void populate(SettingsCache cache, ContentValues contentValues) {
if (cache == null) {
return;
}
String name = contentValues.getAsString(Settings.NameValueTable.NAME);
if (name == null) {
Log.w(TAG, "null name populating settings cache.");
return;
}
String value = contentValues.getAsString(Settings.NameValueTable.VALUE);
cache.populate(name, value);
}

5、检查反复

/**
* For suppressing duplicate/redundant settings inserts early,
* checking our cache first (but without faulting it in),
* before going to sqlite with the mutation.
* 在插入前,检查反复。 在使用sqlite前。先检查cache
* 是否为反复的值。 */
public static boolean isRedundantSetValue(SettingsCache cache, String name, String value) {
if (cache == null) return false;
synchronized (cache) {
Bundle bundle = cache.get(name);
if (bundle == null) return false;
String oldValue = bundle.getPairValue();
if (oldValue == null && value == null) return true;
if ((oldValue == null) != (value == null)) return false;//奇妙代码
return oldValue.equals(value);
}
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。