okhttp缓存大致内容(3.12)

2019-08-04  本文已影响0人  虚假雨

在最近的工作中接触到了okhttp的缓存,借此机会记录下。网上已经有很多相关文章了,比如参考文章一还包括了http头缓存的相关介绍;但是一来自己跟代码记得比较牢,二来我比较关心缓存中lastModified和etag的设置所以需要挑重点,所以还是自己下手了。
本文基于okhttp 3.12版本

开启缓存

okhttp开启缓存非常方便,只需要设置cache参数即可

//3mb缓存
final int cacheSize = 1024 * 1024 * 3;
defaultBuilder = new OkHttpClient.Builder()
.cache(new Cache(CmGameSdkConstant.getAppContext().getCacheDir(), cacheSize))
.connectTimeout(DEFAULT_CONNECT_TIME_SEC, TimeUnit.SECONDS);

这样设置之后,okhttp就开启了缓存,包括本地缓存以及浏览器相关缓存(etag、last-modify),这一点我一开始是不信的(原先没有关注这方面),看完之后就。。。真香

缓存类Cache内部设置

在第一步中我们开启了缓存,也就是开启了cache选项,其中传入了Cache对象。我们看看这个对象做了些什么,首先就是get,这里根据请求拿到缓存对应的回复,这里是使用LruCache做本地缓存的。

@Nullable Response get(Request request) {
    String key = key(request.url());
    DiskLruCache.Snapshot snapshot;
    Entry entry;
    try {
      snapshot = cache.get(key);
      if (snapshot == null) {
        return null;
      }
    } catch (IOException e) {
      // Give up because the cache cannot be read.
      return null;
    }

    try {
      entry = new Entry(snapshot.getSource(ENTRY_METADATA));
    } catch (IOException e) {
      Util.closeQuietly(snapshot);
      return null;
    }

    Response response = entry.response(snapshot);

    if (!entry.matches(request, response)) {
      Util.closeQuietly(response.body());
      return null;
    }

    return response;
  }

  // 这里是将请求映射为url,使用md5计算
  public static String key(HttpUrl url) {
    return ByteString.encodeUtf8(url.toString()).md5().hex();
  }

缓存任务拦截器 CacheInterceptor

在请求流程中,是用链式分发的形式走的,大概如下:

Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    List<Interceptor> interceptors = new ArrayList<>();
    interceptors.addAll(client.interceptors());
    interceptors.add(retryAndFollowUpInterceptor);
    interceptors.add(new BridgeInterceptor(client.cookieJar()));
    interceptors.add(new CacheInterceptor(client.internalCache()));
    interceptors.add(new ConnectInterceptor(client));
    if (!forWebSocket) {
      interceptors.addAll(client.networkInterceptors());
    }
    interceptors.add(new CallServerInterceptor(forWebSocket));

    Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
        originalRequest, this, eventListener, client.connectTimeoutMillis(),
        client.readTimeoutMillis(), client.writeTimeoutMillis());

    return chain.proceed(originalRequest);
  }

其中缓存的关键类就是CacheInterceptor
我们来看看他的拦截关键方法

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

一开始根据请求去获取缓存的回复

拿到历史缓存后,加上当前时间构建Request以及Response对象

long now = System.currentTimeMillis();

CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
Request networkRequest = strategy.networkRequest;
Response cacheResponse = strategy.cacheResponse;

这个缓存策略CacheStrategy很重要,基本上后面的匹配都与他有关。这里根据请求和缓存回复构建,后面关于http的匹配都是在里面。

在构造方法里,我们看到他将解决请求时间、etag以及lastModify等信息都保存下来了,为之后的缓存氢气做准备

CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();

//看下Factory的构造方法
 public Factory(long nowMillis, Request request, Response cacheResponse) {
      this.nowMillis = nowMillis;
      this.request = request;
      this.cacheResponse = cacheResponse;

      if (cacheResponse != null) {
        this.sentRequestMillis = cacheResponse.sentRequestAtMillis();
        this.receivedResponseMillis = cacheResponse.receivedResponseAtMillis();
        Headers headers = cacheResponse.headers();
        for (int i = 0, size = headers.size(); i < size; i++) {
          String fieldName = headers.name(i);
          String value = headers.value(i);
          //这里用来保存服务器时间,为之后的缓存过期做准备
          if ("Date".equalsIgnoreCase(fieldName)) {
            servedDate = HttpDate.parse(value);
            servedDateString = value;
          } else if ("Expires".equalsIgnoreCase(fieldName)) {
            expires = HttpDate.parse(value);
          } else if ("Last-Modified".equalsIgnoreCase(fieldName)) {
            lastModified = HttpDate.parse(value);
            lastModifiedString = value;
          } else if ("ETag".equalsIgnoreCase(fieldName)) {
            etag = value;
          //表示收到的时间,同样是为了后面过期做准备
          } else if ("Age".equalsIgnoreCase(fieldName)) {
            ageSeconds = HttpHeaders.parseSeconds(value, -1);
          }
        }
      }
    }

