【轉】Retrofit 源碼解讀之離線緩存策略的實現

Retrofit 源碼解讀之離線緩存策略的實現

Retrofit 是square公司開發的一款網絡框架,也是至今Android網絡請求中最火的一個,配合OkHttp+RxJava+Retrofit三劍客更是如魚得水,公司項目重構時,我也在第一時間使用了RxJava+Retrofit,使用過程當中遇到的一些問題,也會在後續的博客中,一點點分享出來,供你們參考!android

在項目的過程當中,項目需求須要在離線的狀況下可以繼續瀏覽app內容,第一時間想到緩存,因而通過各類google搜索,得出如下結論(使用Retrofit 2.0)web

-參考stackoverflow地址 ,Retrofit 2.0開始,底層的網絡鏈接全都依賴於OkHttp,故要設置緩存,必須從OkHttp下手sql

-具體的使用過程爲: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 !。markdown

回到Retrofit,經過閱讀上面的文檔,咱們知道還有一個類,CacheControl類,主要負責緩存策略的管理,其中,支持一下策略策略以下:網絡

1. noCache 不使用緩存,所有走網絡 2. noStore 不使用緩存,也不存儲緩存 3. onlyIfCached 只使用緩存 4. maxAge 設置最大失效時間,失效則不使用 須要服務器配合 5. maxStale 設置最大失效時間,失效則不使用 須要服務器配合 感受這兩個相似 還沒怎麼弄清楚,清楚的同窗歡迎留言 6. minFresh 設置有效時間,依舊如上 7. FORCE_NETWORK 只走網絡 8. FORCE_CACHE 只走緩存

經過上面的CacheControl類,咱們很快就能指定詳細的策略app

首先,判斷網絡,有網絡,則從網絡獲取,並保存到緩存中,無網絡,則從緩存中獲取框架

因此,最終的代碼以下

-首先,給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; } };

原文連接:http://www.jianshu.com/p/3a8d910cce38

相關文章
相關標籤/搜索