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 = 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的更多相关文章
- Winform开发框架之参数配置管理功能实现-基于SettingsProvider.net的构建
在较早时期,我写过一篇文章<结合Control.FirefoxDialog控件,构造优秀的参数配置管理模块>,介绍过在我的Winform框架基础上集成的参数配置模块功能,但是参数模块的配置 ...
- SettingsProvider该CRUD
转载请注明出处:http://blog.csdn.net/droyon/article/details/35558697 什么时候delete要么update时间.需要清空缓存并重新加载数据. 1.i ...
- Android默认输入法语言的修改以及SettingsProvider作用
Android源码中默认的有三种输入法:英文,中文,日文.对应的工程代码路径为:<android_root>/packages/inputmethods/LatinIME/<andr ...
- External Configuration Store Pattern 外部配置存储模式
Move configuration information out of the application deployment package to a centralized location. ...
- c#面试题汇总
下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化,不要梦想着把题覆盖了,下面的题是供大家查漏补缺用的,真正的把这些题搞懂了,才能“以不变应万变”.回答问题的时候能联系做过项目的例子是最好 ...
- .NET工程师面试宝典
.Net工程师面试笔试宝典 传智播客.Net培训班内部资料 这套面试笔试宝典是传智播客在多年的教学和学生就业指导过程中积累下来的宝贵资料,大部分来自于学员从面试现场带过来的真实笔试面试题,覆盖了主流的 ...
- 传智播客DotNet面试题
技术类面试.笔试题汇总(整理者:杨中科,部分内容从互联网中整理而来) 注:标明*的问题属于选择性掌握的内容,能掌握更好,没掌握也没关系. 下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化 ...
- 【转】VS2010安装后出现无法使用帮助的解决方案
安装完VS2010后 再安装MSDN 打开“Help Library 管理器 - Microsoft Help 查看器 1.0” 提示“请为本地内容选择位置” 默认的位置是在“C:/Documents ...
- Interview
下面的题是供大家查漏补缺用的,真正的把这些题搞懂了,才能"以不变应万变". 回答问题的时候能联系做过项目的例子是最好的,有的问题后面我已经补充联系到项目中的对应的案例了. 1.简述 ...
随机推荐
- 【原创】leetCodeOj --- Fraction to Recurring Decimal 解题报告
原题地址: https://oj.leetcode.com/problems/fraction-to-recurring-decimal/ 题目内容: Given two integers repre ...
- strchr,wcschr 和strrchr, wcsrchr,_tcschr,_tcsrchr功能
strchr,wcschr 和strrchr, wcsrchr,_tcschr,_tcsrchr功能 (1) char *strchr( const char *string, int ...
- Cocos2d-x学习笔记(五岁以下儿童) 精灵两种方式播放动画
这几天在看控件类,临时没有想好实际运用的方向.单纯的创建网上已经有非常多这方面的样例,我就不写了.接下来是学习精灵类.精灵类若是单独学习也是非常easy.于是我加了一些有关动画方面的知识点与精灵 ...
- Linux在简短而经常使用的命令
Linux组成: 内核:的心脏.是执行程序和管理像磁盘和打印机等硬件设备的核心程序. shell:是系统的用户界面,提供了用户和内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行.是 ...
- 左右PHP自增力、神秘递减操作
首先看一个面试题: $a = 1; $b = &$a; if ($b == $a++) echo "true"; else echo "false"; ...
- 代码的未来读书笔记<二>
代码的未来读书笔记<二> 3.1语言的设计 对Ruby JavaScript Java Go 从服务端client以及静态动态这2个角度进行了对照. 这四种语言因为不同的设计方针,产生了不 ...
- 关于WCF的引用,添加服务和添加web服务的区别
原文:关于WCF的引用,添加服务和添加web服务的区别 本章内容主要是根据我做的实验来阐述这2种添加服务针对WCF的不同之处,我们按照示例一步一步来看. 如下是工程的结构: 该WCF服务是通过控制台程 ...
- zabbix 实现curl 显示器
1.进入Configure->Templates 2. 新建一个模板 3.新建模板,并保存 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGFpND ...
- Linux之父:除了写内核代码 别的真不会(转)
Linus Torvalds 是 Linux 之父,被誉为活着的传奇. 其实很多人不了解他也并不奇怪,因为人是在是太低调了.现年 46 岁的他每天的工作仍然是编程,领导并推动着 Linux 的发展. ...
- 7 JavaScript Basics Many Developers Aren't Using (Properly)【转】
JavaScript, at its base, is a simple language that we continue to evolve with intelligent, flexible ...