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标头可以帮助您更有效地重新验证过期的缓存资源。
参考链接
