rengwuxian / RxJavaSamples

RxJava 2 和 Retrofit 结合使用的几个最常见使用方式举例
3.94k stars 858 forks source link

缓存的问题 #18

Open wzasd opened 7 years ago

wzasd commented 7 years ago

我尝试使用okhttp3自带的缓存机制为什么无法使用呢?您使用过自带的缓存机制吗?望回复

rengwuxian commented 7 years ago

没看太懂,你说的是缓存机制在哪里无法使用?

wzasd commented 7 years ago
static Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        CacheControl.Builder cacheBuilder = new CacheControl.Builder();
        cacheBuilder.maxAge(60, TimeUnit.SECONDS);
        cacheBuilder.maxStale(365,TimeUnit.DAYS);
        CacheControl cacheControl = cacheBuilder.build();
        Request request = chain.request();
        request = request.newBuilder()
                .cacheControl(cacheControl)
                .build();
        Response originalResponse = chain.proceed(request);
        int maxAge = 60; // read from cache
        return originalResponse.newBuilder()
                .removeHeader("Pragma")
                .header("Cache-Control", "public ,max-age=" + maxAge)
                .build();
    }
};

就是自己写的使用okhttp3.0的缓存机制,有木有推荐的缓存办法呢?