1. 程式人生 > >volley6--CacheDispatcher從緩存中獲取數據

volley6--CacheDispatcher從緩存中獲取數據

splay nbsp anti without .post getc rri software req

源碼:

技術分享
  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<Request<?>> mCacheQueue; 38 39 /** The queue of requests going out to the network. */ 40 private final BlockingQueue<Request<?>> 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<Request<?>> cacheQueue, BlockingQueue<Request<?>> 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 for 138 // 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 have 146 // the delivery then forward the request along to the network. 147 final Request<?> finalRequest = request; 148 mDelivery.postResponse(request, response, new Runnable() { 149 @Override 150 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

volley6--CacheDispatcher從緩存中獲取數據