欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

拦截器CacheInterceptor

时间:2023-07-09
CacheInterceptor拦截器

在说第三个拦截器缓存拦截器之前,我们先了解下http协议的缓存机制是怎么样的

缓存规则分为强制缓存和对比缓存两种。

http把对于缓存的控制规则都放在头部的几个字段里了。

Cache-Control

Cache-Control常见的取值有private、public、no-cache、max-age,no-store,默认为private

private: 客户端可以缓存
public: 客户端和代理服务器都可缓存
max-age: 内容将在 xxx 秒后失效,单位秒
no-cache: 使用对比缓存来验证缓存数据,会发送请求和服务器对比,看是否使用本地缓存的
no-store: 所有内容都不会缓存,强制缓存,对比缓存都不会触发

强制缓存

假如配置了max-age的值为3600,那么当前请求就会默认缓存1个小时,在一个小时内,去请求这条数据,都会使用本地的缓存数据作为响应。

假如no-store,则每次请求都会去服务器请求最新的数据

对比缓存

如果使用对比缓存,那么服务器会去对比客户端发送过来的数据和服务器上的资源是不是一样的,如果一样,则服务器只会响应一个302状态,并不会把客户端请求的资源响应回去,当客户端接收到响应,解析为302,则会去缓存中获取数据。否则解析获取最新响应数据。

对比缓存又有2组规则:ETag/If-None-Match、Last-Modified/If-Modified-Since

ETag/If-None-Match

当第一次请求资源时,服务器会在响应头里加上ETag标识,如W/“4342c344-8d3”,该标识的生成规则由服务器决定,一般是 文件的hash值。

当再次请求资源时,客户端的头会附带If-None-Match,值为W/“4342c344-8d3”,服务器接收到请求后,如果和服务器上的资源运算出的ETag相同,那它就会返回302,否则就会返回200并附带资源文件到响应里。

Last-Modified/If-Modified-Since

当第一次请求资源时,服务器响应头里会有资源的最后修改时间Last-Modified:Fri, 21 Jan 2022 02:54:57 GMT。

下一次请求,客户端就会在请求头上增加头If-Modified-Since:Fri, 21 Jan 2022 02:54:57 GMT。服务器收到请求后,如果这个时间比服务器该资源最后的修改时间早的话,说明被改动过,返回最新资源,否则服务器返回302,客户端则取缓存资源。

注意上面的两组规则,ETag/If-None-Match 优先级大于 Last-Modified/If-Modified-Since

知道以上缓存规则后,我们再来解读CacheInterceptor这个拦截器的代码:

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()); } //当CacheStrategy里的onlyIfCached这个值配置为false时,networkRequest会为空,这个值表示不使用网络 //不使用网络,并且缓存响应也为空时,就报错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 { //向下调用拦截器,请求网络 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 { //服务器返回非304,则缓存不可用,关闭缓存 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)) { 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;}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。