首页 > 代码库 > SettingsProvider 之SettingsCache
SettingsProvider 之SettingsCache
转载请注明出处: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 = http://www.mamicode.com/c.getString(1);>
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 =http://www.mamicode.com/= null) ? NULL_SETTING : Bundle.forPair("value", value);>
4、populate(填充)/** * Populates a key in a given (possibly-null) cache. * 填充cache中的关键字 */ 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 = http://www.mamicode.com/contentValues.getAsString(Settings.NameValueTable.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 = http://www.mamicode.com/bundle.getPairValue();>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。