【深藍】Volley 全程模擬

GET 緩存

首先 要有一個RequestQueue 隊列,實例化它。網絡

 

    public RequestQueue(Cache cache, Network network, int threadPoolSize,
            ResponseDelivery delivery) {
        mCache = cache;
        mNetwork = network;
        mDispatchers = new NetworkDispatcher[threadPoolSize];
        mDelivery = delivery;
    }

最終須要的參數,第一個緩存,第二個網絡請求,第三個網絡線程的個數,第四個響應交付。ide

    /**
     * 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();
        }
    }

先直接開啓這個線程,先stop 把如今全部的網絡調度所有中止掉。this

而後實例化一個緩存調度程序,幾個networkDispatcher調度程序,調度程序其實就是一個Thread,而後開啓他們。url

而緩存須要的參數第一個爲mCacheQueue緩存隊列,先把請求添加到緩存隊列,執行請求,若是沒有結果,會添加到第二個隊列mNetworkQueue網絡隊列,等待網絡調度,idea

mCache也就是從這裏找緩存,mDelivery 就是至關於結果回調。spa

網絡調度程序 也有4個參數,分別爲 網絡請求低劣,執行的請求方法,緩存,固然還有回調。線程

 

public <T> Request<T> add(Request<T> request)

須要的參數爲requestcode

    /**
     * Creates a new request with the given method (one of the values from
     * {@link Method}), URL, and error listener. Note that the normal response
     * listener is not provided here as delivery of responses is provided by
     * subclasses, who have a better idea of how to deliver an already-parsed
     * response.
     */
    public Request(int method, String url, Response.ErrorListener listener) {
        mMethod = method;
        mUrl = url;
        mErrorListener = listener;
        setRetryPolicy(new DefaultRetryPolicy());

        mDefaultTrafficStatsTag = findDefaultTrafficStatsTag(url);
    }
Request

 

1:請求類型,GET POST之類的。Request.Method.GET XXXcomponent

2:請求的URL地址。

3:錯誤結果回調監聽器。

4:失敗的時候重試策略。

5:看了裏面的構造爲

    /**
     * @return The hashcode of the URL's host component, or 0 if there is none.
     */
    private static int findDefaultTrafficStatsTag(String url) {
        if (!TextUtils.isEmpty(url)) {
            Uri uri = Uri.parse(url);
            if (uri != null) {
                String host = uri.getHost();
                if (host != null) {
                    return host.hashCode();
                }
            }
        }
        return 0;
    }
findDefaultTrafficStatsTag

  若是Url 解析成Uri 而後返回Uri的HashCode

下面是add(Request )過程了。

request.setRequestQueue(this); 把request 關聯到 請求隊列中。

        synchronized (mCurrentRequests) {
            mCurrentRequests.add(request);
        }

mCurrentRequests就是一個HashSet 不能有重複的的請求。

request.setSequence(getSequenceNumber()); 獲取一個請求序列號。自動增長的

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();
            if (mWaitingRequests.containsKey(cacheKey)) {
                // 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);
                mWaitingRequests.put(cacheKey, stagedRequests);
                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);
                mCacheQueue.add(request);
            }
            return request;
        }

 

是否已經在請求隊列中等等。

這個是應該緩存的,第一步先判斷

相關文章
相關標籤/搜索