Skip to content

Commit d6faf05

Browse files
committed
新增请求缓存功能及文档
新增删除全局参数和请求头的方法 优化框架代码相关的逻辑嵌套 修正打印格式化 Json 代码逻辑
1 parent f90c894 commit d6faf05

37 files changed

+2492
-1906
lines changed

EasyHttp.apk

802 KB
Binary file not shown.

HelpDoc.md

+125
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
* [同步请求](#同步请求)
2626

27+
* [请求缓存](#请求缓存)
28+
2729
* [疑难解答](#疑难解答)
2830

2931
* [如何添加全局参数](#如何添加全局参数)
@@ -238,6 +240,8 @@ public final class LoginApi implements IRequestApi {
238240

239241
* implements IRequestType:实现这个接口之后可以重新指定这个请求的提交方式
240242

243+
* implements IRequestCache:实现这个接口之后可以重新指定这个请求的缓存模式
244+
241245
* implements IRequestClient:实现这个接口之后可以重新指定这个请求所用的 OkHttpClient 对象
242246

243247
* 字段作为请求参数的衡量标准
@@ -419,6 +423,127 @@ try {
419423
}
420424
```
421425

426+
#### 请求缓存
427+
428+
* 需要先实现读取和写入缓存的接口,如果已配置则可以跳过,这里以 MMKV 为例
429+
430+
```java
431+
public final class RequestHandler implements IRequestHandler {
432+
433+
private final Application mApplication;
434+
private final MMKV mMmkv;
435+
436+
public RequestHandler(Application application) {
437+
mApplication = application;
438+
mMmkv = MMKV.mmkvWithID("http_cache_id");
439+
}
440+
441+
..................
442+
443+
@Override
444+
public Object readCache(LifecycleOwner lifecycle, IRequestApi api, Type type) {
445+
String cacheKey = GsonFactory.getSingletonGson().toJson(api);
446+
String cacheValue = mMmkv.getString(cacheKey, null);
447+
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
448+
return null;
449+
}
450+
EasyLog.print("---------- cacheKey ----------");
451+
EasyLog.json(cacheKey);
452+
EasyLog.print("---------- cacheValue ----------");
453+
EasyLog.json(cacheValue);
454+
return GsonFactory.getSingletonGson().fromJson(cacheValue, type);
455+
}
456+
457+
@Override
458+
public boolean writeCache(LifecycleOwner lifecycle, IRequestApi api, Response response, Object result) {
459+
String cacheKey = GsonFactory.getSingletonGson().toJson(api);
460+
String cacheValue = GsonFactory.getSingletonGson().toJson(result);
461+
if (cacheValue == null || "".equals(cacheValue) || "{}".equals(cacheValue)) {
462+
return false;
463+
}
464+
EasyLog.print("---------- cacheKey ----------");
465+
EasyLog.json(cacheKey);
466+
EasyLog.print("---------- cacheValue ----------");
467+
EasyLog.json(cacheValue);
468+
return mMmkv.putString(cacheKey, cacheValue).commit();
469+
}
470+
}
471+
```
472+
473+
* 首先请求缓存模式有四种方式,都在 `CacheMode` 这个枚举类中
474+
475+
```java
476+
public enum CacheMode {
477+
478+
/**
479+
* 默认(按照 Http 协议来缓存)
480+
*/
481+
DEFAULT,
482+
483+
/**
484+
* 不使用缓存(禁用 Http 协议缓存)
485+
*/
486+
NO_CACHE,
487+
488+
/**
489+
* 只使用缓存
490+
*
491+
* 有缓存的情况下:读取缓存 -> 回调成功
492+
* 无缓存的情况下:请求网络 -> 写入缓存 -> 回调成功
493+
*/
494+
USE_CACHE_ONLY,
495+
496+
/**
497+
* 优先使用缓存
498+
*
499+
* 有缓存的情况下:先读缓存 —> 回调成功 —> 请求网络 —> 刷新缓存
500+
* 无缓存的情况下:请求网络 -> 写入缓存 -> 回调成功
501+
*/
502+
USE_CACHE_FIRST,
503+
504+
/**
505+
* 只在网络请求失败才去读缓存
506+
*/
507+
USE_CACHE_AFTER_FAILURE
508+
}
509+
```
510+
511+
* 为某个接口设置缓存模式
512+
513+
```java
514+
public final class XxxApi implements IRequestApi, IRequestCache {
515+
516+
@Override
517+
public String getApi() {
518+
return "xxx/";
519+
}
520+
521+
@Override
522+
public CacheMode getMode() {
523+
// 设置优先使用缓存
524+
return CacheMode.USE_CACHE_FIRST;
525+
}
526+
}
527+
```
528+
529+
* 全局设置缓存模式
530+
531+
```java
532+
public class XxxServer implements IRequestServer {
533+
534+
@Override
535+
public String getHost() {
536+
return "https://www.xxxxxxx.com/";
537+
}
538+
539+
@Override
540+
public CacheMode getMode() {
541+
// 只在请求失败才去读缓存
542+
return CacheMode.USE_CACHE_AFTER_FAILURE;
543+
}
544+
}
545+
```
546+
422547
# 疑难解答
423548

424549
#### 如何添加全局参数

LICENSE

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
Apache License
3-
Version 2.0, January 2004
3+
Version 2.0, November 2019
44
http://www.apache.org/licenses/
55

66
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
@@ -187,7 +187,7 @@
187187
same "printed page" as the copyright notice for easier
188188
identification within third-party archives.
189189

190-
Copyright [yyyy] [name of copyright owner]
190+
Copyright 2019 Huang JinQun
191191

192192
Licensed under the Apache License, Version 2.0 (the "License");
193193
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)