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

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!!!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,182評論 6 543
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,489評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,290評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,776評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,510評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,866評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,860評論 3 447
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,036評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,585評論 1 336
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,331評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,536評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,058評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,754評論 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,154評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,469評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,273評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,505評論 2 379

推薦閱讀更多精彩內容