OKHttp開源框架學習七 快取策略原始碼分析

2021-09-24 18:40:34 字數 3262 閱讀 1051

目錄

系列文章:

**體現:

cache類的put方法:

cache類的get方法:

okhttp開源框架學習一:同步請求總結

okhttp開源框架學習二:非同步請求總結

okhttp開源框架學習三:任務排程核心類dispatcher

okhttp開源框架學習四:***

okhttp開源框架學習五:***之retryandfollowupinterceptor

okhttp開源框架學習六:***之bridgeinterceptor

okhttp開源框架學習七:快取策略原始碼分析

okhttp開源框架學習八:***之cacheinterceptor

okhttp開源框架學習九:***之connectinterceptor

okhttp開源框架學習十:connectionpool連線池

okhttp開源框架學習十一:***之callserverinterceptor

okhttp總結

.build();接下來我們來看cache類:

cache(file directory, long maxsize, filesystem filesystem)
可以看到cache類的快取主要是通過disklrucache類來實現的。

}其中:

if (!requestmethod.equals("get"))
從這段**可以看出,只有「get」請求才會快取,而「post」請求是不會快取資料的。如果需要快取「post」請求的資料,可檢視關於okhttp快取post請求的問題

而快取實現**是這些:

entry entry = new entry(response);

disklrucache.editor editor = null;

try

//就是表示把快取寫到磁碟上。

entry.writeto(editor);

return new cacherequestimpl(editor);

} catch (ioexception e)

entry類是cache的內部類,專門用來儲存response包含的一系列屬性。

private final string url;

private final headers varyheaders;

private final string requestmethod;

private final protocol protocol;

private final int code;

private final string message;

private final headers responseheaders;

private final @nullable handshake handshake;

private final long sentrequestmillis;

private final long receivedresponsemillis;

writeto方法:

//獲取到response

response response = entry.response(snapshot);

//判斷request與response是否匹配,如果不匹配,就結束

if (!entry.matches(request, response))

return response;

}

public response response(disklrucache.snapshot snapshot)

okhttp框架學習

整合和 預設是get請求 根據天氣id請求城市天氣資訊。public void requestweather final string weatherid else swiperefresh.setrefreshing false override public void onfailure cal...

OKHttp開源框架學習二 非同步請求總結

目錄 系列文章 第四步,call.enqueue okhttp開源框架學習一 同步請求總結 okhttp開源框架學習二 非同步請求總結 okhttp開源框架學習三 任務排程核心類dispatcher okhttp開源框架學習四 okhttp開源框架學習五 之retryandfollowupinter...

如果學習開源框架

當我們在學習乙個開源框架時,成熟的開源框架一般都會非常龐大,各個模組 包之間會有依賴關係,而如果只是修改開源包的話,始終覺得不能真正地從根本上理解 實現乙個開源框架。如果自己去模仿著寫乙個的話,也會非常痛苦,因為想實現模組a,但是寫著寫著,發現模組a依賴了太多其他模組的東西,導致無法繼續模仿下去。有...