GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 优化缓存策略,版本更新至 1.2.0 · uncle-byte/OkHttpUtils@6656676

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • README.md
  • app
    • build.gradle
  • lib
    • okhttputils-1.2.0.jar
  • okhttputils_v1.1.0.apk
  • okhttputils_v1.2.0.apk
  • okhttputils
    • bintray.gradle
    • build.gradle
    • src/main/java/com/lzy/okhttputils
      • request
        • BaseRequest.java
      • utils
        • HeaderParser.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎README.md‎

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 numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
## 1.用法
2323
对于Eclipse不能运行项目的,提供了apk供直接运行,位于项目根目录 `okhttputils_v1.x.x.apk`。
2424

25+
本项目Demo的网络请求是我自己的服务器,有时候可能不稳定,网速比较慢时请耐心等待。。
26+
2527
* 对于Android Studio的用户,可以选择添加:
2628
```java
27-
compile 'com.lzy.net:okhttputils:1.1.0' //可以单独使用,不需要依赖下方的扩展包
29+
compile 'com.lzy.net:okhttputils:1.2.0' //可以单独使用,不需要依赖下方的扩展包
2830
compile 'com.lzy.net:okhttpserver:0.1.0' //扩展了下载管理和上传管理,根据需要添加
2931

3032
compile 'com.lzy.net:okhttputils:+' //版本号使用 + 可以自动引用最新版
@@ -37,7 +39,7 @@
3739
```
3840
* 对于Eclipse的用户,可以选择添加 `/lib` 目录下的:
3941
```java
40-
okhttputils-1.1.0.jar
42+
okhttputils-1.2.0.jar
4143
okhttpserver-0.1.0.jar
4244
```
4345

Collapse file

‎app/build.gradle‎

Copy file name to clipboardExpand all lines: app/build.gradle
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "com.lzy.okhttpdemo"
99
minSdkVersion 8
1010
targetSdkVersion 22
11-
versionCode 10
12-
versionName "1.1.0"
11+
versionCode 11
12+
versionName "1.2.0"
1313
}
1414
buildTypes {
1515
release {
Collapse file

‎lib/okhttputils-1.1.0.jar‎ ‎lib/okhttputils-1.2.0.jar‎lib/okhttputils-1.1.0.jar renamed to lib/okhttputils-1.2.0.jar

Copy file name to clipboard
60.8 KB
Binary file not shown.
Collapse file

‎okhttputils/bintray.gradle‎

Copy file name to clipboardExpand all lines: okhttputils/bintray.gradle
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apply plugin: 'com.github.dcendents.android-maven'
22
apply plugin: 'com.jfrog.bintray'
33

4-
version = "1.1.0" // 数据仓库依赖第三部分
4+
version = "1.2.0" // 数据仓库依赖第三部分
55

66
def siteUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils'
77
def gitUrl = 'https://github.com/jeasonlzy0216/OkHttpUtils.git'
Collapse file

‎okhttputils/build.gradle‎

Copy file name to clipboardExpand all lines: okhttputils/build.gradle
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
defaultConfig {
88
minSdkVersion 8
99
targetSdkVersion 22
10-
versionCode 4
11-
versionName "1.1.0"
10+
versionCode 5
11+
versionName "1.2.0"
1212
}
1313
buildTypes {
1414
release {
Collapse file

‎okhttputils/src/main/java/com/lzy/okhttputils/request/BaseRequest.java‎

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 numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ public void onResponse(Call call, Response response) throws IOException {
364364
T data = (T) mCallback.parseNetworkResponse(response);
365365
sendSuccessResultCallback(false, data, call, response, mCallback);
366366
//网络请求成功,保存缓存数据
367-
saveCache(response.headers(), data);
367+
handleCache(response.headers(), data);
368368
}
369369
});
370370
}
@@ -376,11 +376,15 @@ public void onResponse(Call call, Response response) throws IOException {
376376
* @param data 响应数据
377377
*/
378378
@SuppressWarnings("unchecked")
379-
private <T> void saveCache(Headers headers, T data) {
379+
private <T> void handleCache(Headers headers, T data) {
380380
// DEFAULT 默认遵循 304 规则,其他缓存模式忽略 304 缓存头
381381
boolean forceCache = (cacheMode != CacheMode.DEFAULT);
382382
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+
//缓存命中,更新缓存
384388
cacheManager.replace(cacheKey, (CacheEntity<Object>) cache);
385389
}
386390
}
Collapse file

‎okhttputils/src/main/java/com/lzy/okhttputils/utils/HeaderParser.java‎

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 numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class HeaderParser {
2828
* Cache-Control: private 响应只能作为私有缓存,不能在用户之间共享
2929
* Cache-Control: no-cache 提醒浏览器要从服务器提取文档进行验证
3030
* Cache-Control: no-store 绝对禁止缓存(用于机密,敏感文件)
31-
* Cache-Control: max-age=60 60秒之后缓存过期(相对时间)
31+
* Cache-Control: max-age=60 60秒之后缓存过期(相对时间),优先级比Expires高
3232
* Date: Mon, 19 Nov 2012 08:39:00 GMT 当前response发送的时间
3333
* Expires: Mon, 19 Nov 2012 08:40:01 GMT 缓存过期的时间(绝对时间)
3434
* Last-Modified: Mon, 19 Nov 2012 08:38:01 GMT 服务器端文件的最后修改时间
@@ -46,6 +46,9 @@ public static <T> CacheEntity<T> parseCacheHeaders(Headers responseHeaders, T da
4646
long expires = HttpHeaders.getExpiration(responseHeaders.get(HttpHeaders.HEAD_KEY_EXPIRES));
4747
String cacheControl = HttpHeaders.getCacheControl(responseHeaders.get(HttpHeaders.HEAD_KEY_CACHE_CONTROL), responseHeaders.get(HttpHeaders.HEAD_KEY_PRAGMA));
4848

49+
//没有缓存头控制,不需要缓存
50+
if (TextUtils.isEmpty(cacheControl) && expires <= 0 && !forceCache) return null;
51+
4952
long maxAge = 0;
5053
if (!TextUtils.isEmpty(cacheControl)) {
5154
StringTokenizer tokens = new StringTokenizer(cacheControl, ",");
@@ -58,6 +61,8 @@ public static <T> CacheEntity<T> parseCacheHeaders(Headers responseHeaders, T da
5861
try {
5962
//获取最大缓存时间
6063
maxAge = Long.parseLong(token.substring(8));
64+
//服务器缓存设置立马过期,不缓存
65+
if (maxAge <= 0 && !forceCache) return null;
6166
} catch (Exception e) {
6267
e.printStackTrace();
6368
}
@@ -72,10 +77,10 @@ public static <T> CacheEntity<T> parseCacheHeaders(Headers responseHeaders, T da
7277
} else {
7378
now = System.currentTimeMillis();
7479
}
75-
if (!TextUtils.isEmpty(cacheControl)) {
80+
if (maxAge > 0) {
7681
// Http1.1 优先验证 Cache-Control 头
7782
localExpire = now + maxAge * 1000;
78-
} else if (date > 0 && expires >= date) {
83+
} else if (expires >= 0) {
7984
// Http1.0 验证 Expires 头
8085
localExpire = expires;
8186
}
Collapse file

‎okhttputils_v1.1.0.apk‎

Copy file name to clipboard
-2.09 MB
Binary file not shown.
Collapse file

‎okhttputils_v1.2.0.apk‎

Copy file name to clipboard
2.09 MB
Binary file not shown.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.