在最近的工作中接觸到了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.
}
}
}
參考文章
- http://www.lxweimin.com/p/00d281c226f6 okhttp緩存處理
- https://blog.csdn.net/aiynmimi/article/details/79807036 OkHttp3源碼分析之緩存Cache