深入volley(二)volley缓存细节

上一章讲解了volley缓存文件的结构与内容,没有看过的朋友请先阅读 深入Volley(一)volley缓存文件结构

本章主要内容来讲解volley的缓存细节,即volley如何处理c/s结构的http交互;


http缓存机制

要弄明白volley缓存机制,那么肯定是和浏览器的缓存机制有关了,简单来说volley整套框架要做的事都是模拟浏览器来进行一次次的http交互

     Cache-Control缓存

Cache-Control缓存属于浏览器返回的响应头字段,他是优先级最高的缓存字段,主要用来控制资源的有效时期,从而决定是浏览器直接拿缓存数据还是重发请求
其值有: publicprivateno-cacheno- storeno-transformmust-revalidateproxy-revalidatemax-age
(图片来自网络)
1.0

            依赖性Last-Modified/If-Modified-Since

Last-Modified:主要用来标记最后修改的时间,属于相应头之中
If-Modified-Since:当资源过期的时候(所有缓存过期),响应头发现有Last-Modified字段,则这时请求头中带上该字段,表示请求时间,这时服务器会将该字段时间和最后修改时间进行一个比对操作,如果发现最后修改时间新一点就是http 200响应,反之则为http 304响应;

           依赖性Etag/If-None-Match(tomcat 7.0新引入)

相比于Last-modified/If-Modified-Since字段来说,则相对优先级比较低一点,的操作同样是用来进行确定是200 还是 304;
Etag:位于响应头中,他是服务器资源唯一的标示符,默认计算是通过对文件的索引节(INode),大小(Size)和最后修改时间(MTime)进行Hash后得到的
  If-None-Match : 当资源过期时,发现资源是存在Etag响应字段的则再次向服务器请求时通过该字段带上Etag的值,服务器收到后对两者进行比对,用来决定返回200还是304;
Etag存在的原因:Etag和Last-Modified完全是两种不同的缓存模式,如果Last-Modified会因为refresh而进行定期刷新的话、或者说服务器时间缓存是用的不准确,不推荐用Last-Modified字段的话,那么用Etag就是最好的选择;

    Expires缓存

Expires缓存是Http1.0提出来的缓存概念,他的优先级是低于Cache-Control
有朋友之前讨论的时候问我:http1.0的东西在http1.1是不是失效了?
我拿Fidder抓Chrome包
对比一下抓IE 8包
后面一堆长的省略了
奇怪了:chrome缓存是失效的,IE缓存是有效的,这是因为Chrome抽分的请求加上了cache字段,这些字段导致了Expires不可用,然后我发现只要有Expires字段的响应头,他居然都给加上这样的东西;具体chrome为什么我也搞不太懂(搜索了很久没找到答案),希望懂的朋友指点一二;
Expires具体缓存:Expires位于响应头里面,在响应http请求时告诉浏览器在过期时间前浏览器可以直接从浏览器缓存取数据,而无需再次请求。

     浏览器缓存流程

图片来源:http://www.cnblogs.com/skynet/archive/2012/11/28/2792503.html)
 这里还要注明一点:在判断是否有缓存的时候是先Cache-Control再Expires的如果都没有就进行依赖性的两字段判断操作;

Volley缓存处理

分析了那么多终于要到正文了,(必须要吐槽下,最近很莫名其妙啊)
这里必须再强调一点:一切在client有关http头的应用都是进行模拟浏览器的操作;所以具体流程细节我会注重描述缓存这一过程;
代码从RequestQueue的add方法开始,当你add进来一个request的时候,其会根据request.shouldCache进行分发决定是交给NetworkDispatcher还是CacheDispatcher处理

NetworkDispatcher处理

NetworkDispatcher接收到request有两种途径:
1.直接接收,通过RequestQueue的分发,一般是第一次请求或者之前请求没有设置过缓存
2.在CacheDispatcher中处理过的request(其内Cache.Entry被处理过,不为空)发现缓存过期了会丢给NetworkDispatcher;这里就和304有关
当NetworkDispatcher接收到一个request的时候,其会交给Network子类BasicNetwork调用performRequest处理
                NetworkResponse networkResponse = mNetwork.performRequest(request);
在BasicNetwork.performRequest重点关注addCacheHeaders(Map<String, String> headers, Cache.Entry entry) 这个方法的调用
    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));
        }
    }
