Okhttp的缓存机制,【绝对干货】

private final boolean mustRevalidate;
private final int maxStaleSeconds;
private final int minFreshSeconds;
private final boolean onlyIfCached;
private final boolean noTransform;

//解析头文件中的相关字段,得到该缓存控制类
public static CacheControl parse(Headers headers) {

}

}

CacheStrategy

主要用于判断是否使用缓存数据

public final class CacheStrategy {

public Factory(long nowMillis, Request request, Response cacheResponse) {
this.nowMillis = nowMillis;
//网络请求和缓存响应
this.request = request;
this.cacheResponse = cacheResponse;

if (cacheResponse != null) {
//找到缓存响应的响应头信息
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 = HeaderParser.parseSeconds(value, -1);
} else if (OkHeaders.SENT_MILLIS.equalsIgnoreCase(fieldName)) {
sentRequestMillis = Long.parseLong(value);
} else if (OkHeaders.RECEIVED_MILLIS.equalsIgnoreCase(fieldName)) {
receivedResponseMillis = Long.parseLong(value);
}
}
}
}

public CacheStrategy get() {
//获取判定的缓存策略
CacheStrategy candidate = getCandidate();

if (candidate.networkRequest != null && request.cacheControl().onlyIfCached()) {
// 如果判定的缓存策略的网络请求不为空,但是只使用缓存,则返回两者都为空的缓存策略。
return new CacheStrategy(null, null);
}

return candidate;
}

/** Returns a strategy to use assuming the request can use the network. */
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.
//如果请求是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.
//如果请求对应的响应不能被缓存,则返回没有缓存响应的策略
if (!isCacheable(cacheResponse, request)) {
return new CacheStrategy(request, null);
}

//获取请求头中的CacheControl信息
CacheControl requestCaching = request.cacheControl();
//如果请求头中的CacheControl信息是不缓存的,则返回没有缓存响应的策略
if (requestCaching.noCache() || hasConditions(request)) {
return new CacheStrategy(request, null);
}

//获取响应的年龄
long ageMillis = cacheResponseAge();
//计算上次响应刷新的时间
long freshMillis = computeFreshnessLifetime();
//如果请求里有最大持续时间要求,则取较小的值作为上次响应的刷新时间
if (requestCaching.maxAgeSeconds() != -1) {
freshMillis = Math.min(freshMillis, SECONDS.toMillis

  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值