Volley源碼分析之流程和緩存

前言
Android一開始提供了HttpURLConnection和HttpClient兩種方式請(qǐng)求網(wǎng)絡(luò),但是這兩種控制粒度太細(xì),需要自己做很多操作,同時(shí)也導(dǎo)致了需要寫很多的重復(fù)代碼。為此,2013年Google推出Volley網(wǎng)絡(luò)請(qǐng)求框架,基于此框架,開發(fā)者只需設(shè)置請(qǐng)求url,傳遞參數(shù)和請(qǐng)求成功或失敗的回調(diào)的方法即可,大大簡化了網(wǎng)絡(luò)請(qǐng)求;雖然現(xiàn)在Okhttp很流行,但Volley的源碼非常值得學(xué)習(xí);
使用
可分為三個(gè)步驟,簡稱三部走:
- 創(chuàng)建RequestQueue實(shí)例
- 創(chuàng)建StringRequest實(shí)例
- 調(diào)用RequestQueue實(shí)例方法add將request作為參數(shù)傳入
StringRequest中并沒有提供設(shè)置POST參數(shù)的方法,但是當(dāng)發(fā)出POST請(qǐng)求的時(shí)候,Volley會(huì)嘗試調(diào)StringRequest的父類——Request中的getParams()方法來獲取POST參數(shù),那么解決方法自然也就有了,我們只需要在StringRequest的匿名類中重寫getParams()方法,在這里設(shè)置POST參數(shù)就可以了。示范代碼如下:
String requestUrl = "http://yourapi";//建立請(qǐng)求的api的URL; Response.Listener successListener = new Response.Listener<String>() {//成功回調(diào) @Override public void onResponse(String response) { Log.d("TAG", response); } }; Response.ErrorListener errorListener = new Response.ErrorListener() {//失敗回調(diào) @Override public void onErrorResponse(VolleyError error) { Log.e("TAG", error.getMessage(), error); } }; StringRequest stringRequest = new StringRequest(Request.Method.POST, requestUrl, successListener, errorListener) { @Override protected Map<String, String> getParams() throws AuthFailureError { //重寫getParams方法傳POST請(qǐng)求參數(shù) Map<String, String> map = new HashMap<String, String>(); map.put("phone", "phone num"); map.put("passwd", "passwd"); return map; } }; Volley.newRequestQueue(mContext).add(stringRequest);//創(chuàng)建newRequestQueue并傳入?yún)?shù)
請(qǐng)求成功后,Logcat中會(huì)打印出服務(wù)器返回的信息;
源碼分析
Volley適合通信頻繁及數(shù)據(jù)量少的應(yīng)用,大多展示類的App都適用;不適合繁重的下載或者流的操作,因?yàn)閂olley會(huì)把解析到的響應(yīng)數(shù)據(jù)保持在內(nèi)存中;
首先從我們一開始的三部走的第一步開始,創(chuàng)建RequestQueue實(shí)例,代碼語句
Volley.newRequestQueue(mContext)
跟進(jìn)代碼查看
public static RequestQueue newRequestQueue(Context context) { return newRequestQueue(context, null); }
然后調(diào)用了兩個(gè)參數(shù)的newRequestQueue方法,默認(rèn)第二個(gè)參數(shù)HttpStack為null;
/** * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it. * * @param context A {@link Context} to use for creating the cache dir. * @param stack An {@link HttpStack} to use for the network, or null for default. * @return A started {@link RequestQueue} instance. */ 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) {//根據(jù)系統(tǒng)版本號(hào)判斷適用哪個(gè)請(qǐng)求方式 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);//進(jìn)入DiskBasedCache構(gòu)造方法可知,此處設(shè)置緩存目錄,以及設(shè)定了一個(gè)默認(rèn)5M的緩存大??; queue.start(); return queue; }
HttpStack有實(shí)現(xiàn)兩個(gè)類:HttpClientStack和HurlStack,都實(shí)現(xiàn)了performRequest方法,主要的作用是執(zhí)行網(wǎng)絡(luò)請(qǐng)求操作并獲取返回結(jié)果;HttpClientStack使用了HttpClient進(jìn)行網(wǎng)絡(luò)通信,而HttpStack是使用HttpURLConnection;當(dāng)系統(tǒng)版本小于9時(shí)使用HttpClientStack,反之使用HurlStack;這個(gè)判斷主要是因?yàn)橄到y(tǒng)版本小于9時(shí)HttpURLConnection有bug,調(diào)用 close() 函數(shù)會(huì)影響連接池,導(dǎo)致連接復(fù)用失效;穩(wěn)定性不如HttpClient穩(wěn)定;而9之后系統(tǒng)作了修改,默認(rèn)開啟了 gzip 壓縮,提高了 HTTPS 的性能,HttpURLConnection成了最佳選擇;郭霖文章給出的原因
最后創(chuàng)建了RequestQueue,調(diào)用了start方法并返回RequestQueue實(shí)例;注意,RequestQueue的創(chuàng)建方法中我們可以看到,自己可以自定義RequestQueue,配置緩存目錄,及緩存的大??;然后調(diào)用了RequestQueue的start方法
/** * Starts the dispatchers in this queue. */ 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(); } }
start方法主要?jiǎng)?chuàng)建了CacheDispatcher和NetworkDispatcher,并調(diào)用了對(duì)應(yīng)的start方法;CacheDispatcher負(fù)責(zé)處理調(diào)度走緩存邏輯的請(qǐng)求,而NetworkDispatcher則是處理調(diào)度走網(wǎng)絡(luò)的請(qǐng)求;一看start方法可以推測(cè)這兩個(gè)類為Thread的子類,進(jìn)入看果然是繼承了Thread
public class CacheDispatcher extends Thread{ }
for循環(huán)默認(rèn)創(chuàng)建4個(gè)NetworkDispatcher線程并開啟,也就是說當(dāng)Volley.newRequstQueue這句代碼執(zhí)行時(shí),就創(chuàng)建了5個(gè)線程在不斷運(yùn)行,不斷阻塞內(nèi)部的請(qǐng)求隊(duì)列知道請(qǐng)求的添加才開始下面的處理邏輯;
Volley.newRequstQueue創(chuàng)建完RequestQueue后,到了三步走的第三部,調(diào)用add方法把Request插入RequestQueue中
public Request add(Request request) { /* ... ...隱藏部分代碼 ...*/ // If the request is uncacheable, skip the cache queue and go straight to the network. //判斷request是否需要緩存,若不需要緩存則加入mNetworkQueue中,默認(rèn)Request都需要緩存, //setShouldCache可改變?cè)O(shè)置 if (!request.shouldCache()) { mNetworkQueue.add(request); return request; } /* ... ...隱藏部分代碼 ...*/ mWaitingRequests.put(cacheKey, null); mCacheQueue.add(request);//默認(rèn)的request都加入到mCacheQueue return request; } }
默認(rèn)添加Request都會(huì)添加到mCacheQueue,緩存調(diào)度會(huì)不斷輪詢此隊(duì)列;上面說到創(chuàng)建了RequestQueue后會(huì)默認(rèn)開啟5個(gè)線程,其中包括了緩存線程CacheDispatcher,現(xiàn)在看下CacheDispatcher的run方法
@Override public void run() { /* ...隱藏部分代碼 */ final Request request = mCacheQueue.take();//獲取隊(duì)列頭部元素,若為空則一直阻塞,采用了BlockingQueue 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. //若過期,則添加到NetworkQueue中執(zhí)行網(wǎng)絡(luò)請(qǐng)求 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);//緩存可用則進(jìn)行回調(diào) } else { // 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; } } }
可以看到開啟線程后會(huì)一直阻塞直到獲取緩存隊(duì)列中插入的數(shù)據(jù),阻塞隊(duì)列使用了BlockingQueue,可另外了解;獲取后查找緩存中是否有response,若response過期或需要刷新則會(huì)向mNetworkQueue中插入,走網(wǎng)絡(luò)請(qǐng)求,反之則使用緩存中的response;若緩存可用便調(diào)用request的parseNetworkResponse解析數(shù)據(jù),然后就是調(diào)用mDelivery.postResponse回調(diào);mDelivery默認(rèn)的實(shí)現(xiàn)是創(chuàng)建RequestQueue時(shí)候創(chuàng)建的
public RequestQueue(Cache cache, Network network, int threadPoolSize) { this(cache, network, threadPoolSize, new ExecutorDelivery(new Handler(Looper.getMainLooper()))); }
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); } }; }
mDelivery.postResponse最終會(huì)調(diào)用ExecutorDelivery的execute方法,其傳入的參數(shù)ResponseDeliveryRunnable,最終回調(diào)用handler的post方法把線程切換到主線程;ResponseDeliveryRunnable的run方法執(zhí)行在主線程,如下:
public void run() { /* 隱藏部分代碼 */ // Deliver a normal response or error, depending. if (mResponse.isSuccess()) { mRequest.deliverResponse(mResponse.result);//回調(diào)到我們一開始創(chuàng)建的Listenner回調(diào)中 } else { mRequest.deliverError(mResponse.error); } // If this is an intermediate response, add a marker, otherwise we're done // and the request can be finished. if (mResponse.intermediate) {//響應(yīng)需要刷新的時(shí)候?yàn)閠rue mRequest.addMarker("intermediate-response"); } else { mRequest.finish("done"); } // If we have been provided a post-delivery runnable, run it. if (mRunnable != null) { mRunnable.run(); }
可以看到若成功了則調(diào)用mRequest.deliverResponse(mResponse.result);這句代碼,而deliverResponse則調(diào)用了我們一開始創(chuàng)建的StringRequest代碼的Response.Listener的onResponse,我們便可以在這里處理返回來的響應(yīng)并更新View;
現(xiàn)在看下當(dāng)沒有緩存或緩存實(shí)效時(shí),使用網(wǎng)絡(luò)獲取響應(yīng)的NetworkDispatcher,看下run方法:
@Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); Request request; while (true) { try { // Take a request from the queue. request = mQueue.take();//從Volley中創(chuàng)建RequestQueue后調(diào)用start方法中,同用一個(gè)隊(duì)列傳進(jìn)構(gòu)造方法可知:mQueue和剛才CacheDispatcher的mNetworkQueue是同一個(gè)隊(duì)列, } catch (InterruptedException e) { // We may have been interrupted because it was time to quit. if (mQuit) { return; } continue; } // Perform the network request. NetworkResponse networkResponse = mNetwork.performRequest(request);//網(wǎng)絡(luò)請(qǐng)求,實(shí)際上mNetwork的實(shí)現(xiàn)是BasicNetwork request.addMarker("network-http-complete"); /* 隱藏部分代碼 */ // Parse the response here on the worker thread. Response<?> response = request.parseNetworkResponse(networkResponse); request.addMarker("network-parse-complete"); // Write to cache if applicable. // TODO: Only update cache metadata instead of entire record for 304s. if (request.shouldCache() && response.cacheEntry != null) { mCache.put(request.getCacheKey(), response.cacheEntry); request.addMarker("network-cache-written"); } // Post the response back. request.markDelivered(); mDelivery.postResponse(request, response); /* 隱藏部分代碼 */ } }
mNetwork.performRequest這句代碼實(shí)現(xiàn)了請(qǐng)求代碼,mNetwork的唯一實(shí)現(xiàn)為BasicNetwork,其performRequest方法主要根據(jù)HttpStack來執(zhí)行網(wǎng)絡(luò)請(qǐng)求并構(gòu)造NetworkResponse返回;然后就是調(diào)用mDelivery.postResponse(request, response)方法回調(diào),跟CacheDispatcher一樣的流程了;
Volley.newRequestQueue(mContext).add(stringRequest)
到此,這句代碼的流程完全走了一遍;總結(jié)一下:調(diào)用newRequestQueue創(chuàng)建了RequestQueue,同時(shí)開啟了CacheDispatcher和NetworkDispatcher 5個(gè)線程不斷阻塞輪詢,當(dāng)調(diào)用add方法時(shí),會(huì)先在緩存隊(duì)列mCacheQueue中插入request,此時(shí)緩存線程CacheDispatcher先查找緩存中是否有該request的響應(yīng)緩存,若有且沒有過期,且不需要刷新操作,則request自身的parseNetworkResponse方法解析成response且回調(diào)給主線程自己創(chuàng)建request的Response.Listener中的onResponse,若過期,沒有緩存或需要刷新,則添加到mNetworkQueue中,NetworkDispatcher輪詢獲取request后,根據(jù)系統(tǒng)版本使用HttpClient或HttpURLConnection執(zhí)行網(wǎng)絡(luò)請(qǐng)求,得到響應(yīng)后解析并回調(diào)給主線程,若request需要緩存且響應(yīng)不為null,則存入響應(yīng)緩存中;
流程圖總結(jié)如下:

緩存策略
-
request的緩存
?
現(xiàn)在我們從緩存的角度分析下源碼,當(dāng)調(diào)用Volley.newRequestQueue(mContext).add(stringRequest)時(shí),我們進(jìn)入RequestQueue的add方法
public Request add(Request request) { // Tag the request as belonging to this queue and add it to the set of current requests. request.setRequestQueue(this);//把request與當(dāng)前RequestQueue建立聯(lián)系 synchronized (mCurrentRequests) { mCurrentRequests.add(request);//存放所有的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()) {//是否應(yīng)該緩存,默認(rèn)所有request都緩存 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(); if (mWaitingRequests.containsKey(cacheKey)) {//當(dāng)前add的request是否已經(jīng)有相同的請(qǐng)求,若請(qǐng)求沒有完成(成功或者失?。?,則會(huì)一直存在mWaitingRequest中 // There is already a request in flight. Queue up. Queue<Request> stagedRequests = mWaitingRequests.get(cacheKey); if (stagedRequests == null) { stagedRequests = new LinkedList<Request>(); } stagedRequests.add(request);//創(chuàng)建一個(gè)隊(duì)列并添加request mWaitingRequests.put(cacheKey, stagedRequests);//緩存含request的隊(duì)列 if (VolleyLog.DEBUG) { VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", cacheKey); } } else { // Insert 'null' queue for this cacheKey, indicating there is now a request in // flight. mWaitingRequests.put(cacheKey, null);//如果request沒有則存入map中 mCacheQueue.add(request); } return request; } }
當(dāng)add方法調(diào)用時(shí),會(huì)判斷mWaitingRequests是否包含cacheKey,假設(shè)第一個(gè)request請(qǐng)求mWaitingRequests不包含,則緩存cacheKey,鍵值為null,同時(shí)添加入緩存隊(duì)列。當(dāng)?shù)谝粋€(gè)request還在請(qǐng)求中,第二個(gè)和第三個(gè)相同的request這時(shí)添加進(jìn)來,則走進(jìn)入邏輯新建一個(gè)隊(duì)列并加入兩個(gè)request,和cacheKey緩存進(jìn)mWaitingRequests的map中,注意此時(shí)緩存的都是request,當(dāng)?shù)谝粋€(gè)request請(qǐng)求完成后會(huì)回調(diào)request的finish方法
void finish(final String tag) { if (mRequestQueue != null) { mRequestQueue.finish(this);//調(diào)用RequestQueue的finish方法 } /* 隱藏部分代碼 */ }
而request的finish方法會(huì)調(diào)用RequestQueue的finish方法
void finish(Request request) { // Remove from the set of requests currently being processed. synchronized (mCurrentRequests) { mCurrentRequests.remove(request); } if (request.shouldCache()) { synchronized (mWaitingRequests) { String cacheKey = request.getCacheKey(); Queue<Request> waitingRequests = mWaitingRequests.remove(cacheKey);//移除該request的cacheKey,并得到緩存時(shí)創(chuàng)建的request隊(duì)列 if (waitingRequests != null) { if (VolleyLog.DEBUG) { VolleyLog.v("Releasing %d waiting requests for cacheKey=%s.", waitingRequests.size(), cacheKey); } // Process all queued up requests. They won't be considered as in flight, but // that's not a problem as the cache has been primed by 'request'. mCacheQueue.addAll(waitingRequests);//若緩存起來的隊(duì)列不為空,把里面的request全部添加到緩存隊(duì)列; } } } }
當(dāng)waitingRequests中的request添加到緩存隊(duì)列后,又進(jìn)入到CacheDispatcher的調(diào)度線程中了
- CacheDispatcher里的緩存
又從CacheDispatcher的run分析,這次是從緩存角度;主要有三點(diǎn)
- 沒有緩存,添加到網(wǎng)絡(luò)請(qǐng)求隊(duì)列
- 緩存過期,添加到網(wǎng)絡(luò)請(qǐng)求隊(duì)列
- 緩存需要刷新,先回調(diào)數(shù)據(jù)給用戶,再添加請(qǐng)求到網(wǎng)絡(luò)請(qǐng)求隊(duì)列
public void run() { if (DEBUG) VolleyLog.v("start new dispatcher"); Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); // Make a blocking call to initialize the cache. mCache.initialize(); 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) {//沒有緩存,添加到網(wǎng)絡(luò)請(qǐng)求隊(duì)列 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()) {//緩存過期,添加到網(wǎng)絡(luò)請(qǐng)求隊(duì)列 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()) {//不需要刷新,回調(diào)給用戶設(shè)置的listener // Completely unexpired cache hit. Just deliver the response. mDelivery.postResponse(request, response); } else {//緩存需要刷新,先回調(diào)數(shù)據(jù)給用戶,再添加請(qǐng)求到網(wǎng)絡(luò)請(qǐng)求隊(duì)列 // 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;//在需要刷新時(shí)置為true,在mDelivery.postResponse后,會(huì)調(diào)用ResponseDeliveryRunnable的run方法,其中中有個(gè)判斷決定是添加信息或者調(diào)用request的finish方法 // 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; } } }
注意這里當(dāng)需要刷新數(shù)據(jù)的時(shí)候,會(huì)先回調(diào)用戶設(shè)置的listener,然后再去發(fā)起請(qǐng)求,若數(shù)據(jù)有變化會(huì)再次回調(diào),所以這種情況會(huì)回調(diào)用戶設(shè)置的listener兩次
- NetworkDispatcher的緩存
請(qǐng)求前添加和cache相關(guān)的headers
private void addCacheHeaders(Map<String, String> headers, Cache.Entry entry) { // If there's no cache entry, we're done. if (entry == null) { return; } if (entry.etag != null) { headers.put("If-None-Match", entry.etag); } if (entry.serverDate > 0) { Date refTime = new Date(entry.serverDate); headers.put("If-Modified-Since", DateUtils.formatDate(refTime)); } }
request的字段含義:
If-None-Match:存文件的Etag(Hash)值,與服務(wù)器回應(yīng)的Etag比較判斷是否改變
If-Modified-Since:緩存文件的最后修改時(shí)間
當(dāng)網(wǎng)絡(luò)請(qǐng)求完成后,調(diào)用了request中的parseNetworkResponse方法,用我們一開始的StringRequest來分析下。
protected Response<String> parseNetworkResponse(NetworkResponse response) { String parsed; try { parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); } catch (UnsupportedEncodingException e) { parsed = new String(response.data); } return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response)); }
此時(shí)又調(diào)用了HttpHeaderParser.parseCacheHeaders(response)來構(gòu)建Cache.Entry對(duì)象,代碼就不放了,主要是用response.headers中的Date,Cache-Control,Expires,ETag來構(gòu)建對(duì)象,然后若request需要緩存及響應(yīng)不為空則放進(jìn)響應(yīng)緩存中
response字段含義:
Cache-Control:告訴所有的緩存機(jī)制是否可以緩存及哪種類型
Expires:響應(yīng)過期的日期和時(shí)間
Last-Modified:請(qǐng)求資源的最后修改時(shí)間
ETag:請(qǐng)求變量的實(shí)體標(biāo)簽的當(dāng)前值
NetworkDispatcher
Response<?> response = request.parseNetworkResponse(networkResponse); request.addMarker("network-parse-complete"); // Write to cache if applicable. // TODO: Only update cache metadata instead of entire record for 304s. if (request.shouldCache() && response.cacheEntry != null) { mCache.put(request.getCacheKey(), response.cacheEntry); request.addMarker("network-cache-written"); } // Post the response back. request.markDelivered(); mDelivery.postResponse(request, response);
存入緩存的mCache的實(shí)現(xiàn)是DiskBasedCache,是不是有點(diǎn)熟悉,沒錯(cuò),就是我們一開始Volley.newRequestQueue中創(chuàng)建RequestQueue中的一個(gè)參數(shù),我們看下DiskBasedCache的put方法
public synchronized void put(String key, Entry entry) { pruneIfNeeded(entry.data.length);//put之前先修正大小,避免超出限定的緩存大小 File file = getFileForKey(key); try { FileOutputStream fos = new FileOutputStream(file); CacheHeader e = new CacheHeader(key, entry); e.writeHeader(fos); fos.write(entry.data); fos.close(); putEntry(key, e); return; } catch (IOException e) { } boolean deleted = file.delete();//try出錯(cuò)時(shí)會(huì)把file給delete if (!deleted) { VolleyLog.d("Could not clean up file %s", file.getAbsolutePath()); } }
進(jìn)入pruneIfNeeded方法
private void pruneIfNeeded(int neededSpace) { if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes) { return; } if (VolleyLog.DEBUG) { VolleyLog.v("Pruning old cache entries."); } long before = mTotalSize; int prunedFiles = 0; long startTime = SystemClock.elapsedRealtime(); Iterator<Map.Entry<String, CacheHeader>> iterator = mEntries.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, CacheHeader> entry = iterator.next(); CacheHeader e = entry.getValue(); boolean deleted = getFileForKey(e.key).delete(); if (deleted) { mTotalSize -= e.size; } else { VolleyLog.d("Could not delete cache entry for key=%s, filename=%s", e.key, getFilenameForKey(e.key)); } iterator.remove(); prunedFiles++; if ((mTotalSize + neededSpace) < mMaxCacheSizeInBytes * HYSTERESIS_FACTOR) { break; } } if (VolleyLog.DEBUG) { VolleyLog.v("pruned %d files, %d bytes, %d ms", prunedFiles, (mTotalSize - before), SystemClock.elapsedRealtime() - startTime); } }
傳入需要插入數(shù)據(jù)的大小參數(shù)neededSpace和當(dāng)前緩存文件總大小的記錄mTotalSize計(jì)算出是否超出了設(shè)定的緩存大小,若超出則獲取CacheHeader列表;CacheHeader里包含緩存文件大小等信息,遍歷CacheHeader列表刪除緩存,直到小于設(shè)定緩存的0.9倍,為什么是這個(gè)數(shù)?猜測(cè)是個(gè)緩存過大的標(biāo)記,同時(shí)可以留有一定可緩存空間;
-
如果讓你去設(shè)計(jì)Volley的緩存功能,你要如何增大它的命中率?
可以知道,在pruneIfNeeded中刪除緩存時(shí),并沒有判斷緩存是否過期,只是遍歷來刪除,很大程度上會(huì)誤刪剛緩存下來的數(shù)據(jù),而過期數(shù)據(jù)卻仍然存在;所以可以在刪除前進(jìn)行一次過期緩存的清除,然后再使用Lru算法清除最近最久未使用的緩存;
總結(jié)一下緩存方面的流程:當(dāng)從RequestQueue中add的時(shí)候會(huì)判斷request是否可以緩存,若不可以則添加到網(wǎng)絡(luò)隊(duì)列;否則添加到緩存隊(duì)列;進(jìn)入緩存隊(duì)列后,CacheDispatcher中不斷輪詢?nèi)〕鼍彺骊?duì)列中的request,然后判斷該request是否已經(jīng)緩存了響應(yīng),響應(yīng)是否過期,響應(yīng)是否需要刷新;若request取消、沒有緩存響應(yīng)、過期或需要刷新,則添加request到網(wǎng)絡(luò)隊(duì)列;NetworkDispatcher也不斷的輪詢,取出添加到網(wǎng)絡(luò)請(qǐng)求隊(duì)列的request,用BasicNetwork請(qǐng)求網(wǎng)絡(luò),請(qǐng)求前添加headers相關(guān)的信息,請(qǐng)求返回的響應(yīng)會(huì)被HttpHeaderParser的parseCacheHeaders解析成Cache.Entry對(duì)象,若此時(shí)的request能緩存且響應(yīng)不為null,則添加到響應(yīng)緩存中;
注:才疏學(xué)淺,如有有寫錯(cuò),理解錯(cuò)的地方麻煩指出更正下,非常感謝!