这里就和304有关,这里传递进来的request如果Entry 不为空(通过第二种获取request的方法交给 NetworkDispatcher消费),他就会进行是否有Etag和 Last-Modified设置的操作;
在BasicNetwork调用完addCacheHeaders之后会得到一个Map(即传递进去第一个参数,该方法将其装载),然后他又调用HttpStack的子类(方便起见只谈HurlStack)的performRequest将request和之前得到的map装进去,

    @Override
    public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders)
            throws IOException, AuthFailureError {
        String url = request.getUrl();
        HashMap<String, String> map = new HashMap<String, String>();
        map.putAll(request.getHeaders());
        map.putAll(additionalHeaders);
        if (mUrlRewriter != null) {
            String rewritten = mUrlRewriter.rewriteUrl(url);
            if (rewritten == null) {
                throw new IOException("URL blocked by rewriter: " + url);
            }
            url = rewritten;
        }
        URL parsedUrl = new URL(url);
        HttpURLConnection connection = openConnection(parsedUrl, request);
        for (String headerName : map.keySet()) {
            connection.addRequestProperty(headerName, map.get(headerName));
        }
        setConnectionParametersForRequest(connection, request);
        // Initialize HttpResponse with data from the HttpURLConnection.
        ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
        int responseCode = connection.getResponseCode();
        if (responseCode == -1) {
            // -1 is returned by getResponseCode() if the response code could not be retrieved.
            // Signal to the caller that something was wrong with the connection.
            throw new IOException("Could not retrieve response code from HttpUrlConnection.");
        }
        StatusLine responseStatus = new BasicStatusLine(protocolVersion,
                connection.getResponseCode(), connection.getResponseMessage());
        BasicHttpResponse response = new BasicHttpResponse(responseStatus);
        response.setEntity(entityFromConnection(connection));
        for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
            if (header.getKey() != null) {
                Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
                response.addHeader(h);
            }
        }
        return response;
    }
这里可以看到该方法将所有request的请求头都装到了一个map里面去,之后通过HttpUrlRequest请求得到了一个BasicHttpResponse,这个类不是android封包的类,我看了下,他是属于org.apache.http.message.BasicHttpResponse;这个包里面,也就是apache封包的类;之所以这么做应该是为了与另外httpClient那边统一一个接口,要不然可复用的代码会降得很低;不过不管他封装成什么类都没关系,因为之后他会重新封装;
继续可以看到从HttpStack返回的一个HttpResponse在BasicNetwork将会被重新封装
public BasicNetwork(HttpStack httpStack, ByteArrayPool pool) {
        mHttpStack = httpStack;
        mPool = pool;
    }

    @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());
                // Handle cache validation.
                if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
                    return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED,
                            request.getCacheEntry() == null ? null : request.getCacheEntry().data,
                            responseHeaders, true);
                }

                // Some responses such as 204s do not have content.  We must check.
                if (httpResponse.getEntity() != null) {
                  responseContents = entityToBytes(httpResponse.getEntity());
                } else {
                  // Add 0 byte response as a way of honestly representing a
                  // no-content request.
                  responseContents = new byte[0];
                }

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

                if (statusCode < 200 || statusCode > 299) {
                    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);
                }
            }
        }
    }
看代码可以看到通过HttpResponse重新封装,得到三个最有用的内容statusCode,responseContents和responseHeaders:并通过这三个内容封装成了一个NetworkResponse返还给了NetworkDispatcher;

当NetworkDispatcher接收到NetworkResponse后,先就是进行判断304的操作
                // If the server returned 304 AND we delivered a response already,
                // we're done -- don't deliver a second identical response.
                if (networkResponse.notModified && request.hasHadResponseDelivered()) {
                    request.finish("not-modified");
                    continue;
                }
如果有304的话这次请求就会被消耗掉,响应也是会直接从之前的内容取
如果没有304的话那么就需要进行一次新的请求,开始回调request的parseNetworkResponse方法,因为这里它需要得到一个Response类
    @Override
    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(NetworkResponse response)静态方法的调用;
parseCacheHeaders的处理其实很简单,就是将NetworkResponse进行封装成一个Cache.Entry对象
 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;

        headerValue = headers.get("Date");
        if (headerValue != null) {
            serverDate = parseDateAsEpoch(headerValue);
        }

        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;
    }
在request回调parseNetworkResponse的拿到Cache.Entry后封装成Response返回NetworkDispatcher;
NetworkDispatcher中,再根据其shouldCache和是否有缓存实体来判断是否要进行缓存操作
                // 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");
                }

     CacheDispatcher的处理

CacheDispatcher处理就简单多了
 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) {
                    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()) {
                    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 {
                    // 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;

<span style="white-space:pre">		</span>    // 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;
            }
        }
    }
当收到一个request之后,会先到Cache里面去取,看下是否有缓存,
当出现没有缓存 和 缓存过期的情况就直接丢给 NetworkDispatcher来处理, NetworkDispatcher再会进行304判断;
如果缓存没过期,直接拿的缓存实体丢给request的parseNetworkResponse方法这里调用就和 NetworkDispatcher里面处理差不多了;



架构示意图:





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值