OkHttp3(十二)--CacheInterceptor

CacheInterceptor

用来负责读取缓存以及更新缓存的

  • 读取候选缓存
  • 创建缓存策略
  • 根据缓存策略决定报错、使用缓存、继续执行Interceptor,即ConnectInterceptor
  • 接收到网络结果,如果响应code式304,则使用缓存,返回缓存结果
  • 读取网络结果
  • 对数据进行缓存
  • 返回网络读取的结果

CacheInterceptor部分源码

  @Override 
  public Response intercept(Chain chain) throws IOException {
    //读取候选缓存
    Response cacheCandidate = cache != null
        ? cache.get(chain.request())
        : null;

    long now = System.currentTimeMillis();
    //创建缓存策略,强制缓存、对比缓存等
    CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
    Request networkRequest = strategy.networkRequest;
    Response cacheResponse = strategy.cacheResponse;

    if (cache != null) {
      cache.trackResponse(strategy);
    }

    if (cacheCandidate != null && cacheResponse == null) {
      closeQuietly(cacheCandidate.body()); // The cache candidate wasn't applicable. Close it.
    }

    // 根据策略,不使用网络,又没有缓存的直接报错,并返回错误码504
    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();
    }

    //根据策略,不使用网络,有缓存的直接返回
    if (networkRequest == null) {
      return cacheResponse.newBuilder()
          .cacheResponse(stripBody(cacheResponse))
          .build();
    }

    Response networkResponse = null;
    try {
      //前面两个都没有返回,继续执行下一个Interceptor,即ConnectInterceptor
      networkResponse = chain.proceed(networkRequest);
    } finally {
      if (networkResponse == null && cacheCandidate != null) {
        closeQuietly(cacheCandidate.body());
      }
    }

    //接收到网络结果,如果响应code式304,则使用缓存,返回缓存结果
    if (cacheResponse != null) {
      if (networkResponse.code() == HTTP_NOT_MODIFIED) {
        Response response = cacheResponse.newBuilder()
            .headers(combine(cacheResponse.headers(), networkResponse.headers()))
            .sentRequestAtMillis(networkResponse.sentRequestAtMillis())
            .receivedResponseAtMillis(networkResponse.receivedResponseAtMillis())
            .cacheResponse(stripBody(cacheResponse))
            .networkResponse(stripBody(networkResponse))
            .build();
        networkResponse.body().close();

        // Update the cache after combining headers but before stripping the
        // Content-Encoding header (as performed by initContentStream()).
        cache.trackConditionalCacheHit();
        cache.update(cacheResponse, response);
        return response;
      } else {
        closeQuietly(cacheResponse.body());
      }
    }
    //读取网络结果
    Response response = networkResponse.newBuilder()
        .cacheResponse(stripBody(cacheResponse))
        .networkResponse(stripBody(networkResponse))
        .build();
    //对数据进行缓存
    if (cache != null) {
      if (HttpHeaders.hasBody(response) && CacheStrategy.isCacheable(response, networkRequest)) {
        // Offer this request to the cache.
        CacheRequest cacheRequest = cache.put(response);
        return cacheWritingResponse(cacheRequest, response);
      }
      //返回网络读取的结果
      if (HttpMethod.invalidatesCache(networkRequest.method())) {
        try {
          cache.remove(networkRequest);
        } catch (IOException ignored) {
          // The cache cannot be written.
        }
      }
    }
    return response;
  }
  1. 判断使用缓存还是进行网络请求
  2. 不使用缓存时进行下一步网络拦截
  3. 缓存并返回网络数据
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 上一篇文章中我们介绍了OkHttp3的同步和异步请求流程,我们分析到不论是同步还是异步请求,都是通过RealCal...
    AntDream阅读 2,106评论 0 11
  • 在《打车APP实战》课程中,我们使用 OkHttp3 简单搭建了一个网络框架, 实践了 OkHttp3 的用法。不...
    大利猫阅读 1,832评论 0 25
  • 前言 做React Native的时候遇到业务线反馈的一个Bug:在使用Charles做代理的时候,将reactT...
    灵丞阅读 12,813评论 2 36
  • OkHttp3系列文章 OkHttp3 源码解析执行流程OkHttp3 源码解析 连接池的复用 如果有了解过OkH...
    Gillben阅读 1,113评论 0 3
  • 运动也好,读书也罢,哪怕是工作上,抑或是思想上,人们都在形式与内容两者之间纠缠。基本上在追求形式的多样化与丰度,以...
    自觉会阅读 478评论 0 0