• Stars
    star
    673
  • Rank 66,754 (Top 2 %)
  • Language
    Java
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RetrofitCache让retrofit2+okhttp3+rxjava配置缓存如此简单。通过注解配置,可以针对每一个接口灵活配置缓存策略;同时让每一个接口方便支持数据模拟,可以代码减小侵入性,模拟数据可以从内存,Assets,url轻松获取。

RetrofitCache

English

RetrofitCache让retrofit2+okhttp3+rxjava 配置缓存如此简单。通过注解配置,可以针对每一个接口灵活配置缓存策略;同时让每一个接口方便支持数据模拟,可以代码减小侵入性,模拟数据可以从内存,Assets,url轻松获取。

为什么使用RetrofitCache

  • 服务端接口不严格按照http缓存策略配置,有些不会针对每一个请求单独配置缓存策略
  • 第三方缓存库不是很方便的针对每一个接口进行缓存策略配置,侵入性比较大
  • 很方便的针对每个接口添加模拟数据

调用例子

  • 不走缓存例子
@GET("users")
Observable<HttpResult> test();
  • 缓存设置为20秒
@Cache(time = 20)
@GET("users")
Observable<HttpResult> test();
  • 缓存设置为20分钟
@Cache(time = 20,timeUnit = TimeUnit.MINUTES)
@GET("users")
Observable<HttpResult> test();
  • 默认时间缓存,默认是0秒
@Cache()
@GET("users")
Observable<HttpResult> test();
  • 默认在无网的时候强制走缓存,forceCacheNoNet=false时无网络时不强制缓存
@Cache(forceCacheNoNet = false)
@GET("users")
Observable<HttpResult> test();
  • 添加模拟数据(value,assets,url同时都配置的话,就按照这个顺序处理)
@Mock(value = "{\"data\":\"mockdata\"}") //模拟内存数据
@GET("users")
Observable<HttpResult> test();
@Mock(assets = "mock/mock.json") //从assets获取模拟数据
@GET("users")
Observable<HttpResult> test();
@Mock(url = "http://url.com/test") //从新的url请求数据
@GET("users")
Observable<HttpResult> test();

缓存只对http Get请求有效;如果要问为什么,可以问问后台开发同学

使用方法:

  • 添加 jcenter lib,注意根据自己的库选择
compile 'ren.yale.android:retrofitcachelib:1.1.1'   //retrofit2+okhttp3+rxjava1
compile 'ren.yale.android:retrofitcachelibrx2:1.1.1'   //retrofit2+okhttp3+rxjava2
  • 在Android Application里初始化
RetrofitCache.getInstance().init(this);

也可以修改默认配置,默认time=0,timeUnit = TimeUnit.SECONDS

RetrofitCache.getInstance().init(this).setDefaultTimeUnit(TimeUnit.MINUTES).setDefaultTime(1);
  • OkHttpClient初始化时配置缓存目录
okhttp3.OkHttpClient.Builder clientBuilder=new okhttp3.OkHttpClient.Builder();
...
int cacheSize = 200 * 1024 * 1024;
File cacheDirectory = new File(mContext.getCacheDir(), "httpcache");
Cache cache = new Cache(cacheDirectory, cacheSize);
OkHttpClient client =  clientBuilder.cache(cache).build();
...

  • 给okhttp添加拦截器
okhttp3.OkHttpClient.Builder clientBuilder=new okhttp3.OkHttpClient.Builder();
...

//clientBuilder.addInterceptor(new MockInterceptor()); //如果只用Mock模拟功能,只需添加这个拦截器
clientBuilder.addInterceptor(new CacheForceInterceptorNoNet());
clientBuilder.addNetworkInterceptor(new CacheInterceptorOnNet());
...

