Volley源碼分析之流程和緩存

Volley源碼分析之流程和緩存

Volley演講時配圖

前言

Android一開始提供了HttpURLConnection和HttpClient兩種方式請求網(wǎng)絡,但是這兩種控制粒度太細,需要自己做很多操作,同時也導致了需要寫很多的重復代碼。為此,2013年Google推出Volley網(wǎng)絡請求框架,基于此框架,開發(fā)者只需設置請求url,傳遞參數(shù)和請求成功或失敗的回調的方法即可,大大簡化了網(wǎng)絡請求;雖然現(xiàn)在Okhttp很流行,但Volley的源碼非常值得學習;

Volley官方介紹文檔(需翻墻)

使用

可分為三個步驟,簡稱三部走:

  1. 創(chuàng)建RequestQueue實例
  2. 創(chuàng)建StringRequest實例
  3. 調用RequestQueue實例方法add將request作為參數(shù)傳入

StringRequest中并沒有提供設置POST參數(shù)的方法,但是當發(fā)出POST請求的時候,Volley會嘗試調StringRequest的父類——Request中的getParams()方法來獲取POST參數(shù),那么解決方法自然也就有了,我們只需要在StringRequest的匿名類中重寫getParams()方法,在這里設置POST參數(shù)就可以了。示范代碼如下:

String requestUrl = "http://yourapi";//建立請求的api的URL;

Response.Listener successListener = new Response.Listener<String>() {//成功回調
   @Override
   public void onResponse(String response) {
       Log.d("TAG", response);
   }
};

Response.ErrorListener errorListener = new Response.ErrorListener() {//失敗回調
   @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請求參數(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ù)

請求成功后,Logcat中會打印出服務器返回的信息;

源碼分析

Volley適合通信頻繁及數(shù)據(jù)量少的應用,大多展示類的App都適用;不適合繁重的下載或者流的操作,因為Volley會把解析到的響應數(shù)據(jù)保持在內存中;

首先從我們一開始的三部走的第一步開始,創(chuàng)建RequestQueue實例,代碼語句

Volley.newRequestQueue(mContext)

跟進代碼查看

public static RequestQueue newRequestQueue(Context context) {
    return newRequestQueue(context, null);
}

然后調用了兩個參數(shù)的newRequestQueue方法,默認第二個參數(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)版本號判斷適用哪個請求方式
            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);//進入DiskBasedCache構造方法可知,此處設置緩存目錄,以及設定了一個默認5M的緩存大??;
    queue.start();

    return queue;
}

HttpStack有實現(xiàn)兩個類:HttpClientStack和HurlStack,都實現(xiàn)了performRequest方法,主要的作用是執(zhí)行網(wǎng)絡請求操作并獲取返回結果;HttpClientStack使用了HttpClient進行網(wǎng)絡通信,而HttpStack是使用HttpURLConnection;當系統(tǒng)版本小于9時使用HttpClientStack,反之使用HurlStack;這個判斷主要是因為系統(tǒng)版本小于9時HttpURLConnection有bug,調用 close() 函數(shù)會影響連接池,導致連接復用失效;穩(wěn)定性不如HttpClient穩(wěn)定;而9之后系統(tǒng)作了修改,默認開啟了 gzip 壓縮,提高了 HTTPS 的性能,HttpURLConnection成了最佳選擇;郭霖文章給出的原因

最后創(chuàng)建了RequestQueue,調用了start方法并返回RequestQueue實例;注意,RequestQueue的創(chuàng)建方法中我們可以看到,自己可以自定義RequestQueue,配置緩存目錄,及緩存的大??;然后調用了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方法主要創(chuàng)建了CacheDispatcher和NetworkDispatcher,并調用了對應的start方法;CacheDispatcher負責處理調度走緩存邏輯的請求,而NetworkDispatcher則是處理調度走網(wǎng)絡的請求;一看start方法可以推測這兩個類為Thread的子類,進入看果然是繼承了Thread

public class CacheDispatcher extends Thread{
  
}

for循環(huán)默認創(chuàng)建4個NetworkDispatcher線程并開啟,也就是說當Volley.newRequstQueue這句代碼執(zhí)行時,就創(chuàng)建了5個線程在不斷運行,不斷阻塞內部的請求隊列知道請求的添加才開始下面的處理邏輯;

Volley.newRequstQueue創(chuàng)建完RequestQueue后,到了三步走的第三部,調用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中,默認Request都需要緩存,       //setShouldCache可改變設置
    if (!request.shouldCache()) {
        mNetworkQueue.add(request);
        return request;
    }

    /*
   ...
   ...隱藏部分代碼
   ...*/
  
    mWaitingRequests.put(cacheKey, null);
    mCacheQueue.add(request);//默認的request都加入到mCacheQueue
       
        return request;
    }
}

