okhttp拦截器之CacheInterceptor

1、背景

最近看了点http缓存相关点东西,之前看了下okhttp源码,但是看得一知半解,现在结合http缓存再看一次。

2、http缓存相关

2.1、http缓存的实现方式

http是通过header来控制缓存的,缓存方式分两种:

1、强制缓存

缓存是否过期不需要经过服务器确认,直接使用缓存。

实现方式: 响应包含Expires或者Cache-Control来指定响应过期时间,该有效时间内缓存可继续使用。Expires是http 1.0的字段,当两者同时存在时,优先使用Cache-Control。

2、协商缓存

缓存是否过期需要经过服务器确认

实现方式: 响应返回包含Last-Modified或者Etag的响应头。客户端请求时带上请求头If-Modified-Since或者If-None-Match,服务器进行对比后判断缓存是否可用,若缓存可用,则返回304状态码(此时响应不包含body),否者返回新的资源。Etag优先于Last-Modified。

  • Last-Modified:资源最后修改的时间
  • Etag:资源标志码

强制缓存优先于协商缓存。

2.2、http缓存相关指令
2.2.1、请求类
  • no-store:请求跟响应都不缓存
  • no-cache:忽略缓存,强制向源服务器请求
  • max-age(s):响应的最大age值
  • max-stale(s):可接受过期响应,但过期时间必须小于指定值,若不指定时间,则无限大
  • min-fresh(s):可接受缓存刷新时间大于缓存age和min-fresh之和的缓存
  • only-if-cached:只从缓存里取
2.2.2、响应类
  • no-store:请求跟响应都不缓存
  • no-cache:缓存是否可用需要向服务器验证
  • public:任何用户都可缓存,如缓存服务器、cdn等
  • private:只有指定用户可以缓存,比如浏览器
  • must-revalidate:可缓存,但是需要跟源服务器确认,存在时忽略max-stale
  • max-age:响应的最大age值
  • s-maxage:缓存服务器响应的最大age值

3、CacheInterceptor分析

首先要使用缓存,必须要在初始化OkhttpClient时就设置好,默认是无缓存的:

/** Sets the response cache to be used to read and write cached responses. */
    public Builder cache(@Nullable Cache cache) {
      this.cache = cache;
      this.internalCache = null;
      return this;
    }

之后从缓存中取出缓存响应:

@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;

通过CacheStrategy.Factory来拿到缓存策略:

/** Returns a strategy to use assuming the request can use the network. */
    private CacheStrategy getCandidate() {
      // 没有缓存,直接返回
      if (cacheResponse == null) {
        return new CacheStrategy(request, null);
      }

      // https协议,但是缺乏tls握手
      if (request.isHttps() && cacheResponse.handshake() == null) {
        return new CacheStrategy(request, null);
      }
      
      //判断缓存和请求是否可用(根据缓存响应码且请求和缓存的Cache-Control不为no-store)
      if (!isCacheable(cacheResponse, request)) {
        return new CacheStrategy(request, null);
      }
	  //请求为no-cache或请求头包含If-Modified-Since或If-None-Match,则缓存不可用
      CacheControl requestCaching = request.cacheControl();
      if (requestCaching.noCache() || hasConditions(request)) {
        return new CacheStrategy(request, null);
      }
      
      //------处理强制缓存------
        
      //计算缓存到现在为止的存活时间
      long ageMillis = cacheResponseAge();
      //计算缓存有效期,根据max-age等
      long freshMillis = computeFreshnessLifetime();
	  //如果请求带有max-age,则取最小的
      if (requestCaching.maxAgeSeconds() != -1) {
        freshMillis = Math.min(freshMillis, SECONDS.toMillis(requestCaching.maxAgeSeconds()));
      }
      //请求带有min-fresh
       long minFreshMillis = 0;
      if (requestCaching.minFreshSeconds() != -1) {
        minFreshMillis = SECONDS.toMillis(requestCaching.minFreshSeconds());
      }
      
      //计算max-stale:缓存过期后仍然可用的时间
      long maxStaleMillis = 0;
      CacheControl responseCaching = cacheResponse.cacheControl();
      //请求带有max-stale且缓存不包含must-revalidate(会令max-stale无效)
      if (!responseCaching.mustRevalidate() && requestCaching.maxStaleSeconds() != -1) {
        maxStaleMillis = SECONDS.toMillis(requestCaching.maxStaleSeconds());
      }
        
      
      //响应带有no-cache时需要向服务器询问缓存是否可用
      //计算缓存是否可直接使用:缓存存活时间 + min-fresh < 缓存有效时间 + 缓存过期仍然有效的时间
      if (!responseCaching.noCache() && ageMillis + minFreshMillis < freshMillis + maxStaleMillis) {
        Response.Builder builder = cacheResponse.newBuilder();
        //如果缓存存活时间超过了有效时间,说明缓存过期了(max-stale生效)
        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());
      }
      
      //------处理协商缓存------
        
      //即处理ETag Last-Modified
      String conditionName;
      String conditionValue;
      //Etag/If-None-Match优先
      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.
      }
      //添加上面的请求头进来
      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);
    }

