GeezerVison is showing github.com — 优化缓存策略,版本更新至 1.2.0 · uncle-byte/OkHttpUtils@6656676
File treeExpand file treeCollapse file tree Open diff view settings src/main/java/com/lzy/okhttputils
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +4-2Lines changed: 4 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | ## 1.用法 | 23 | 23 | 对于Eclipse不能运行项目的,提供了apk供直接运行,位于项目根目录 `okhttputils_v1.x.x.apk`。 | 24 | 24 |
| | 25 | + 本项目Demo的网络请求是我自己的服务器,有时候可能不稳定,网速比较慢时请耐心等待。。 | | 26 | + | 25 | 27 | * 对于Android Studio的用户,可以选择添加: | 26 | 28 | ```java | 27 | | - compile 'com.lzy.net:okhttputils:1.1.0' //可以单独使用,不需要依赖下方的扩展包 | | 29 | + compile 'com.lzy.net:okhttputils:1.2.0' //可以单独使用,不需要依赖下方的扩展包 | 28 | 30 | compile 'com.lzy.net:okhttpserver:0.1.0' //扩展了下载管理和上传管理,根据需要添加 | 29 | 31 |
| 30 | 32 | compile 'com.lzy.net:okhttputils:+' //版本号使用 + 可以自动引用最新版 | | 37 | 39 | ``` | 38 | 40 | * 对于Eclipse的用户,可以选择添加 `/lib` 目录下的: | 39 | 41 | ```java | 40 | | - okhttputils-1.1.0.jar | | 42 | + okhttputils-1.2.0.jar | 41 | 43 | okhttpserver-0.1.0.jar | 42 | 44 | ``` | 43 | 45 |
| |
Collapse file Copy file name to clipboardExpand all lines: app/build.gradle +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,8 +8,8 @@ android { | 8 | 8 | applicationId "com.lzy.okhttpdemo" | 9 | 9 | minSdkVersion 8 | 10 | 10 | targetSdkVersion 22 | 11 | | - versionCode 10 | 12 | | - versionName "1.1.0" | | 11 | + versionCode 11 | | 12 | + versionName "1.2.0" | 13 | 13 | } | 14 | 14 | buildTypes { | 15 | 15 | release { | |
Collapse file Copy file name to clipboard Collapse file Copy file name to clipboardExpand all lines: okhttputils/bintray.gradle +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | apply plugin: 'com.github.dcendents.android-maven' | 2 | 2 | apply plugin: 'com.jfrog.bintray' | 3 | 3 |
| 4 | | -version = "1.1.0" // 数据仓库依赖第三部分 | | 4 | +version = "1.2.0" // 数据仓库依赖第三部分 | 5 | 5 |
| 6 | 6 | def siteUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils' | 7 | 7 | def gitUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils.git' | |
Collapse file Copy file name to clipboardExpand all lines: okhttputils/build.gradle +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,8 +7,8 @@ android { | 7 | 7 | defaultConfig { | 8 | 8 | minSdkVersion 8 | 9 | 9 | targetSdkVersion 22 | 10 | | - versionCode 4 | 11 | | - versionName "1.1.0" | | 10 | + versionCode 5 | | 11 | + versionName "1.2.0" | 12 | 12 | } | 13 | 13 | buildTypes { | 14 | 14 | release { | |
Collapse file Copy file name to clipboardExpand all lines: okhttputils/src/main/java/com/lzy/okhttputils/request/BaseRequest.java +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -364,7 +364,7 @@ public void onResponse(Call call, Response response) throws IOException { | 364 | 364 | T data = (T) mCallback.parseNetworkResponse(response); | 365 | 365 | sendSuccessResultCallback(false, data, call, response, mCallback); | 366 | 366 | //网络请求成功,保存缓存数据 | 367 | | - saveCache(response.headers(), data); | | 367 | + handleCache(response.headers(), data); | 368 | 368 | } | 369 | 369 | }); | 370 | 370 | } | @@ -376,11 +376,15 @@ public void onResponse(Call call, Response response) throws IOException { | 376 | 376 | * @param data 响应数据 | 377 | 377 | */ | 378 | 378 | @SuppressWarnings("unchecked") | 379 | | - private <T> void saveCache(Headers headers, T data) { | | 379 | + private <T> void handleCache(Headers headers, T data) { | 380 | 380 | // DEFAULT 默认遵循 304 规则,其他缓存模式忽略 304 缓存头 | 381 | 381 | boolean forceCache = (cacheMode != CacheMode.DEFAULT); | 382 | 382 | CacheEntity<T> cache = HeaderParser.parseCacheHeaders(headers, data, cacheKey, forceCache); | 383 | | - if (cache != null) { | | 383 | + if (cache == null) { | | 384 | + //服务器不需要缓存,移除本地缓存 | | 385 | + cacheManager.remove(cacheKey); | | 386 | + } else { | | 387 | + //缓存命中,更新缓存 | 384 | 388 | cacheManager.replace(cacheKey, (CacheEntity<Object>) cache); | 385 | 389 | } | 386 | 390 | } | |
Collapse file Copy file name to clipboardExpand all lines: okhttputils/src/main/java/com/lzy/okhttputils/utils/HeaderParser.java +8-3Lines changed: 8 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,7 +28,7 @@ public class HeaderParser { | 28 | 28 | * Cache-Control: private 响应只能作为私有缓存,不能在用户之间共享 | 29 | 29 | * Cache-Control: no-cache 提醒浏览器要从服务器提取文档进行验证 | 30 | 30 | * Cache-Control: no-store 绝对禁止缓存(用于机密,敏感文件) | 31 | | - * Cache-Control: max-age=60 60秒之后缓存过期(相对时间) | | 31 | + * Cache-Control: max-age=60 60秒之后缓存过期(相对时间),优先级比Expires高 | 32 | 32 | * Date: Mon, 19 Nov 2012 08:39:00 GMT 当前response发送的时间 | 33 | 33 | * Expires: Mon, 19 Nov 2012 08:40:01 GMT 缓存过期的时间(绝对时间) | 34 | 34 | * Last-Modified: Mon, 19 Nov 2012 08:38:01 GMT 服务器端文件的最后修改时间 | @@ -46,6 +46,9 @@ public static <T> CacheEntity<T> parseCacheHeaders(Headers responseHeaders, T da | 46 | 46 | long expires = HttpHeaders.getExpiration(responseHeaders.get(HttpHeaders.HEAD_KEY_EXPIRES)); | 47 | 47 | String cacheControl = HttpHeaders.getCacheControl(responseHeaders.get(HttpHeaders.HEAD_KEY_CACHE_CONTROL), responseHeaders.get(HttpHeaders.HEAD_KEY_PRAGMA)); | 48 | 48 |
| | 49 | + //没有缓存头控制,不需要缓存 | | 50 | + if (TextUtils.isEmpty(cacheControl) && expires <= 0 && !forceCache) return null; | | 51 | + | 49 | 52 | long maxAge = 0; | 50 | 53 | if (!TextUtils.isEmpty(cacheControl)) { | 51 | 54 | StringTokenizer tokens = new StringTokenizer(cacheControl, ","); | @@ -58,6 +61,8 @@ public static <T> CacheEntity<T> parseCacheHeaders(Headers responseHeaders, T da | 58 | 61 | try { | 59 | 62 | //获取最大缓存时间 | 60 | 63 | maxAge = Long.parseLong(token.substring(8)); | | 64 | + //服务器缓存设置立马过期,不缓存 | | 65 | + if (maxAge <= 0 && !forceCache) return null; | 61 | 66 | } catch (Exception e) { | 62 | 67 | e.printStackTrace(); | 63 | 68 | } | @@ -72,10 +77,10 @@ public static <T> CacheEntity<T> parseCacheHeaders(Headers responseHeaders, T da | 72 | 77 | } else { | 73 | 78 | now = System.currentTimeMillis(); | 74 | 79 | } | 75 | | - if (!TextUtils.isEmpty(cacheControl)) { | | 80 | + if (maxAge > 0) { | 76 | 81 | // Http1.1 优先验证 Cache-Control 头 | 77 | 82 | localExpire = now + maxAge * 1000; | 78 | | - } else if (date > 0 && expires >= date) { | | 83 | + } else if (expires >= 0) { | 79 | 84 | // Http1.0 验证 Expires 头 | 80 | 85 | localExpire = expires; | 81 | 86 | } | |
Collapse file Copy file name to clipboard Collapse file Copy file name to clipboard
|