yale8848 / RetrofitCache

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

请问如何让请求在一段时间内只使用缓存的数据 #24

Closed abcdisgreat closed 5 years ago

abcdisgreat commented 5 years ago

除了第一次数据,这段时间内即使有网络也从缓存拿数据?

yale8848 commented 5 years ago

本身就是这样的,你设置个缓存时间,在这段时间内只走缓存

abcdisgreat commented 5 years ago

@yale8848
@GET @Cache(time = 7,timeUnit = TimeUnit.DAYS) 但是我这样设置的一个请求,连续访问都显示retrofitcache: get data from net,没有从缓存取啊,是我没设置对吗

yale8848 commented 5 years ago

你详细看看README里的使用方法,每一步都要添加

YuPf1989 commented 5 years ago

@yale8848 你解决了吗?我遇到了同样的问题,只有在网络断的时候log提示来自缓存,正常情况一直都是走网络

yale8848 commented 5 years ago

把代码贴出来看看吧

abcdisgreat commented 5 years ago

@YuPf1989 没有解决,作者说有这样的配置但我没找到,介绍里的方法在有网时都没能强制从缓存取

YuPf1989 commented 5 years ago

创建okhttp部分 ` private RetrofitHelper() { retrofit = new Retrofit.Builder() .baseUrl(Constant.BASE_URL) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(MyGsonConverterFactory.create()) .client(getOkHttpClient()) .build();

    RetrofitCache.getInstance().addRetrofit(retrofit);
}

private OkHttpClient getOkHttpClient() {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    // 200M
    int cacheSize = 200 * 1024 * 1024;
    File httpcacheFile = new File(mContext.getCacheDir(), "httpcache");
    Cache cache = new Cache(httpcacheFile, cacheSize);
    builder
            .cache(cache)
            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor(new CacheForceInterceptorNoNet())
            .addNetworkInterceptor(new CacheInterceptorOnNet())
            .connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
            .writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);

    return builder.build();
}`

接口部分 @Cache(time = 2,timeUnit = TimeUnit.MINUTES,forceCacheNoNet = true) @GET("http://47.93.136.56:7011/appInterface/patient/getWikiList.html") Observable<NewsListBean> getWikiList2(); 调用部分 ` RetrofitHelper.getInstance().retrofit.create(RetrofitService.class).getWikiList2() .compose(CacheTransformer.emptyTransformer()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new MyObserver() {

                @Override
                public void onNext(NewsListBean data) {
                    String content = data.toString();
                    tvContent.append("content:" + content + "\n");
                }

                @Override
                public void onError(Throwable e) {
                    super.onError(e);
                }
            });`

log输出 09-26 17:23:36.551 16277-16553/com.rain.rxjava2demo D/retrofitcache: get data from net = 200 09-26 17:23:38.436 319-637/? I/BufferQueueProducer: [com.rain.rxjava2demo/com.rain.rxjava2demo.ui.RetrofitCacheActivity](this:0x7f9fb88400,id:17208,api:1,p:16277,c:319) queueBuffer: fps=27.33 dur=2048.96 max=1156.41 min=8.22 09-26 17:23:38.500 16277-16277/com.rain.rxjava2demo W/retrofitcache: java.lang.NoSuchMethodException: toRequest [class [Ljava.lang.Object;] 09-26 17:23:38.555 16277-16553/com.rain.rxjava2demo D/retrofitcache: get data from net = 200 09-26 17:23:39.495 319-1177/? I/BufferQueueProducer: [com.rain.rxjava2demo/com.rain.rxjava2demo.ui.RetrofitCacheActivity](this:0x7f9fb88400,id:17208,api:1,p:16277,c:319) queueBuffer: fps=51.91 dur=1059.44 max=163.82 min=8.15 09-26 17:23:39.572 16277-16277/com.rain.rxjava2demo W/retrofitcache: java.lang.NoSuchMethodException: toRequest [class [Ljava.lang.Object;] 09-26 17:23:39.629 16277-16553/com.rain.rxjava2demo D/retrofitcache: get data from net = 200 不知道哪个环节出了问题? 还有必须添加 .compose(CacheTransformer.emptyTransformer())吗?

yale8848 commented 5 years ago

.compose(CacheTransformer.emptyTransformer()) 这个必须要加,你更新至1.0.9 试试

YuPf1989 commented 5 years ago

更新过后试了试,在有网络的情况下,仍旧每次请求都提示from net,另外发现一个有意思的问题,在手机全部断开网络的情况下,并且有之前的缓存,如果超出了缓存时间,进行请求的时候,通过查看okhttp的log,会发现okhttp发起了网络请求,提示from net,之后再多次调用,retrofitcache log输出提示的是from cache,但是查看okhttp的log,仍然是进行了网络连接,状态码200(注意都是在没网的情况下),不知道是怎么一回事,为何没有网络http状态码会是200?是我哪里理解的不到位吗?

yale8848 commented 5 years ago

调用:RetrofitCache.getInstance().init(Context) 了吗?

YuPf1989 commented 5 years ago

调用了,现在在看okhttp缓存这一块,可能哪里理解出现了偏差

yale8848 commented 5 years ago

okhttp 从缓存拿数据也是200

bsdson commented 4 months ago

Hi, @YuPf1989, have you figured out what's wrong? I am new to this and have a similar situation that it only uses cache when offline. Later I found that the header of the response contains cache-control directives like this: no-store, no-cache, must-revalidate This seems inhibit OkHttpClient from properly caching. I know we can use interceptor to modify the response, but it seems not affect the OkHttpClient's cache behavior...