首页 > 代码库 > 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();>