Retrofit 源碼解讀之離線緩存策略的實現
相關代碼已上傳至 GitHub,已開源庫,請移步:
| 優雅的給 Retrofit 加上緩存 RetrofitCache
Retrofit
是square公司開發的一款網絡框架,也是至今Android網絡請求中最火的一個,配合OkHttp+RxJava+Retrofit三劍客更是如魚得水,公司項目重構時,我也在第一時間使用了RxJava+Retrofit,使用過程中遇到的一些問題,也會在后續的博客中,一點點分享出來,供大家參考!
在項目的過程中,項目需求需要在離線的情況下能夠繼續瀏覽app內容,第一時間想到緩存,于是經過各種google搜索,得出以下結論(使用Retrofit 2.0)
-參考stackoverflow地址 ,Retrofit 2.0開始,底層的網絡連接全都依賴于OkHttp,故要設置緩存,必須從OkHttp下手
-具體的使用過程為:1.先開啟OkHttp緩存
File httpCacheDirectory = new File(UIUtils.getContext().getExternalCacheDir(), "responses");
client.setCache(new Cache(httpCacheDirectory,10 * 1024 * 1024));
我們可以看到 先獲取系統外部存儲的緩存路徑,命名為response,此文件夾可以在android/data/<包名>/cache/resposes
看到里面的內容,具體OkHttp是如何做到離線緩存的呢?
我們進入Cache類,有重大發現,首先是它的注釋,極其詳細
Caches HTTP and HTTPS responses to the filesystem so they may be reused, saving time and bandwidth.
Cache Optimization
To measure cache effectiveness, this class tracks three statistics:
Request Count: the number of HTTP requests issued since this cache was created.
Network Count: the number of those requests that required network use.
Hit Count: the number of those requests whose responses were served by the cache.
Sometimes a request will result in a conditional cache hit. If the cache contains a stale copy of the response, the client will issue a conditional GET. The server will then send either the updated response if it has changed, or a short 'not modified' response if the client's copy is still valid. Such responses increment both the network count and hit count.
The best way to improve the cache hit rate is by configuring the web server to return cacheable responses. Although this client honors all HTTP/1.1 (RFC 7234) cache headers, it doesn't cache partial responses.
Force a Network Response
In some situations, such as after a user clicks a 'refresh' button, it may be necessary to skip the cache, and fetch data directly from the server. To force a full refresh, add the no-cache directive:
Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder().noCache().build())
.url("http://publicobject.com/helloworld.txt")
.build();
If it is only necessary to force a cached response to be validated by the server, use the more efficient max-age=0 directive instead:
Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder()
.maxAge(0, TimeUnit.SECONDS)
.build())
.url("http://publicobject.com/helloworld.txt")
.build();
Force a Cache Response
Sometimes you'll want to show resources if they are available immediately, but not otherwise. This can be used so your application can show something while waiting for the latest data to be downloaded. To restrict a request to locally-cached resources, add the only-if-cached directive:
Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder()
.onlyIfCached()
.build())
.url("http://publicobject.com/helloworld.txt")
.build();
Response forceCacheResponse = client.newCall(request).execute();
if (forceCacheResponse.code() != 504) {
// The resource was cached! Show it.
} else {
// The resource was not cached.
}
This technique works even better in situations where a stale response is better than no response. To permit stale cached responses, use the max-stale directive with the maximum staleness in seconds:
Request request = new Request.Builder()
.cacheControl(new CacheControl.Builder()
.maxStale(365, TimeUnit.DAYS)
.build())
.url("http://publicobject.com/helloworld.txt")
.build();
The CacheControl class can configure request caching directives and parse response caching directives. It even offers convenient constants CacheControl.FORCE_NETWORK and CacheControl.FORCE_CACHE that address the use cases above.
文檔詳細說明了此類的作用,支持OkHttp直接使用緩存,然后羅列出了各種具體的用法,可惜的是我們這里使用的是Retrofit,無法直接用OkHttp;但是如果有直接用OkHttp的童鞋們,可以根據上面的提示,完成具體的緩存操作,so easy !。
回到Retrofit,通過閱讀上面的文檔,我們知道還有一個類,CacheControl
類,主要負責緩存策略的管理,其中,支持一下策略策略如下:
1. noCache 不使用緩存,全部走網絡
2. noStore 不使用緩存,也不存儲緩存
3. onlyIfCached 只使用緩存
4. maxAge 設置最大失效時間,失效則不使用 需要服務器配合
5. maxStale 設置最大失效時間,失效則不使用 需要服務器配合 感覺這兩個類似 還沒怎么弄清楚,清楚的同學歡迎留言
6. minFresh 設置有效時間,依舊如上
7. FORCE_NETWORK 只走網絡
8. FORCE_CACHE 只走緩存
通過上面的CacheControl
類,我們很快就能指定詳細的策略
首先,判斷網絡,有網絡,則從網絡獲取,并保存到緩存中,無網絡,則從緩存中獲取
所以,最終的代碼如下
-首先,給OkHttp設置攔截器
client.interceptors().add(interceptor);
-然后,在攔截器內做Request攔截操作
Request request = chain.request();//攔截reqeust
if (!AppUtil.isNetworkReachable(UIUtils.getContext())) {//判斷網絡連接狀況
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)//無網絡時只從緩存中讀取
.build();
UIUtils.showToastSafe("暫無網絡");
}
其中,AppUtil.isNetworkReachable(UIUtils.getContext())
是判斷網絡是否連接的方法,具體邏輯如下
/**
* 判斷網絡是否可用
*
* @param context Context對象
*/
public static Boolean isNetworkReachable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo current = cm.getActiveNetworkInfo();
if (current == null) {
return false;
}
return (current.isAvailable());
}
在每個請求發出前,判斷一下網絡狀況,如果沒問題繼續訪問,如果有問題,則設置從本地緩存中讀取
-接下來是設置Response
Response response = chain.proceed(request);
if (AppUtil.isNetworkReachable(UIUtils.getContext())) {
int maxAge = 60*60; // 有網絡時 設置緩存超時時間1個小時
response.newBuilder()
.removeHeader("Pragma")
//清除頭信息,因為服務器如果不支持,會返回一些干擾信息,不清除下面無法生效
.header("Cache-Control", "public, max-age=" + maxAge)//設置緩存超時時間
.build();
} else {
int maxStale = 60 * 60 * 24 * 28; // 無網絡時,設置超時為4周
response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
//設置緩存策略,及超時策略
.build();
}
先判斷網絡,網絡好的時候,移除header后添加cache失效時間為1小時,網絡未連接的情況下設置緩存時間為4周
-最后,攔截器全部代碼
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!AppUtil.isNetworkReachable(UIUtils.getContext())) {
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.url(path).build();
UIUtils.showToastSafe("暫無網絡");//子線程安全顯示Toast
}
Response response = chain.proceed(request);
if (AppUtil.isNetworkReachable(UIUtils.getContext())) {
int maxAge = 60 * 60; // read from cache for 1 minute
response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
} else {
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
return response;
}
};
快過年了,祝所有的童鞋們,身體健康,事事如意!!,咳咳,還有最重要的,程序無Bug!!!