Android volley 解析(四)之缓存篇

时间:2015-03-07 09:05:36   收藏:0   阅读:21635

这是 volley 的第四篇 blog 了,写完这篇,volley 的大部分用法也都算写了一遍,所以暂时不会写 volley 的文章了,如果想看我前面写的文章,可以点这里 Android volley 解析(三)之文件上传篇

为什么要用缓存

我们知道,当客户端在请求网络数据的时候,是需要消耗流量的,特别是对于移动端用户来说,对于流量的控制要求很高。所以在做网络请求的时候,如果对数据更新要求不是特别高,往往都会用到缓存机制,一方面能减少对服务端的请求,控制流量;另一方面,当客户端在没有网络的情况下也能看到上一次请求的数据,这样使用户体验更佳,如下图,微信朋友圈在没有网络的情况下,依然能看到朋友们的动态
技术分享

volley 缓存的原理

看了我前面blog 的朋友一定还记得,我在讲 get 请求的时候,讲到了volley 的基本流程,首先启动的是缓存线程mCacheDispatcher来获取缓存;
那我们就从如何获取缓存开始分析;
1、初始化缓存

  @Override
    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) {
        ...

2、获取缓存


        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.
                ...

这里有一个问题,缓存队列在什么时候添加缓存请求呢?我们回到最开始请求队列添加请求的地方

    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);
        synchronized (mCurrentRequests) {
            mCurrentRequests.add(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()) {
            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;
        }
    }

代码不长,也很好理解,如果我们的请求没有设置了缓存,则请求添加到网络请求队列中,并直接返回了,不往下执行了,这时缓存队列中无法获取请求,所以这里我们知道了,想要用缓存则需要
在 Request 中把

    //设置是否启用缓存
    public final void setShouldCache(boolean shouldCache) {
        mShouldCache = shouldCache;
    }

设为 true,当然,我们看mShouldCache 的默认值

    /**
     * Whether or not responses to this request should be cached.
     */
    private boolean mShouldCache = true;

volley默认启用缓存的。再往下看

                // 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");
                    Log.i("CacheDispatcher", "没有缓存数据:" + request.getUrl());
                    mNetworkQueue.put(request);
                    continue;
                }

                // If it is completely expired, just send it to the network.缓存已经过期,则重新请求网络
                if (entry.isExpired()) {
                    request.addMarker("cache-hit-expired");
                    request.setCacheEntry(entry);
                    Log.i("CacheDispatcher", "缓存数据过期:" + request.getUrl());
                    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.
                    Log.i("CacheDispatcher", "获取缓存数据:" + request.getUrl());
                    mDelivery.postResponse(request, response);
                } else {
                    // 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;

                    // 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;
            }
        }

直接从缓存类Cache获取,并经过几次验证,如果缓存合法则解析然后交给 UI线程分发出去。下面来看看具体的流程
技术分享

存储缓存
如果是第一次请求,或者缓存已过期,肯定是无法获取到缓存的,这时可根据上图分析,将会进入网络请求线程NetworkDispatcher,储存缓存毫无疑问也是在这里面实现的。

        //省略部分代码
                if (networkResponse.notModified && request.hasHadResponseDelivered()) {
                    request.finish("not-modified");
                    continue;
                }

                // 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.如果需要缓存,并且用户已经把网络请求的数据转换成一份为缓存数据,则通过 Cache 类把缓存存储。
                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);
            } catch (VolleyError volleyError) {
                parseAndDeliverNetworkError(request, volleyError);
            } catch (Exception e) {
                VolleyLog.e(e, "Unhandled exception %s", e.toString());
                mDelivery.postError(request, new VolleyError(e));
            }
            //省略部分代码

通过以上代码可以知道,在网络请求线程请求到数据以后,会交给用户解析,并把数据转换一份成缓存数据,通过 Cache 缓存操作类,把数据缓存起来。

网络数据转换成缓存数据
上面提到了,把网络数据转化成缓存数据,那么,volley 是如何转换的?

     */
    public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
        long now = System.currentTimeMillis();
        //获取网络请求数据的头信息
        Map<String, String> headers = response.headers;

        long serverDate = 0;
        long serverExpires = 0;
        long softExpire = 0;
        long maxAge = 0;
        boolean hasCacheControl = false;

        String serverEtag = null;
        String headerValue;
        //从头信息中获取 Date 数据
        headerValue = headers.get("Date");
        if (headerValue != null) {
            serverDate = parseDateAsEpoch(headerValue);
        }
        //从头信息中获取 Cache-Control 数据,来控制缓存
        headerValue = headers.get("Cache-Control");
        if (headerValue != null) {
            hasCacheControl = true;
            String[] tokens = headerValue.split(",");
            for (int i = 0; i < tokens.length; i++) {
                String token = tokens[i].trim();
                if (token.equals("no-cache") || token.equals("no-store")) {
                    return null;
                } else if (token.startsWith("max-age=")) {
                    try {
                        maxAge = Long.parseLong(token.substring(8));
                    } catch (Exception e) {
                    }
                } else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
                    maxAge = 0;
                }
            }
        }

        headerValue = headers.get("Expires");
        if (headerValue != null) {
            serverExpires = parseDateAsEpoch(headerValue);
        }

        serverEtag = headers.get("ETag");

        // Cache-Control takes precedence over an Expires header, even if both exist and Expires
        // is more restrictive.
        if (hasCacheControl) {
            softExpire = now + maxAge * 1000;
        } else if (serverDate > 0 && serverExpires >= serverDate) {
            // Default semantic for Expire header in HTTP specification is softExpire.
            softExpire = now + (serverExpires - serverDate);
        }

        Cache.Entry entry = new Cache.Entry();
        entry.data = response.data;
        entry.etag = serverEtag;
        entry.softTtl = softExpire;
        entry.ttl = entry.softTtl;
        entry.serverDate = serverDate;
        entry.responseHeaders = headers;

        return entry;
    }

