Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

题主:OkHttp3的cacheControl无效 #1

Open
vihuela opened this issue Jan 8, 2016 · 2 comments
Open

题主:OkHttp3的cacheControl无效 #1

vihuela opened this issue Jan 8, 2016 · 2 comments

Comments

@vihuela
Copy link

vihuela commented Jan 8, 2016

在OkHttp 2.6 下,
public Builder cacheControl(CacheControl cacheControl) {
String value = cacheControl.toString();
if (value.isEmpty()) return removeHeader("Cache-Control");
return header("Cache-Control", value);
}
设置了策略会覆盖头字段的,所以头字段写缓存字段 没必要。

然后在OkHttp 3.0下
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3'
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'

用最新的引用,cacheControl无效,我研究了一下午,题主可以一起研究下

@duzechao
Copy link
Owner

duzechao commented Jan 8, 2016

虽然设置cacheControl()会覆盖了header,但是,已经添加了一个拦截器修改Cache-Control,这个是在cacheControl()之后的,其实OkHttp会将请求保存成一个文件存在手机上的,但是假如用了CacheControl.FORCE_NETWORK,就请求的时候不会去访问文件,所以就会去访问网络数据,但是在访问网络数据的时候,经过拦截器修改,Cache-Control就会变成我们设置的缓存时间

@vihuela
Copy link
Author

vihuela commented Jan 8, 2016

朋友你刚刚说,拦截器起作用是在cacheControl之后才有效?理解了。
其实我觉得,Cache-Control 这种东西设置全局意义不大,所有接口不可能都是需要那么长的缓存时间,用retrofit2,每个接口可以设置单独的Cache-Control,才有意义。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants