OkHttpClient源码分析(四)—— CacheInterceptor

CacheInterceptor

介绍完缓存以后,如今开始介绍缓存拦截器CacheInterceptor了,一样也是查看其intercept()方法,这里边上片断代码边解析,化整为零:缓存

@Override public Response intercept(Chain chain) throws IOException {
    Response cacheCandidate = cache != null
        ? cache.get(chain.request())
        : null;
    ...
}
复制代码

首先经过判断缓存对象是否为null,若是不为null则根据传入的Chain对象的request获取缓存的Response。bash

@Override public Response intercept(Chain chain) throws IOException {
      ...
      CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
      Request networkRequest = strategy.networkRequest;
      Response cacheResponse = strategy.cacheResponse;
      ...
}
复制代码

建立缓存策略对象CacheStrategy,CacheStrategy内部维护了一个Request和一个Response,该类主要是用于解决到底使用网络仍是缓存,亦或是二者皆用:服务器

/** 若是不使用网络,则 networkRequest为 null */
  public final @Nullable Request networkRequest;
  /** 若是不使用缓存,则 cacheResponse为 null */
  public final @Nullable Response cacheResponse;
复制代码

关于缓存策略的建立,咱们查看CacheStrategy的内部类Factory的get()方法:网络

public CacheStrategy get() {
      CacheStrategy candidate = getCandidate();

      if (candidate.networkRequest != null && request.cacheControl().onlyIfCached()) {
        // We're forbidden from using the network and the cache is insufficient. return new CacheStrategy(null, null); } return candidate; } 复制代码

该方法比较简单,建立CacheStrategy的主要的逻辑是在getCandidate()方法中:ide

private CacheStrategy getCandidate() {
      //找不到缓存,须要网络请求
      if (cacheResponse == null) {
        return new CacheStrategy(request, null);
      }

      // 若是是https请求且缺乏握手操做,须要网络请求
      if (request.isHttps() && cacheResponse.handshake() == null) {
        return new CacheStrategy(request, null);
      }

      // 判断网络请求该不应缓存下来,不应缓存则,须要网络请求
      if (!isCacheable(cacheResponse, request)) {
        return new CacheStrategy(request, null);
      }

      
      // 若是指定不缓存或者是可选择的请求,须要网络请求
      CacheControl requestCaching = request.cacheControl();
      if (requestCaching.noCache() || hasConditions(request)) {
        return new CacheStrategy(request, null);
      }
      
      //上述须要网络请求返回的 CacheStrategy 第一个参数传入request
        
      //若是缓存是不受影响的,CacheStrategy传入cacheResponse
      CacheControl responseCaching = cacheResponse.cacheControl();
      if (responseCaching.immutable()) {
        return new CacheStrategy(null, cacheResponse);
      }

      
      //能够缓存,添加请求头信息
      if (!responseCaching.noCache() && ageMillis + minFreshMillis < freshMillis + maxStaleMillis) {
        Response.Builder builder = cacheResponse.newBuilder();
        if (ageMillis + minFreshMillis >= freshMillis) {
          builder.addHeader("Warning", "110 HttpURLConnection \"Response is stale\"");
        }
        long oneDayMillis = 24 * 60 * 60 * 1000L;
        if (ageMillis > oneDayMillis && isFreshnessLifetimeHeuristic()) {
          builder.addHeader("Warning", "113 HttpURLConnection \"Heuristic expiration\"");
        }
        return new CacheStrategy(null, builder.build());
      }

      ...
    }

复制代码

介绍完CacheStrategy对象和建立CacheStrategy的过程后,接着分析intercept()方法:源码分析

@Override public Response intercept(Chain chain) throws IOException {
    ...
    
    //若是有缓存,更新统计指标, 增长命中率
    if (cache != null) {
      cache.trackResponse(strategy);
    }

    ...
   
    //若是当前没有网络且找不到缓存
    if (networkRequest == null && cacheResponse == null) {
      return new Response.Builder()
          .request(chain.request())
          .protocol(Protocol.HTTP_1_1)
          .code(504)
          .message("Unsatisfiable Request (only-if-cached)")
          .body(Util.EMPTY_RESPONSE)
          .sentRequestAtMillis(-1L)
          .receivedResponseAtMillis(System.currentTimeMillis())
          .build();
    }
    
    ...
}

复制代码

若是有缓存,经过调用CacheStrategy的trackResponse()方法更新统计指标, 更新网络请求数和命中缓存数;post

接着经过判断CacheStrategy的networkRequest和cacheResponse,若是两者同时为null,即当前没有网络且没有缓存,则构造一个Response对象并返回,其中状态码为504,并设置了提示的message信息。ui

接着分析:spa

@Override public Response intercept(Chain chain) throws IOException {
    ...
      //若是不使用网络请求,直接返回缓存的Response
     if (networkRequest == null) {
      return cacheResponse.newBuilder()
          .cacheResponse(stripBody(cacheResponse))
          .build();
     }
     
    
    //调用下一个拦截器进行处理  
    Response networkResponse = null;
    try {
      networkResponse = chain.proceed(networkRequest);
    } finally {
      // If we're crashing on I/O or otherwise, don't leak the cache body.
      if (networkResponse == null && cacheCandidate != null) {
        closeQuietly(cacheCandidate.body());
      }
    }
    
    
    //本地有缓存
    if (cacheResponse != null) {
      //服务器返回状态码为HTTP_NOT_MODIFIED(304)
      if (networkResponse.code() == HTTP_NOT_MODIFIED) {
        //使用缓存数据
        Response response = cacheResponse.newBuilder()
             ...
            .build();
            ...
      } else {
        closeQuietly(cacheResponse.body());
      }
    }
    
    
    //若是服务器资源已经修改,使用网络响应返回的最新数据
    Response response = networkResponse.newBuilder()
        .cacheResponse(stripBody(cacheResponse))
        .networkResponse(stripBody(networkResponse))
        .build();
        
    
    //若是有缓存
    if (cache != null) {
      //htpp 头部有响应体且须要缓存
      if (HttpHeaders.hasBody(response) && CacheStrategy.isCacheable(response, networkRequest)) {
        CacheRequest cacheRequest = cache.put(response);
        return cacheWritingResponse(cacheRequest, response);
      }
      //请求方法不符合可以缓存
      if (HttpMethod.invalidatesCache(networkRequest.method())) {
        try {
          //移除对应的request
          cache.remove(networkRequest);
        } catch (IOException ignored) {
        }
      }
    }

    return response;
  }
复制代码

当服务器返回状态码为HTTP_NOT_MODIFIED即304时,说明缓存还没过时或服务器资源没修改,此时返回缓存;若是服务器资源修改了,则使用网络响应返回的最新数据构造Response,接着将最新的数据缓存并移除无效的缓存。.net

总结CacheInterceptor主要作的操做:

  1. 从缓存中获取Reponse对象,赋值给caceResponse,若是找不到缓存则为null;
  2. 根据当前请求request和caceResponse 构建一个CacheStrategy对象;
  3. CacheStrategy这个策略对象将根据相关规则来决定cacheResponse和Request是否有效,若是无效则分别将cacheResponse和request设置为null;
  4. 若是request和cacheResponse都为null,即没有网络且没有缓存,直接返回一个状态码为504的空Respone对象;
  5. 若是resquest为null而cacheResponse不为null,即没有网络且有缓存,则直接返回cacheResponse对象;
  6. 执行下一个拦截器的intercept()方法进行网络请求,获取response;
  7. 若是服务器资源没有过时(状态码304)且存在缓存,则返回缓存;
  8. 若是服务器资源有修改,则将返回的最新数据进行缓存并移除掉无效的缓存,最后返回response对象给上一个拦截器。

下一篇将讲解五大拦截器中的最后两个拦截器, ConnectInterceptor和CallServerInterceptor,感兴趣的朋友能够继续阅读:

OkHttpClient源码分析(五)—— ConnectInterceptor和CallServerInterceptor

相关文章
相关标签/搜索