随后我们会开始构建CacheStrategy,也就是缓存策略,缓存会最终体现在CacheStrategy的networkRequest和cacheResponse中,也就是他的构造函数。最终实现方法在getCandidate()中:

 private CacheStrategy getCandidate() {
      // No cached response.
      // 没有缓存,那直接返回
      if (cacheResponse == null) {
        return new CacheStrategy(request, null);
      }

      // Drop the cached response if it's missing a required handshake.
      // handshake是握手的意思,如果请求为https而且没有经过握手,那也不缓存
      if (request.isHttps() && cacheResponse.handshake() == null) {
        return new CacheStrategy(request, null);
      }

      // If this response shouldn't have been stored, it should never be used
      // as a response source. This check should be redundant as long as the
      // persistence store is well-behaved and the rules are constant.
      // 这里是判断是否不允许缓存,比如返回头中的no-store,注意这里同步比较了请求和返回的cache策略
      if (!isCacheable(cacheResponse, request)) {
        return new CacheStrategy(request, null);
      }
      
      // 第一个判断是请求是否允许缓存,第二个是请求中如果已经带了If-Modified-Since或者If-None-Match,说明上层自己做了缓存
      CacheControl requestCaching = request.cacheControl();
      if (requestCaching.noCache() || hasConditions(request)) {
        return new CacheStrategy(request, null);
      }

      CacheControl responseCaching = cacheResponse.cacheControl();
      
      //cacheResponseAge 缓存时间的计算大概如下
      //return receivedAge + responseDuration + residentDuration;
      //也就是请求到收到时间+现在使用时间
      long ageMillis = cacheResponseAge();
      //通过之前缓存的expires、last-modify等计算缓存有效期
      long freshMillis = computeFreshnessLifetime();
      
      // 如果请求中已经包含了max-age,那么我们取相对最小值
      if (requestCaching.maxAgeSeconds() != -1) {
        freshMillis = Math.min(freshMillis, SECONDS.toMillis(requestCaching.maxAgeSeconds()));
      }
      // Cache-Control相关    
      // min-fresh是指在期望的时间内响应有效
      long minFreshMillis = 0;
      if (requestCaching.minFreshSeconds() != -1) {
        minFreshMillis = SECONDS.toMillis(requestCaching.minFreshSeconds());
      }

      // 这个同上,mustRevalidate是指在本地副本未过期前可以使用,否则必须刷新
      long maxStaleMillis = 0;
      if (!responseCaching.mustRevalidate() && requestCaching.maxStaleSeconds() != -1) {
        maxStaleMillis = SECONDS.toMillis(requestCaching.maxStaleSeconds());
      }

      // 还没过期,加个warning信息返回上层,随即复用缓存
      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());
      }

      // Find a condition to add to the request. If the condition is satisfied, the response body
      // will not be transmitted.
      // 这里也有点意思,etag优先,然后是If-Modified-Since,然后是服务器时间,进行拼接,向服务器请求
      String conditionName;
      String conditionValue;
      if (etag != null) {
        conditionName = "If-None-Match";
        conditionValue = etag;
      } else if (lastModified != null) {
        conditionName = "If-Modified-Since";
        conditionValue = lastModifiedString;
      } else if (servedDate != null) {
        conditionName = "If-Modified-Since";
        conditionValue = servedDateString;
      } else {
        // 如果这些都没有了,那只能走普通的网络请求了
        return new CacheStrategy(request, null); // No condition! Make a regular request.
      }
        
      // 最后就是我们的集大成者了,header内部是用键值对维护的
      // 我们重新修改后,生成“附带属性”的conditionalRequest
      Headers.Builder conditionalRequestHeaders = request.headers().newBuilder();
      Internal.instance.addLenient(conditionalRequestHeaders, conditionName, conditionValue);
    
      Request conditionalRequest = request.newBuilder()
          .headers(conditionalRequestHeaders.build())
          .build();
      return new CacheStrategy(conditionalRequest, cacheResponse);
    }

上面是缓存的大头,构建出了我们想要的内容,同时先过滤一次网络请求,如果本地缓存可用则先用本地缓存,这个时候request是为空的。

下面我们回到拦截方法CacheInterceptor.intercept

// If we're forbidden from using the network and the cache is insufficient, fail
// 这里是禁止使用网络的情况下缓存又无效,所以直接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 we don't need the network, we're done.
    // 如果缓存未过期同时又没有网络,直接返回缓存结果
    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 we have a cache response too, then we're doing a conditional get.
    if (cacheResponse != null) {
      if (networkResponse.code() == HTTP_NOT_MODIFIED) {
        // 如果是传说中的304,那么缓存可用,我们主动构建一个回复
        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 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.
        }
      }
    }
    

参考文章

  1. https://www.jianshu.com/p/00d281c226f6 okhttp缓存处理
  2. https://blog.csdn.net/aiynmimi/article/details/79807036 OkHttp3源码分析之缓存Cache
上一篇下一篇

猜你喜欢

热点阅读