okhttp内置缓存策略,在CacheInterceptor中实现了缓存机制。okhttp3未做任何设置情况下,默认不使用缓存。okhttp缓存策略遵循了http协议缓存,因此了解okhttp缓存策略前需要有http缓存相关基础,可以参考《浏览器 HTTP 协议缓存机制详解》。
配置缓存
全局设置
如果需要使用okhttp的缓存机制,需要在构建OkHttpClient时给它设置Cache对象,例如:
// 参数1为缓存文件目录,参数2为缓存大小
Cache cache = new Cache(directory, maxSize);
new OkHttpClient.Builder().cache(cache).build();
看看Cache的构造函数,会发现它内部是封装了DiskLruCache来做缓存文件存取。
Cache(File directory, long maxSize, FileSystem fileSystem) {
this.cache = DiskLruCache.create(fileSystem, directory, VERSION, ENTRY_COUNT, maxSize);
}
单个请求配置缓存策略
若要为单个请求设置不同的缓存策略,可以给request设置CacheControl对象,它将会为请求加上Cache-Control请求头。
new Request.Builder().cacheControl(cacheControl).build();
public Builder cacheControl(CacheControl cacheControl) {
String value = cacheControl.toString();
if (value.isEmpty()) return removeHeader("Cache-Control");
return header("Cache-Control", value);
}
CacheControl对象可以通过Builder自行配置构建:
也可以使用CacheControl提供的两个配置:
/**
* Cache control request directives that require network validation of responses. Note that such
* requests may be assisted by the cache via conditional GET requests.
*/
public static final CacheControl FORCE_NETWORK = new Builder().noCache().build();
/**
* Cache control request directives that uses the cache only, even if the cached response is
* stale. If the response isn't available in the cache or requires server validation, the call
* will fail with a {@code 504 Unsatisfiable Request}.
*/
public static final CacheControl FORCE_CACHE = new Builder()
.onlyIfCached()
.maxStale(Integer.MAX_VALUE, TimeUnit.SECONDS)
.build();
- FORCE_NETWORK:强制使用网络请求,不使用缓存
- FORCE_CACHE:强制缓存,不进行网络请求
缓存拦截器机制
okhttp中的缓存策略有强制缓存和对比缓存:
- 强制缓存,即缓存在有效期内就直接返回缓存,不进行网络请求。
- 对比缓存,即缓存超过有效期,进行网络请求。若数据未修改,服务端返回不带body的304响应,表示客户端缓存仍有效可用;否则返回完整最新数据,客户端取网络请求的最新数据。
okhttp利过CacheStrategy对象中的networkRequest和cacheResponse来判断执行什么策略:
/** The request to send on the network, or null if this call doesn't use the network. */
public final @Nullable Request networkRequest;
/** The cached response to return or validate; or null if this call doesn't use a cache. */
public final @Nullable Response cacheResponse;
通过注释可以知道networkRequest是否为空决定是否请求网络,cacheResponse是否为空决定是否使用缓存
有4种组合情况(这里先了解对应结论,后面再分析生成规则):
- networkRequest、cacheResponse均为空,构建返回一个状态码为504的response;
- networkRequest为空、cacheResponse不为空,执行强制缓存
- networkRequest、cacheResponse均不为空,执行对比缓存
- networkRequest不为空、cacheResponse为空,网络请求获取到最新response后,视情况缓存response
缓存机制实现是在CacheInterceptor的intercept方法中,这里会根据前面配置的参数来执行相应策略:
@Override public Response intercept(Chain chain) throws IOException {
// cache即构建OkHttpClient时传入的Cache对象的内部成员,用request的url作为key,查找缓存的response作为候选缓存
Response cacheCandidate = cache != null
? cache.get(chain.request())
: null;
long now = System.currentTimeMillis();
//todo === 1.生成缓存策略 ===
CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
// 获取缓存策略生成的networkRequest和cacheResponse(合法缓存)
// 下面流程会根据生成的networkRequest和cacheResponse来决定执行什么缓存策略
Request networkRequest = strategy.networkRequest;
Response cacheResponse = strategy.cacheResponse;
if (cache != null) {
// cache内部的计数器加1
cache.trackResponse(strategy);
}
// 若候选缓存存在但是缓存策略生成的cache不存在,关闭cacheCandidate中的BufferedSource的输入输出流
if (cacheCandidate != null && cacheResponse == null) {
closeQuietly(cacheCandidate.body()); // The cache candidate wasn't applicable. Close it.
}
// If we're forbidden from using the network and the cache is insufficient, fail.
// 即无网络请求又无合法缓存,返回状态码504的response
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();
}
// networkResponse表示当前网络请求的最新response,cacheResponse表示由缓存策略获取的合法缓存response
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内计数器加1
cache.trackConditionalCacheHit();
// 更新本地缓存信息
cache.update(cacheResponse, response);
return response;
} else {
closeQuietly(cacheResponse.body());
}
}
Response response = networkResponse.newBuilder()
.cacheResponse(stripBody(cacheResponse))
.networkResponse(stripBody(networkResponse))
.build();
//todo === 2.判断能否缓存networkResponse ===
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.生成缓存策略
接下来分析CacheStrategy的生成规则
CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
创建CacheStrategy.Factory
public Factory(long nowMillis, Request request, Response cacheResponse) {
// 保存当前时间、request、候选缓存response
this.nowMillis = nowMillis;
this.request = request;
this.cacheResponse = cacheResponse;
if (cacheResponse != null) {
// 若存在候选缓存,获取该response请求时发起和接收的时间(在CallServerInterceptor中记录的这些时间)
this.sentRequestMillis = cacheResponse.sentRequestAtMillis();
this.receivedResponseMillis = cacheResponse.receivedResponseAtMillis();
Headers headers = cacheResponse.headers();
// 遍历header,保存Date、Expires、Last-Modified、ETag、Age等缓存机制相关字段的值
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);
}
}
}
}
调用Factory.get()方法获取CacheStrategy
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;
}
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.
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.
// isCacheable方法通过response的状态码以及response、request的缓存控制字段来判断是否可缓存
if (!isCacheable(cacheResponse, request)) {
return new CacheStrategy(request, null);
}
// 判断request中的缓存控制字段和header是否设置If-Modified-Since或If-None-Match
CacheControl requestCaching = request.cacheControl();
if (requestCaching.noCache() || hasConditions(request)) {
return new CacheStrategy(request, null);
}
// 判断response的header中的Cache-Control是否有immutable标志,若有,则认为该响应数据不需更新
CacheControl responseCaching = cacheResponse.cacheControl();
if (responseCaching.immutable()) {
return new CacheStrategy(null, cacheResponse);
}
long ageMillis = cacheResponseAge(); //该response已缓存的时长
long freshMillis = computeFreshnessLifetime(); //可缓存时长
if (requestCaching.maxAgeSeconds() != -1) {
freshMillis = Math.min(freshMillis, SECONDS.toMillis(requestCaching.maxAgeSeconds()));
}
long minFreshMillis = 0;
if (requestCaching.minFreshSeconds() != -1) {
minFreshMillis = SECONDS.toMillis(requestCaching.minFreshSeconds());
}
long maxStaleMillis = 0;
if (!responseCaching.mustRevalidate() && requestCaching.maxStaleSeconds() != -1) {
maxStaleMillis = SECONDS.toMillis(requestCaching.maxStaleSeconds());
}
// 判断缓存是否在有效期内
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和Last-Modified
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.
}
// 添加请求头,Etag对应If-None-Match、Last-Modified对应If-Modified-Since
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);
}
getCandidate方法返回CacheStrategy后,会再经过一次判断,返回最终的CacheStrategy
if (candidate.networkRequest != null && request.cacheControl().onlyIfCached()) {
// We're forbidden from using the network and the cache is insufficient.
return new CacheStrategy(null, null);
}
2.判断能否缓存networkResponse
若网络请求完成不执行对比缓存策略,就会调用以下方法
Response response = networkResponse.newBuilder()
.cacheResponse(stripBody(cacheResponse))
.networkResponse(stripBody(networkResponse))
.build();
if (cache != null) {
// OkHttpClient设置了Cache
if (HttpHeaders.hasBody(response) && CacheStrategy.isCacheable(response, networkRequest)) {
// Offer this request to the cache.
// 保存拥有body且符合缓存条件的响应
// put方法中只缓存GET请求的response
CacheRequest cacheRequest = cache.put(response);
// 通过该方法返回新建的repsonse,保存缓存response的body能够正确的写入和关闭流
return cacheWritingResponse(cacheRequest, response);
}
// 判断是否时无效缓存,若请求方式是POST、PATCH、PUT、DELETE、MOVE,则移除缓存
if (HttpMethod.invalidatesCache(networkRequest.method())) {
try {
cache.remove(networkRequest);
} catch (IOException ignored) {
// The cache cannot be written.
}
}
}
// 返回响应给上层拦截器
return response;
总结
okhttp中缓存机制实现大致流程是先判断执行强制缓存策略,不执行则请求服务端获取数据,然后判断执行对比缓存策略,接着更新缓存或新增缓存,最后返回response到上层拦截器。