|
24 | 24 |
|
25 | 25 | * [同步请求](#同步请求)
|
26 | 26 |
|
| 27 | + * [请求缓存](#请求缓存) |
| 28 | + |
27 | 29 | * [疑难解答](#疑难解答)
|
28 | 30 |
|
29 | 31 | * [如何添加全局参数](#如何添加全局参数)
|
@@ -238,6 +240,8 @@ public final class LoginApi implements IRequestApi {
|
238 | 240 |
|
239 | 241 | * implements IRequestType:实现这个接口之后可以重新指定这个请求的提交方式
|
240 | 242 |
|
| 243 | + * implements IRequestCache:实现这个接口之后可以重新指定这个请求的缓存模式 |
| 244 | + |
241 | 245 | * implements IRequestClient:实现这个接口之后可以重新指定这个请求所用的 OkHttpClient 对象
|
242 | 246 |
|
243 | 247 | * 字段作为请求参数的衡量标准
|
@@ -419,6 +423,127 @@ try {
|
419 | 423 | }
|
420 | 424 | ```
|
421 | 425 |
|
| 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 | + |
422 | 547 | # 疑难解答
|
423 | 548 |
|
424 | 549 | #### 如何添加全局参数
|
|
0 commit comments