默認添加Request都會添加到mCacheQueue,緩存調度會不斷輪詢此隊列;上面說到創(chuàng)建了RequestQueue后會默認開啟5個線程,其中包括了緩存線程CacheDispatcher,現(xiàn)在看下CacheDispatcher的run方法

 @Override
    public void run() {
        /*
        ...隱藏部分代碼
        */
           
                final Request request = mCacheQueue.take();//獲取隊列頭部元素,若為空則一直阻塞,采用了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)絡請求
                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 {
                  
                    // 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;
            }
        }
    }

可以看到開啟線程后會一直阻塞直到獲取緩存隊列中插入的數(shù)據(jù),阻塞隊列使用了BlockingQueue,可另外了解;獲取后查找緩存中是否有response,若response過期或需要刷新則會向mNetworkQueue中插入,走網(wǎng)絡請求,反之則使用緩存中的response;若緩存可用便調用request的parseNetworkResponse解析數(shù)據(jù),然后就是調用mDelivery.postResponse回調;mDelivery默認的實現(xiàn)是創(chuàng)建RequestQueue時候創(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最終會調用ExecutorDelivery的execute方法,其傳入的參數(shù)ResponseDeliveryRunnable,最終回調用handler的post方法把線程切換到主線程;ResponseDeliveryRunnable的run方法執(zhí)行在主線程,如下:

public void run() {
    /*
    隱藏部分代碼
    */
    // Deliver a normal response or error, depending.
    if (mResponse.isSuccess()) {
        mRequest.deliverResponse(mResponse.result);//回調到我們一開始創(chuàng)建的Listenner回調中
    } 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) {//響應需要刷新的時候為true
        mRequest.addMarker("intermediate-response");
    } else {
        mRequest.finish("done");
    }

    // If we have been provided a post-delivery runnable, run it.
    if (mRunnable != null) {
        mRunnable.run();
    }

可以看到若成功了則調用mRequest.deliverResponse(mResponse.result);這句代碼,而deliverResponse則調用了我們一開始創(chuàng)建的StringRequest代碼的Response.Listener的onResponse,我們便可以在這里處理返回來的響應并更新View;

現(xiàn)在看下當沒有緩存或緩存實效時,使用網(wǎ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后調用start方法中,同用一個隊列傳進構造方法可知:mQueue和剛才CacheDispatcher的mNetworkQueue是同一個隊列,
        } 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)絡請求,實際上mNetwork的實現(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這句代碼實現(xiàn)了請求代碼,mNetwork的唯一實現(xiàn)為BasicNetwork,其performRequest方法主要根據(jù)HttpStack來執(zhí)行網(wǎng)絡請求并構造NetworkResponse返回;然后就是調用mDelivery.postResponse(request, response)方法回調,跟CacheDispatcher一樣的流程了;

Volley.newRequestQueue(mContext).add(stringRequest)

到此,這句代碼的流程完全走了一遍;總結一下:調用newRequestQueue創(chuàng)建了RequestQueue,同時開啟了CacheDispatcher和NetworkDispatcher 5個線程不斷阻塞輪詢,當調用add方法時,會先在緩存隊列mCacheQueue中插入request,此時緩存線程CacheDispatcher先查找緩存中是否有該request的響應緩存,若有且沒有過期,且不需要刷新操作,則request自身的parseNetworkResponse方法解析成response且回調給主線程自己創(chuàng)建request的Response.Listener中的onResponse,若過期,沒有緩存或需要刷新,則添加到mNetworkQueue中,NetworkDispatcher輪詢獲取request后,根據(jù)系統(tǒng)版本使用HttpClient或HttpURLConnection執(zhí)行網(wǎng)絡請求,得到響應后解析并回調給主線程,若request需要緩存且響應不為null,則存入響應緩存中;

流程圖總結如下:

Volley原理圖

緩存策略

  • request的緩存

    ?

現(xiàn)在我們從緩存的角度分析下源碼,當調用Volley.newRequestQueue(mContext).add(stringRequest)時,我們進入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與當前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()) {//是否應該緩存,默認所有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)) {//當前add的request是否已經(jīng)有相同的請求,若請求沒有完成(成功或者失?。瑒t會一直存在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)建一個隊列并添加request
            mWaitingRequests.put(cacheKey, stagedRequests);//緩存含request的隊列
            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;
    }
}

當add方法調用時,會判斷mWaitingRequests是否包含cacheKey,假設第一個request請求mWaitingRequests不包含,則緩存cacheKey,鍵值為null,同時添加入緩存隊列。當?shù)谝粋€request還在請求中,第二個和第三個相同的request這時添加進來,則走進入邏輯新建一個隊列并加入兩個request,和cacheKey緩存進mWaitingRequests的map中,注意此時緩存的都是request,當?shù)谝粋€request請求完成后會回調request的finish方法

