[Android]Volley源代码分析(二)Cache
Cache作为Volley最为核心的一部分,Volley花了重彩来实现它。本章我们顺着Volley的源代码思路往下,来看下Volley对Cache的处理逻辑。
我们回忆一下昨天的简单代码,我们的入口是从构造一个Request队列開始的,而我们并不直接调用new来构造,而是将控制权反转给Volley这个静态工厂来构造。
com.android.volley.toolbox.Volley:
public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR); String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
} if (stack == null) {
if (Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
} Network network = new BasicNetwork(stack); RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
queue.start(); return queue;
}
參数HttpStack用于制定你的HttpStack实现机制,比方是採用apache的http-client还是HttpUrlConnection.当然假设你不指定,Volley也会依据你的sdk版本号给出不同的策略。
而这样的HttpStack对象被Network对象包装起来。
上一节我们说过,为了构造平台统一的网络调用,Volley通过桥接的方式来实现网络调用,而桥接的接口就是这个Network.
Volley的核心在于Cache和Network。
既然两个对象已经构造完了,我们就能够生成request队列RequestQueue.可是,为什么要开启queue.start呢?我们先看一下这个代码:
public void start() {
stop(); // Make sure any currently running dispatchers are stopped.
// Create the cache dispatcher and start it.
mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
mCacheDispatcher.start(); // Create network dispatchers (and corresponding threads) up to the pool size.
for (int i = 0; i < mDispatchers.length; i++) {
NetworkDispatcher networkDispatcher = new NetworkDispatcher(mNetworkQueue, mNetwork,
mCache, mDelivery);
mDispatchers[i] = networkDispatcher;
networkDispatcher.start();
}
}
上一节体系结构我们已经说了,Volley採用生产者和消费者的模式来产生反应堆,而这中反应必需要通过线程的方式来实现。调用了RequestQueue的start之后,将开启一个Cache线程和一定数量的Network线程池。我们看到networkDispatcher的线程池数量由数组mDispatchers指定。而mDispatchers的赋值在RequestQueue的<init>中:
public RequestQueue(Cache cache, Network network, int threadPoolSize,
ResponseDelivery delivery) {
mCache = cache;
mNetwork = network;
mDispatchers = new NetworkDispatcher[threadPoolSize];
mDelivery = delivery;
}
如何?是不是认为Volley的代码写的很的浅显合理。好了,到RequestQueue.start開始,我们已经为我们的request构建好了它的上下文环境,我们接着仅仅须要将它add到这个队列中来就能够了;
public <T> Request<T> add(Request<T> request) {
// Tag the request as belonging to this queue and add it to the set of current requests.
request.setRequestQueue(this);
synchronized (mCurrentRequests) {
mCurrentRequests.add(request);
} // Process requests in the order they are added.
request.setSequence(getSequenceNumber());
request.addMarker("add-to-queue"); // If the request is uncacheable, skip the cache queue and go straight to the network.
if (!request.shouldCache()) {
mNetworkQueue.add(request);
return request;
} // Insert request into stage if there's already a request with the same cache key in flight.
synchronized (mWaitingRequests) {
String cacheKey = request.getCacheKey();
System.out.println("request.cacheKey = "+(cacheKey));
if (mWaitingRequests.containsKey(cacheKey)) {
// There is already a request in flight. Queue up.
<span style="color:#33cc00;"> Queue<Request<?>> stagedRequests = mWaitingRequests.get(cacheKey);
if (stagedRequests == null) {
stagedRequests = new LinkedList<Request<? >>();
}
stagedRequests.add(request);
mWaitingRequests.put(cacheKey, stagedRequests);</span>
} else {
// Insert 'null' queue for this cacheKey, indicating there is now a request in
// flight.
mWaitingRequests.put(cacheKey, null);
mCacheQueue.add(request);
}
return request;
}
}
这段代码绿色的部分是点睛之笔,有了暂存的概念,避免了反复的请求。我们add一个Request的时候,须要设置上这个RequestQueue。目的是为了结束的时候将自己从Queue中回收。我们这里还能够看到一个简单的状态机:
request.addMarker("add-to-queue");
这种方法将在request不同的上下文中调用。方便以后查错。之后Request会检查是否须要进行Cache
if (!request.shouldCache()) {
mNetworkQueue.add(request);
return request;
}
我们的观念里面,似乎文本数据是不须要Cache的,你能够通过这种方法来实现是否要cache住你的东西,当然不限制你的数据类型。
之后,假设你的请求不被暂存的话,那就被投入Cache反应堆。
我们来看下mCacheQueue这个对象:
private final PriorityBlockingQueue<Request<?>> mCacheQueue =
new PriorityBlockingQueue<Request<?>>();
我们看到mCacheQueue本质是一个PriorityBlockingQueue的线程安全队列,并且在这个队列里面是能够进行优先级比較。ImageRequest对Request的优先级进行了指定:
com.android.volley.toolbox.ImageRequest:
@Override
public Priority getPriority() {
return Priority.LOW;
}
你能够自己指定你的Request的优先级别.我们回到CacheDispatcher消费者,CacheDispatcher继承Thread。生成之后直接对Cache初始化mCache.initialize();初始化的目的在于获得Cache中已经存在的数据。Cache的实现类是DiskBasedCache.java我们来看下它怎样实现的初始化:
@Override
public synchronized void initialize() {
if (!mRootDirectory.exists()) {
if (!mRootDirectory.mkdirs()) {
VolleyLog.e("Unable to create cache dir %s", mRootDirectory.getAbsolutePath());
}
return;
} File[] files = mRootDirectory.listFiles();
if (files == null) {
return;
}
for (File file : files) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
CacheHeader entry = CacheHeader.readHeader(fis);
entry.size = file.length();
putEntry(entry.key, entry);
} catch (IOException e) {
if (file != null) {
file.delete();
}
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ignored) { }
}
}
}
我们能够看出,Volley差别于其它Cache的还有一个特点,就是存储元数据,或者说自己定义了数据格式。
在文件的头部添加了Volley的文件头。这样的做法不仅能从某方面保证了数据的安全性,也能非常好的存储数据元。
public static CacheHeader readHeader(InputStream is) throws IOException {
CacheHeader entry = new CacheHeader();
int magic = readInt(is);
if (magic != CACHE_MAGIC) {
// don't bother deleting, it'll get pruned eventually
throw new IOException();
}
entry.key = readString(is);
entry.etag = readString(is);
if (entry.etag.equals("")) {
entry.etag = null;
}
entry.serverDate = readLong(is);
entry.ttl = readLong(is);
entry.softTtl = readLong(is);
entry.responseHeaders = readStringStringMap(is);
return entry;
}
我们从这段代码里面看到不少信息,Volley自定义了自己的数据魔数,也依照Volley自己的规范来读取元数据。
好的,我们初始化了Cache接下来就是CacheDispatcher的核心了。
while (true) {
try {
// Get a request from the cache triage queue, blocking until
// at least one is available.
final Request<? > request = mCacheQueue.take();
request.addMarker("cache-queue-take"); // If the request has been canceled, don't bother dispatching it.
if (request.isCanceled()) {
request.finish("cache-discard-canceled");
continue;
} // Attempt to retrieve this item from cache.
Cache.Entry entry = mCache.get(request.getCacheKey());
if (entry == null) {
request.addMarker("cache-miss");
// Cache miss; send off to the network dispatcher.
mNetworkQueue.put(request);
continue;
} // If it is completely expired, just send it to the network.
if (entry.isExpired()) {//推断是否失效
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
mNetworkQueue.put(request);
continue;
} // We have a cache hit; parse its data for delivery back to the request.
request.addMarker("cache-hit");
Response<?> response = request.parseNetworkResponse(
new NetworkResponse(entry.data, entry.responseHeaders));
request.addMarker("cache-hit-parsed"); if (!entry.refreshNeeded()) {
// Completely unexpired cache hit. Just deliver the response.
mDelivery.postResponse(request, response);
} else {
// Soft-expired cache hit. We can deliver the cached response,
// but we need to also send the request to the network for
// refreshing.
request.addMarker("cache-hit-refresh-needed");
request.setCacheEntry(entry); // Mark the response as intermediate.
response.intermediate = true; // Post the intermediate response back to the user and have
// the delivery then forward the request along to the network.
mDelivery.postResponse(request, response, new Runnable() {
@Override
public void run() {
try {
mNetworkQueue.put(request);
} catch (InterruptedException e) {
// Not much we can do about this.
}
}
});
} } catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
continue;
}
}
线程通过while true的方式进行轮询,当然因为queue是堵塞的,因此不会造成费电问题。
Cache.Entry entry = mCache.get(request.getCacheKey());获得数据的时候假设数据存在,则会将真实数据读取出来。这就是Volley的LazyLoad。
if (entry.isExpired()) {//推断是否失效
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
mNetworkQueue.put(request);
continue;
}
这段代码从时效性来推断是否进行淘汰。我们回想下刚才所示代码,request在不同的上下文中总被标记为不同的状态,这对后期维护有及其重要的意义。
同一时候,为了保证接口的统一性,CacheDispatcher将自己的结果伪装成为NetResponse。
这样对外部接口来说,不论你採用的是那种方式获得数据,对我来说都当作网络来获取,这本身也是DAO模式存在的意义之中的一个。
request.addMarker("cache-hit");
Response<?> response = request.parseNetworkResponse(
<strong><span style="color:#006600;">new NetworkResponse</span></strong>(entry.data, entry.responseHeaders));
request.addMarker("cache-hit-parsed");
request.parseNetworkResponse的目的是为了让你的request转成自己的数据对象。好了,到如今,对于Cache来说就差分发了,数据已经全然准备就绪了。我们上一讲说道Request终于会抛给Delivery对象用来异步分发,这样能有效避免分发造成的线程堵塞。我刚才说了,Cache会伪装成为Netresponse来post数据,那也就是说对于Network的处理,这些部分也是一模一样的。
因此,后一篇关于NetworkDispatcher的管理我将省略掉这些。Volley中Delivery的实现类是:
com.android.volley.ExecutorDelivery.java
public ExecutorDelivery(final Handler handler) {
// Make an Executor that just wraps the handler.
mResponsePoster = new Executor() {
@Override
public void execute(Runnable command) {
handler.post(command);
}
};
}
我们看到在它的<init>中传入了一个Handler,这个Handler假设是UI线程的Handler。那么你的线程就是在UI线程中执行,避免了你自己post UI线程消息的问题。post出来数据将被封装成为ResponseDeliveryRunnable 命令。
这样的命令跑在Handler所在的线程中.到此CacheDispatcher的基本流程就结束了,ResponseDeliveryRunnable中除了分发以外也会进行一些收尾的工作,看官们能够自己阅读。
[Android]Volley源代码分析(二)Cache的更多相关文章
- [Android] Volley源代码分析(五岁以下儿童)Q \\ u0026一个
Volley源代码分析系列那里一段时间,告诉我,有许多私人留言,同时一些问题抛出.对于一些简单的问题,我们跳,这两天被连接到朋友@smali提出的问题.告诉我你不得不赞叹查看源代码时的详细程度,大家一 ...
- [Android]Volley源代码分析(叁)Network
假设各位看官细致看过我之前的文章,实际上Network这块的仅仅是点小功能的补充.我们来看下NetworkDispatcher的核心处理逻辑: <span style="font-si ...
- [Android]Volley源代码分析(店)应用
通过前面的谈话,我相信你有Volley有了一定的了解了原理.本章将给出一些我们的应用程序都可以在样品中直接使用,第一样品是 NetworkImageView类,事实上NetworkImageView顾 ...
- [Android]Fragment源代码分析(二) 状态
我们上一讲,抛出来一个问题,就是当Activity的onCreateView的时候,是怎样构造Fragment中的View參数.要回答这个问题我们先要了解Fragment的状态,这是Fragment管 ...
- Android HttpURLConnection源代码分析
Android HttpURLConnection源代码分析 之前写过HttpURLConnection与HttpClient的差别及选择.后来又分析了Volley的源代码. 近期又遇到了问题,想在V ...
- Android 消息处理源代码分析(1)
Android 消息处理源代码分析(1) 在Android中,通常被使用的消息队列的代码在文件夹\sources\android-22\android\os下,涉及到下面几个类文件 Handler.j ...
- Android HandlerThread 源代码分析
HandlerThread 简单介绍: 我们知道Thread线程是一次性消费品,当Thread线程运行完一个耗时的任务之后.线程就会被自己主动销毁了.假设此时我又有一 个耗时任务须要运行,我们不得不又 ...
- Android KLog源代码分析
Android KLog源代码分析 Android KLog源代码分析 代码结构 详细分析 BaseLog FileLog JsonLog XmlLog 核心文件KLogjava分析 遇到的问题 一直 ...
- android开发源代码分析--多个activity调用多个jni库的方法
android开发源代码分析--多个activity调用多个jni库的方法 有时候,我们在开发android项目时会遇到须要调用多个native c/jni库文件,下面是本人以前实现过的方法,假设有知 ...
随机推荐
- servlet(2) - 利用MyEclipse新建一个servlet - 小易Java笔记
1.Tomcat在MyEclipse中集成 ==> Window-preferences-MyEclipse-Servers-Tomcat-Tomcat 6.x-点击右侧的Browse,选择你的 ...
- golang命令行参数解析
package main import ( "fmt" "os" ) func main(){ s:= os.Args fmt.Println(s) } 直接执 ...
- 6.memcached缓存系统
1.memcached的安装和参数 memcached缓存系统一般还是部署在linux服务器上,所以这里只介绍linux上memcache的安装 首先切换到root用户,然后apt-get insta ...
- [ python ] 使用sys模块实现进度条
在写网络IO传输的时候, 有时候需要进度条来显示当前传输进度,使用 sys 模块就可以实现: sys.stdout.write() 这个函数在在控制台输出字符串不会带任何结尾,这就意味着这个输出还没有 ...
- selenium java 读取xml (数据驱动)
selenium 数据驱动 (xml解析) getElementByTagName()可以通过标签名获取某个标签.它所获取的对象是以数组形式存放.如“caption”和“item”标签在info.xm ...
- Selenium2+python自动化55-unittest之装饰器(@classmethod)【转载】
前言 前面讲到unittest里面setUp可以在每次执行用例前执行,这样有效的减少了代码量,但是有个弊端,比如打开浏览器操作,每次执行用例时候都会重新打开,这样就会浪费很多时间. 于是就想是不是可以 ...
- Java语言中的协变和逆变(zz)
转载声明: 本文转载至:http://swiftlet.net/archives/1950 协变和逆变指的是宽类型和窄类型在某种情况下的替换或交换的特性.简单的说,协变就是用一个窄类型替代宽类型,而逆 ...
- 深入理解Java的注解(Annotation):注解处理器(3)
如果没有用来读取注解的方法和工作,那么注解也就不会比注释更有用处了.使用注解的过程中,很重要的一部分就是创建于使用注解处理器.Java SE5扩展了反射机制的API,以帮助程序员快速的构造自定义注解处 ...
- JAVA集合操作的利器:CollectionUtils
使用 CollectionUtils 中四个方法之一执行集合操作.这四种分别是 union(),intersection();disjunction(); subtract(); 下列例子就是演示了如 ...
- Python3 字典(map)
ayout: post title: Python3 字典(map) author: "luowentaoaa" catalog: true tags: mathjax: true ...