codeestX / GeekNews

:books:A pure reading App based on Material Design + MVP + RxJava2 + Retrofit + Dagger2 + Realm + Glide
3.5k stars 826 forks source link

缓存问题 #103

Open luckyjmcc opened 7 years ago

luckyjmcc commented 7 years ago

首先致敬神作! 现在的缓存应该是有问题的,掘金的服务器是禁止缓存的,无法达到缓存目的。问题在HttpModule:


@Singleton
    @Provides
    OkHttpClient provideClient(OkHttpClient.Builder builder) {
       .....
        Interceptor cacheInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                if (!SystemUtil.isNetworkConnected()) {
                    request = request.newBuilder()
                            .cacheControl(CacheControl.FORCE_CACHE)
                            .build();
                }
                Response response = chain.proceed(request);
                if (SystemUtil.isNetworkConnected()) {
                    int maxAge = 0;
                    response.newBuilder()
                            .header("Cache-Control", "public, max-age=" + maxAge)
                            .removeHeader("Pragma")
                            .build();
                } else {
                    int maxStale = 60 * 60 * 24 * 28;
                    response.newBuilder()
                            .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                            .removeHeader("Pragma")
                            .build();
                }
                return response;//直接返回了之前的response是达不到缓存目的的
            }
        };
       .....
    }

应该改为

 response =  response.newBuilder()
                            .removeHeader("Pragma")
                            .removeHeader("Cache-Control")
                            .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                            //.removeHeader("Pragma")
                            .build();
ghost commented 6 years ago

在自己的项目中上面的两种都没有实现离线缓存,请问除了拦截器还需要配置哪里吗?

luckyjmcc commented 6 years ago

post无法缓存

尹宝华 邮箱:iloveaman@163.com

签名由 网易邮箱大师 定制

在2018年10月18日 14:46,1391884664 写道:

在自己的项目中上面的两种都没有实现离线缓存,请问除了拦截器还需要配置哪里吗?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.