前面我们提到的缓存是否过期和是否需要刷新,都是通过 response 的头部信息来判断,但是在BasicNetwork中只实现了缓存是否过期的操作,没有实现缓存是否需要刷新

    @Override
    public NetworkResponse performRequest(Request<?> request) throws VolleyError {
        long requestStart = SystemClock.elapsedRealtime();
        while (true) {
            HttpResponse httpResponse = null;
            byte[] responseContents = null;
            Map<String, String> responseHeaders = new HashMap<String, String>();
            try {
                // Gather headers.
                Map<String, String> headers = new HashMap<String, String>();
                addCacheHeaders(headers, request.getCacheEntry());
                httpResponse = mHttpStack.performRequest(request, headers);
                StatusLine statusLine = httpResponse.getStatusLine();
                int statusCode = statusLine.getStatusCode();

                responseHeaders = convertHeaders(httpResponse.getAllHeaders());
                //从这里开始设置缓存信息,如果设置了缓存的缓存时间,则把它添加到头部信息中,但是没有实现是否需要刷新缓存的操作,如果有需要,也可以在这里实现,这是就需要修改源码。
                if(request.getCacheTime() != 0){
                    responseHeaders.put("Cache-Control","max-age=" + request.getCacheTime());
                }
                // Handle cache validation.
                if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
                    return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED,
                            request.getCacheEntry().data, responseHeaders, true);
                }

                responseContents = entityToBytes(httpResponse.getEntity());
                // if the request is slow, log it.
                long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
                logSlowRequests(requestLifetime, request, responseContents, statusLine);

                if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_NO_CONTENT) {
                    throw new IOException();
                }
                return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
            } catch (SocketTimeoutException e) {
                attemptRetryOnException("socket", request, new TimeoutError());
            } catch (ConnectTimeoutException e) {
                attemptRetryOnException("connection", request, new TimeoutError());
            } catch (MalformedURLException e) {
                throw new RuntimeException("Bad URL " + request.getUrl(), e);
            } catch (IOException e) {
                int statusCode = 0;
                NetworkResponse networkResponse = null;
                if (httpResponse != null) {
                    statusCode = httpResponse.getStatusLine().getStatusCode();
                } else {
                    throw new NoConnectionError(e);
                }
                VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
                if (responseContents != null) {
                    networkResponse = new NetworkResponse(statusCode, responseContents,
                            responseHeaders, false);
                    if (statusCode == HttpStatus.SC_UNAUTHORIZED ||
                            statusCode == HttpStatus.SC_FORBIDDEN) {
                        attemptRetryOnException("auth",
                                request, new AuthFailureError(networkResponse));
                    } else {
                        // TODO: Only throw ServerError for 5xx status codes.
                        throw new ServerError(networkResponse);
                    }
                } else {
                    throw new NetworkError(networkResponse);
                }
            }
        }
    }

如何使用缓存

根据上面分析不难发现,要使用缓存,得具备两个条件,
1、启用缓存

public final void setShouldCache(boolean shouldCache) {
        mShouldCache = shouldCache;
    }

不过这个条件默认情况下是开启的。
2、设置缓存的时间

    public void setCacheTime(long cacheTime) {
        mCacheTime = cacheTime;
    }

这里 cacheTime 的单位是秒。
接下来看看具体用法

public class CacheRequestActivity extends ActionBarActivity {

    /*数据显示的View*/
    private TextView mIdTxt,mNameTxt,mDownloadTxt,mLogoTxt,mVersionTxt ;
    /*弹出等待对话框*/
    private ProgressDialog mDialog ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get);
        mIdTxt = (TextView) findViewById(R.id.id_id) ;
        mNameTxt = (TextView) findViewById(R.id.id_name) ;
        mDownloadTxt = (TextView) findViewById(R.id.id_download) ;
        mLogoTxt = (TextView) findViewById(R.id.id_logo) ;
        mVersionTxt = (TextView) findViewById(R.id.id_version) ;
        mDialog = new ProgressDialog(this) ;
        mDialog.setMessage("get请求中...");
        mDialog.show();
        /*请求网络获取数据*/
        MiNongApi.CacheObjectMiNongApi("minongbang", new ResponseListener<TestBean>() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mDialog.dismiss();
            }

            @Override
            public void onResponse(TestBean response) {
                Log.v("zgy","=======response======="+response) ;
                mDialog.dismiss();
                /*显示数据*/
                mIdTxt.setText(response.getId() + "");
                mNameTxt.setText(response.getName());
                mDownloadTxt.setText(response.getDownload() + "");
                mLogoTxt.setText(response.getLogo());
                mVersionTxt.setText(response.getVersion() + "");
            }
        });
    }

}

cache api

    public static void CacheObjectMiNongApi(String value,ResponseListener listener){
        String url ;
        try {
            url = Constant.MinongHost +"?test="+ URLEncoder.encode(value, "utf-8") ;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            url = Constant.MinongHost +"?test="+ URLEncoder.encode(value) ;
        }
        Request request = new GetObjectRequest(url,new TypeToken<TestBean>(){}.getType(),listener) ;
        //请用缓存
        request.setShouldCache(true);
        //设置缓存时间10分钟
        request.setCacheTime(10*60);
        VolleyUtil.getRequestQueue().add(request) ;
    }

再来看看效果图,在缓存存在的情况下当把网络连接断开的时候,依然能够获取到数据
技术分享

有一种情况需要注意:当需要获取缓存,且希望缓存刷新的时候,这种情况就需要修改 Volley 的源码,前面已经提到一点点,相信大家都能实现的。

源码下载地址

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!