码迷,mamicode.com
首页 > 其他好文 > 详细

SettingsProvider 之SettingsCache

时间:2014-06-30 07:52:41      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:settingscache   settingsprovider   

转载请注明出处: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中的关键字
         */
        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);
            }
        }


SettingsProvider 之SettingsCache,布布扣,bubuko.com

SettingsProvider 之SettingsCache

标签:settingscache   settingsprovider   

原文地址:http://blog.csdn.net/droyon/article/details/35558437

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!