title: OkHttp效能最佳化 author: 書蟲 tags:
- 工作記錄 categories:
- 工作 date: 2021-01-08 11:41:00
支援Brotli壓縮
Important note: Brotli availability is restricted to HTTPS connections.
Advantages:
- Brotli outperforms gzip for typical web assets (e.g. css, html, js) by 17–25 %.
- Brotli -11 density compared to gzip -9:
- html (multi-language corpus): 25 % savings
- js (alexa top 10k): 17 % savings
- minified js (alexa top 10k): 17 % savings
- css (alexa top 10k): 20 % savings
支援header快取
OkHttp快取的工作原理如下圖
- 如何啟用快取功能?
int cacheSize = 10 * 1024 * 1024; // 10MB
OkHttpClient.Builder builder = new OkHttpClient.Builder().
cache(new Cache(context.getCacheDir(),cacheSize)
....如何使它起作用?
- Cache-Control
- ETag
- Last-Modified
- Date
- 其他控制快取的屬性
現在用的是Cache-Control。
如果要關閉某個介面的快取的話,考慮使用以下Cache-Control配置:
Cache-Control: no-cache 每次使用前都應使用伺服器重新驗證的資源,客戶端會儲存一份資料。
Cache-Control: no-store 該響應不允許被快取,必須在每個請求中全部獲取。用於永不快取的資源。
Cache-Control: max-age=31536000 快取有效時間,在有效期之內的使用快取。
ETag或Last-Modified標頭可以幫助您更有效地重新驗證過期的快取資源。
參考連結
