转载请注明出处: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);
}
}

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

SettingsProvider 它SettingsCache的更多相关文章

  1. Winform开发框架之参数配置管理功能实现-基于SettingsProvider.net的构建

    在较早时期,我写过一篇文章<结合Control.FirefoxDialog控件,构造优秀的参数配置管理模块>,介绍过在我的Winform框架基础上集成的参数配置模块功能,但是参数模块的配置 ...

  2. SettingsProvider该CRUD

    转载请注明出处:http://blog.csdn.net/droyon/article/details/35558697 什么时候delete要么update时间.需要清空缓存并重新加载数据. 1.i ...

  3. Android默认输入法语言的修改以及SettingsProvider作用

    Android源码中默认的有三种输入法:英文,中文,日文.对应的工程代码路径为:<android_root>/packages/inputmethods/LatinIME/<andr ...

  4. External Configuration Store Pattern 外部配置存储模式

    Move configuration information out of the application deployment package to a centralized location. ...

  5. c#面试题汇总

    下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化,不要梦想着把题覆盖了,下面的题是供大家查漏补缺用的,真正的把这些题搞懂了,才能“以不变应万变”.回答问题的时候能联系做过项目的例子是最好 ...

  6. .NET工程师面试宝典

    .Net工程师面试笔试宝典 传智播客.Net培训班内部资料 这套面试笔试宝典是传智播客在多年的教学和学生就业指导过程中积累下来的宝贵资料,大部分来自于学员从面试现场带过来的真实笔试面试题,覆盖了主流的 ...

  7. 传智播客DotNet面试题

    技术类面试.笔试题汇总(整理者:杨中科,部分内容从互联网中整理而来) 注:标明*的问题属于选择性掌握的内容,能掌握更好,没掌握也没关系. 下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化 ...

  8. 【转】VS2010安装后出现无法使用帮助的解决方案

    安装完VS2010后 再安装MSDN 打开“Help Library 管理器 - Microsoft Help 查看器 1.0” 提示“请为本地内容选择位置” 默认的位置是在“C:/Documents ...

  9. Interview

    下面的题是供大家查漏补缺用的,真正的把这些题搞懂了,才能"以不变应万变". 回答问题的时候能联系做过项目的例子是最好的,有的问题后面我已经补充联系到项目中的对应的案例了. 1.简述 ...

随机推荐

  1. ESFramework 开发手册(07) -- 掉线与心跳机制(转)

    虽然我们前面已经介绍完了ESFramework开发所需掌握的各种基础设施,但是还不够.想要更好地利用ESFramework这一利器,有些背景知识是我们必须要理解的.就像本文介绍的心跳机制,在严峻的In ...

  2. Asp.net获取用户名和IP

    1. 在ASP.NET中专用属性:    获取服务器电脑名:Page.Server.ManchineName    获取用户信息:Page.User 获取客户端电脑名:Page.Request.Use ...

  3. SQL查询优化——数据结构设计

    本文部分内容会涉及mysql,可能在其它数据库中并不适用. 本章节仅仅针对数据库结构设计做讨论.查询优化的其它内容待续. 数据库设计及使用是WEB开发程序猿必备的一项基础技能,在大数据量和高并发场景, ...

  4. js阻止冒泡

    js阻止冒泡 (ev || event).cancelBubble = true; 标签切换 <script type="text/javascript"> windo ...

  5. 正确使用Git Flow

    Git 在团队中的最佳实践--如何正确使用Git Flow 我们已经从SVN 切换到Git很多年了,现在几乎所有的项目都在使用Github管理, 本篇文章讲一下为什么使用Git, 以及如何在团队中正确 ...

  6. OOP思想

    OOP思想 读者朋友们大家好,我们今天这一讲就接着前面的封装继续讲解,今天就是在前面内容上面的升级,OOP思想中的继承,我们就先来解释一下继承到底是什么意思,我们在什么地方会用到继续. 继承就是,后代 ...

  7. 从零开始做UI-静电的sketch设计教室 视频教程

    全套31集目录 01-初识Sketch  http://www.ui.cn/detail/52223.html02-sketch的下载与安装  http://www.ui.cn/detail/5222 ...

  8. ti8168 eth0 启动

    ti8168 原始文件系统进去后没有网络eth0接口,为了有该接口须要配置/etc/network/interfaces 文件 详细配置例如以下(红色要配置) # /etc/network/inter ...

  9. android:强大的图像下载和缓存库Picasso

    1.Picasso一个简短的引论 Picasso它是Square该公司生产的一个强大的图像下载并缓存画廊.官方网站:http://square.github.io/picasso/ 仅仅须要一句代码就 ...

  10. ios至于理解锚

    锚点ios出现在少数地方,多数用在动画. 今天看了一部电影,以上所有关于锚,两年前锚这个概念看cocos2d当被接触的基本概念,当时我没怎么看,今天看了,刚刚好学习. 阅读blog,它是关于锚,像: ...