拿到缓存策略CacheStrategy之后,如果参数networkRequest不为空,则说明需要进行网络请求。

/**
     * Returns a strategy to satisfy {@code request} using the a cached response {@code response}.
     */
    public CacheStrategy get() {
      CacheStrategy candidate = getCandidate();
	  //处理本地缓存无法直接使用或者没有缓存,而请求头又带有if-only-cache(只能用缓存)的情况
      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;
    }

之后处理if-only-cache和缓存冲突的情况。这时已经处理完了缓存的所有情况。

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

    //only-if-cached的情况,即缓存不可用,但是必须从缓存读取,直接返回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();
    }

    //networkRequest == null说明强制缓存生效,缓存可用
    if (networkRequest == null) {
      return cacheResponse.newBuilder()
          .cacheResponse(stripBody(cacheResponse))
          .build();
    }
    //否则说明是协商缓存或者无缓存,进行网络请求
    Response networkResponse = null;
    try {
      //责任链继续往后执行,向服务器请求并返回结果
      networkResponse = chain.proceed(networkRequest);
    } finally {
      ...
    }
  
    //cacheResponse != null说明是协商缓存
    if (cacheResponse != null) {
      //协商缓存返回304说明缓存依然有效,可以使用
      if (networkResponse.code() == HTTP_NOT_MODIFIED) {
        //更新本地缓存的headers
        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();
        ...
        //返回更新后的缓存
        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;

缓存策略中networkRequest表示是否需要进行网络请求,cacheResponse表示缓存是否存在。分几种情况:

  1. networkRequest == null && cacheResponse == null 返回504错误(only-if-cached)
  2. networkRequest == null && cacheResponse != null 强制缓存,缓存生效
  3. networkRequest != null && cacheResponse != null 协商缓存,需要向服务器确认
  4. networkRequest != null && cacheResponse == null 无缓存,需要向服务器请求

4、总结

虽然看了不少网上资料但是都感觉解释得不太好,看得一知半解,代码也是边看边查资料再结合自己的理解分析下来的,还有许多不懂的地方。http相关的知识还是看书比较全面、靠谱。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OkHttp是一个用于处理HTTP请求的开源Java库。它提供了一个拦截器机制,可以在发送请求和接收响应之前对它们进行修改和处理。以下是关于OkHttp拦截器的一些介绍和示例: 1. OkHttp拦截器是一个接口,它有一个方法intercept(Chain chain),该方法接收一个Chain对象作为参数,该对象表示当前的拦截器链。 2. 拦截器链是按照添加顺序执行的,每个拦截器都可以选择将请求传递给下一个拦截器或者直接返回响应。 3. 拦截器可以在请求和响应中添加、修改或删除头信息,也可以重试请求或者记录请求和响应的日志等。 以下是一个简单的OkHttp拦截器示例,它会在请求头中添加一个自定义的User-Agent信息: ```java public class UserAgentInterceptor implements Interceptor { private static final String USER_AGENT_HEADER = "User-Agent"; private final String userAgent; public UserAgentInterceptor(String userAgent) { this.userAgent = userAgent; } @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Request newRequest = request.newBuilder() .header(USER_AGENT_HEADER, userAgent) .build(); return chain.proceed(newRequest); } } ``` 在上面的示例中,我们创建了一个名为UserAgentInterceptor拦截器,它接收一个User-Agent字符串作为参数。在intercept方法中,我们首先获取当前的请求对象,然后使用Request.Builder添加一个自定义的User-Agent头信息,最后使用chain.proceed方法将请求传递给下一个拦截器或者返回响应。 以下是一个使用上面定义的拦截器的示例: ```java OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new UserAgentInterceptor("MyApp/1.0")) .build(); ``` 在上面的示例中,我们创建了一个OkHttpClient对象,并使用addInterceptor方法添加了一个UserAgentInterceptor拦截器。这样,在发送请求时,OkHttp会自动调用我们定义的拦截器,并在请求头中添加一个自定义的User-Agent信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值