void finish(final String tag) {
    if (mRequestQueue != null) {
        mRequestQueue.finish(this);//調用RequestQueue的finish方法
    }
   /*
   隱藏部分代碼
   */
}

而request的finish方法會調用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,并得到緩存時創(chuàng)建的request隊列
            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);//若緩存起來的隊列不為空,把里面的request全部添加到緩存隊列;
            }
        }
    }
}

當waitingRequests中的request添加到緩存隊列后,又進入到CacheDispatcher的調度線程中了

  • CacheDispatcher里的緩存

又從CacheDispatcher的run分析,這次是從緩存角度;主要有三點

  1. 沒有緩存,添加到網(wǎng)絡請求隊列
  2. 緩存過期,添加到網(wǎng)絡請求隊列
  3. 緩存需要刷新,先回調數(shù)據(jù)給用戶,再添加請求到網(wǎng)絡請求隊列
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)絡請求隊列
                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)絡請求隊列
                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()) {//不需要刷新,回調給用戶設置的listener
                // Completely unexpired cache hit. Just deliver the response.
                mDelivery.postResponse(request, response);
            } else {//緩存需要刷新,先回調數(shù)據(jù)給用戶,再添加請求到網(wǎng)絡請求隊列
                // 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;//在需要刷新時置為true,在mDelivery.postResponse后,會調用ResponseDeliveryRunnable的run方法,其中中有個判斷決定是添加信息或者調用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;
        }
    }
}

注意這里當需要刷新數(shù)據(jù)的時候,會先回調用戶設置的listener,然后再去發(fā)起請求,若數(shù)據(jù)有變化會再次回調,所以這種情況會回調用戶設置的listener兩次

  • NetworkDispatcher的緩存

請求前添加和cache相關的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)值,與服務器回應的Etag比較判斷是否改變

If-Modified-Since:緩存文件的最后修改時間

當網(wǎng)絡請求完成后,調用了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));
}

此時又調用了HttpHeaderParser.parseCacheHeaders(response)來構建Cache.Entry對象,代碼就不放了,主要是用response.headers中的Date,Cache-Control,Expires,ETag來構建對象,然后若request需要緩存及響應不為空則放進響應緩存中

response字段含義:

Cache-Control:告訴所有的緩存機制是否可以緩存及哪種類型

Expires:響應過期的日期和時間

Last-Modified:請求資源的最后修改時間

ETag:請求變量的實體標簽的當前值

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的實現(xiàn)是DiskBasedCache,是不是有點熟悉,沒錯,就是我們一開始Volley.newRequestQueue中創(chuàng)建RequestQueue中的一個參數(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出錯時會把file給delete
   if (!deleted) {
       VolleyLog.d("Could not clean up file %s", file.getAbsolutePath());
   }
}

進入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和當前緩存文件總大小的記錄mTotalSize計算出是否超出了設定的緩存大小,若超出則獲取CacheHeader列表;CacheHeader里包含緩存文件大小等信息,遍歷CacheHeader列表刪除緩存,直到小于設定緩存的0.9倍,為什么是這個數(shù)?猜測是個緩存過大的標記,同時可以留有一定可緩存空間;

  • 如果讓你去設計Volley的緩存功能,你要如何增大它的命中率?

    可以知道,在pruneIfNeeded中刪除緩存時,并沒有判斷緩存是否過期,只是遍歷來刪除,很大程度上會誤刪剛緩存下來的數(shù)據(jù),而過期數(shù)據(jù)卻仍然存在;所以可以在刪除前進行一次過期緩存的清除,然后再使用Lru算法清除最近最久未使用的緩存;

總結一下緩存方面的流程:當從RequestQueue中add的時候會判斷request是否可以緩存,若不可以則添加到網(wǎng)絡隊列;否則添加到緩存隊列;進入緩存隊列后,CacheDispatcher中不斷輪詢取出緩存隊列中的request,然后判斷該request是否已經(jīng)緩存了響應,響應是否過期,響應是否需要刷新;若request取消、沒有緩存響應、過期或需要刷新,則添加request到網(wǎng)絡隊列;NetworkDispatcher也不斷的輪詢,取出添加到網(wǎng)絡請求隊列的request,用BasicNetwork請求網(wǎng)絡,請求前添加headers相關的信息,請求返回的響應會被HttpHeaderParser的parseCacheHeaders解析成Cache.Entry對象,若此時的request能緩存且響應不為null,則添加到響應緩存中;

注:才疏學淺,如有有寫錯,理解錯的地方麻煩指出更正下,非常感謝!

相關文章的引用

Volley的緩存策略

Android Volley完全解析(四),帶你從源碼的角度理解Volley

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容