博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
volley6--CacheDispatcher从缓存中获取数据
阅读量:5995 次
发布时间:2019-06-20

本文共 6870 字,大约阅读时间需要 22 分钟。

源码:

1 /*  2  * Copyright (C) 2011 The Android Open Source Project  3  *  4  * Licensed under the Apache License, Version 2.0 (the "License");  5  * you may not use this file except in compliance with the License.  6  * You may obtain a copy of the License at  7  *  8  *      http://www.apache.org/licenses/LICENSE-2.0  9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.android.volley; 18  19 import android.os.Process; 20  21 import java.util.concurrent.BlockingQueue; 22  23 /** 24  * Provides a thread for performing cache triage on a queue of requests. 25  * 26  * Requests added to the specified cache queue are resolved from cache. 27  * Any deliverable response is posted back to the caller via a 28  * {@link ResponseDelivery}.  Cache misses and responses that require 29  * refresh are enqueued on the specified network queue for processing 30  * by a {@link NetworkDispatcher}. 31  */ 32 public class CacheDispatcher extends Thread { 33  34     private static final boolean DEBUG = VolleyLog.DEBUG; 35  36     /** The queue of requests coming in for triage. */ 37     private final BlockingQueue
> mCacheQueue; 38 39 /** The queue of requests going out to the network. */ 40 private final BlockingQueue
> mNetworkQueue; 41 42 /** The cache to read from. */ 43 private final Cache mCache; 44 45 /** For posting responses. */ 46 private final ResponseDelivery mDelivery; 47 48 /** Used for telling us to die. */ 49 private volatile boolean mQuit = false; 50 51 /** 52 * Creates a new cache triage dispatcher thread. You must call {@link #start()} 53 * in order to begin processing. 54 * 55 * @param cacheQueue Queue of incoming requests for triage 56 * @param networkQueue Queue to post requests that require network to 57 * @param cache Cache interface to use for resolution 58 * @param delivery Delivery interface to use for posting responses 59 */ 60 public CacheDispatcher( 61 BlockingQueue
> cacheQueue, BlockingQueue
> networkQueue, 62 Cache cache, ResponseDelivery delivery) { 63 mCacheQueue = cacheQueue; 64 mNetworkQueue = networkQueue; 65 mCache = cache; 66 mDelivery = delivery; 67 } 68 69 /** 70 * Forces this dispatcher to quit immediately. If any requests are still in 71 * the queue, they are not guaranteed to be processed. 72 */ 73 public void quit() { 74 mQuit = true; 75 interrupt(); 76 } 77 78 @Override 79 public void run() { 80 if (DEBUG) VolleyLog.v("start new dispatcher"); 81 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 82 83 // Make a blocking call to initialize the cache. 84 mCache.initialize(); 85 86 Request
request; 87 while (true) { 88 // release previous request object to avoid leaking request object when mQueue is drained. 89 request = null; 90 try { 91 // Take a request from the queue. 92 request = mCacheQueue.take(); 93 } catch (InterruptedException e) { 94 // We may have been interrupted because it was time to quit. 95 if (mQuit) { 96 return; 97 } 98 continue; 99 }100 try {101 request.addMarker("cache-queue-take");102 103 // If the request has been canceled, don't bother dispatching it.104 if (request.isCanceled()) {105 request.finish("cache-discard-canceled");106 continue;107 }108 109 // Attempt to retrieve this item from cache.110 Cache.Entry entry = mCache.get(request.getCacheKey());111 if (entry == null) {112 request.addMarker("cache-miss");113 // Cache miss; send off to the network dispatcher.114 mNetworkQueue.put(request);115 continue;116 }117 118 // If it is completely expired, just send it to the network.119 if (entry.isExpired()) {120 request.addMarker("cache-hit-expired");121 request.setCacheEntry(entry);122 mNetworkQueue.put(request);123 continue;124 }125 126 // We have a cache hit; parse its data for delivery back to the request.127 request.addMarker("cache-hit");128 Response
response = request.parseNetworkResponse(129 new NetworkResponse(entry.data, entry.responseHeaders));130 request.addMarker("cache-hit-parsed");131 132 if (!entry.refreshNeeded()) {133 // Completely unexpired cache hit. Just deliver the response.134 mDelivery.postResponse(request, response);135 } else {136 // Soft-expired cache hit. We can deliver the cached response,137 // but we need to also send the request to the network for138 // refreshing.139 request.addMarker("cache-hit-refresh-needed");140 request.setCacheEntry(entry);141 142 // Mark the response as intermediate.143 response.intermediate = true;144 145 // Post the intermediate response back to the user and have146 // the delivery then forward the request along to the network.147 final Request
finalRequest = request;148 mDelivery.postResponse(request, response, new Runnable() {149 @Override150 public void run() {151 try {152 mNetworkQueue.put(finalRequest);153 } catch (InterruptedException e) {154 // Not much we can do about this.155 }156 }157 });158 }159 } catch (Exception e) {160 VolleyLog.e(e, "Unhandled exception %s", e.toString());161 }162 }163 }164 }
CacheDispatcher

 111

转载地址:http://hdqlx.baihongyu.com/

你可能感兴趣的文章
SQL疑难杂症【3】链接服务器提示"无法启动分布式事物"
查看>>
Windows Mobile和Wince(Windows Embedded CE)下如何封装Native DLL提供给.NET Compact Framework进行调用...
查看>>
数据库相关
查看>>
HDU Count the string (KMP)
查看>>
C#中的泛型
查看>>
编程之美4:求数组中的最大值和最小值
查看>>
ios7新增基础类库以及OC新特性
查看>>
[LeetCode] Maximal Square
查看>>
代码设置TSQLCONNECTION参数
查看>>
BROKER服务器同客户端和应用服务器三者之间传递消息的格式定义
查看>>
【转】20个Cydia常见错误问题解决方法汇总
查看>>
使用jQuery和Bootstrap实现多层、自适应模态窗口
查看>>
C#中如何选择使用T[]或List<T>
查看>>
iOS开发-数据存储NSCoder
查看>>
SQL Server 存储过程【转】
查看>>
localstorage和sessionstorage上手使用记录
查看>>
荣耀手机缅甸仰光店开业,只有我觉得缅甸美女比较多吗?
查看>>
融合数据库技术,降低开源MySQL使用成本实践
查看>>
IDC:全球以太网交换机和路由器市场整体看涨
查看>>
英国零售商:“无协议脱欧”恐让超市空荡荡
查看>>