添加CacheForceInterceptorNoNet作用是在无网时强制走缓存,如果只添加了CacheInterceptorOnNet,那么在有网和无网的缓存策略就会一样

  • 添加retrofit对象
Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .client(getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
RetrofitCache.getInatance().addRetrofit(retrofit);
  • 添加 rx Observable compose
api.test().compose(CacheTransformer.emptyTransformer())...

进阶

  • setCacheInterceptorListener 设置是否每一个接口都缓存
RetrofitCache.getInstance().setCacheInterceptorListener(
                new CacheInterceptorListener() {
            @Override
            public boolean canCache(Request request,Response response) {
                String res = "";
                try {
                    res = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return true;
            }
});

  • 设置是否走模拟数据,比如说在正式接口好了后可以如下设置,让模拟数据失效
RetrofitCache.getInstance().enableMock(false);
  • 忽略某个参数;如果你对原url增加参数,可以设置忽略
 RetrofitCache.getInstance().addIgnoreParam("access_token");

混淆配置(retrofit2+okhttp3+rxjava1)

-dontwarn ren.yale.android.retrofitcachelib.**
-keep class ren.yale.android.retrofitcachelib.** { *; }
-keepclasseswithmembernames class rx.Observable { *; }
-keepclasseswithmembernames class rx.internal.operators.OnSubscribeLift { *; }
-keepclasseswithmembernames class retrofit2.adapter.rxjava.RxJavaCallAdapterFactory { *; }
-keepclasseswithmembernames class retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe { *; }
-keepclasseswithmembernames class retrofit2.Retrofit { *; }
-keepclasseswithmembernames class retrofit2.ServiceMethod { *; }
-keepclasseswithmembernames class retrofit2.OkHttpCall { *; }

###retrofit2 2.5.0
-keepclasseswithmembernames class retrofit2.HttpServiceMethod {*;}
-keepclasseswithmembernames class retrofit2.RequestFactory {*;}

#retrofit2,okhttp3,rxjava1等其它混淆配置请自行添加

混淆配置(retrofit2+okhttp3+rxjava2)

-dontwarn ren.yale.android.retrofitcachelibrx2.**
-keep class ren.yale.android.retrofitcachelibrx2.** { *; }
-keepclasseswithmembernames class  retrofit2.adapter.rxjava2.BodyObservable { *; }
-keepclasseswithmembernames class  retrofit2.adapter.rxjava2.ResultObservable { *; }
-keepclasseswithmembernames class  retrofit2.adapter.rxjava2.CallEnqueueObservable { *; }
-keepclasseswithmembernames class  retrofit2.adapter.rxjava2.CallExecuteObservable { *; }
-keepclasseswithmembernames class retrofit2.Retrofit { *; }
-keepclasseswithmembernames class retrofit2.ServiceMethod { *; }
-keepclasseswithmembernames class retrofit2.OkHttpCall { *; }

###retrofit2 2.5.0
-keepclasseswithmembernames class retrofit2.HttpServiceMethod {*;}
-keepclasseswithmembernames class retrofit2.RequestFactory {*;}

#retrofit2,okhttp3,rxjava2等其它混淆配置请自行添加

欢迎提问讨论

讨论区

贡献代码

贡献代码

开源协议

MIT License

Copyright (c) 2017 Yale

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

More Repositories

1

CacheWebView

Custom implement Android WebView cache, offline website, let cahe config more simple and flexible
Java
1,910
star
2

EasyIncrementalUpdate

Android差分补丁库,通过native层合并APK,实现增量更新升级,让你更新的APK更小。
C
238
star
3

Summer

Vertx router with JAX-RS
Java
64
star
4

quickapp-wechat

快应用实现的微信Demo
JavaScript
47
star
5

EasyJSBridge

让JS在Android/iOS WebView中反调接口统一,调用更容易
JavaScript
42
star
6

gorpool

Simple Goroutine pool
Go
35
star
7

stream

Golang stream lib is like Java 8 stream. Only handle slice or array.
Go
9
star
8

deploy

通过SSH连接,简单的配置文件,可以配置私钥,可以配置socks5,让部署更简单
Go
7
star
9

JavaAutoDeployClient

java auto deploy
Java
6
star
10

weixin-service

Golang微信,小程序后台接口
Go
5
star
11

gosocket

Simple Golang server socket long connection skeleton
Go
5
star
12

gank-uni-app

gank-uni-app 通过uni-app框架,调用gank.io接口写的干货集中营Demo,通过HBuilderX将项目打包为APP,包括iOS、Android、H5、微信小程序,支付宝小程序,百度小程序、头条小程序
CSS
5
star
13

VideoUtil

给视频添加关键帧工具
Go
4
star
14

doris-stream-loader

Apache Doris Stream Loader Golang API
Go
4
star
15

url-to-pdf-api-docker

url-to-pdf-api docker build
HTML
3
star
16

gffmpeg

golang ffmpeg wrapper
Go
3
star
17

ScrollViewSlipping

android ScrollViewSlipping
Java
3
star
18

nw-windows-easy-package

node-webkit windows easy package
HTML
2
star
19

PopList

Android poplist
Java
2
star
20

caddy-build

caddy build with some plugins
2
star
21

Roadmap

Go
1
star
22

Words

Words is for sentence util kit.
Java
1
star
23

go-commons

go-commons
Go
1
star
24

timer-message

Simple windows timer to show message box.
Go
1
star
25

vue-installer

vue component installer from github
JavaScript
1
star
26

xlsx2db

load xlsx file to db
Go
1
star
27

goutils

Go
1
star
28

ImageDeskew

ImageDeskew
Python
1
star
29

Axis1WebServiceClient

Axis1 WebService 客户端 例子,通过wsdl文件生成客户端代码并调用
Java
1
star
30

Axis1WebService

Axis1 WebService 例子,通过wsdl文件生成服务器端代码并部署
Java
1
star
31

deployx

Node deploy cli utils
JavaScript
1
star
32

wkvideoplayer

Java
1
star
33

ratelimiter

ratelimiter is wrapper token-bucket and count-limit by golang
Go
1
star
34

DataUpdater

Golang remote data update with timer lib. You can update config auto.
Go
1
star
35

vue-admin-frame

Simple vue admin frame demo, use vue2.0, element-ui
Vue
1
star