[英]OkHttp exclude APIs from caching
I'm using Retrofit and OkHttp client for making network calls. 我正在使用Retrofit和OkHttp客户端进行网络调用。 My server supports Etag caching and I have added cache to
okHttp
client . 我的服务器支持Etag缓存,并且已将缓存添加到
okHttp
client中。 There are some APIs which I don't want to cache 有一些我不想缓存的API
This is my okHttpClient
config 这是我的
okHttpClient
配置
OkHttpClient okHttpClient(Context context,
HttpLoggingInterceptor loggingInterceptor,Cache okHttpCache) {
final OkHttpClient.Builder builder = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.cache(okHttpCache)
return builder.build();
}
Cache cache(Context context) {
return new Cache(new File(context.getCacheDir(), "cache"),10 * 1024 * 1024);
}
Can I ignore some of the APIs from caching? 我可以忽略某些API进行缓存吗?
If you are using only OkHttp library then you could specify cache control policy as CacheControl.FORCE_NETWORK
to your request object. 如果仅使用OkHttp库,则可以将缓存控制策略指定为请求对象的
CacheControl.FORCE_NETWORK
。 More info here: https://github.com/square/okhttp/issues/1496 更多信息在这里: https : //github.com/square/okhttp/issues/1496
If you are using OkHttp in couple with Retrofit, you could add Cache-Control: no-cache
header to your request definition method inside your interface: (spelling updated in example) 如果将OkHttp与Retrofit结合使用,则可以在接口内的请求定义方法中添加
Cache-Control: no-cache
标头:(示例中拼写已更新)
@Headers("Cache-Control: no-cache")
@GET("users/me")
Call<User> getUser();
Okhttp3 Provides an Iterator for urls request in cache Okhttp3为缓存中的url请求提供一个迭代器
public Iterator<String> urls() throws IOException {
hasNext();
next();
remove();
}
You can simple use this to check against the APIs of interest and ignore it. 您可以简单地使用它来检查感兴趣的API并忽略它。 see doc http://java.sun.com/javase/6/docs/api/java/util/Iterator.html?is-external=true
参见文档http://java.sun.com/javase/6/docs/api/java/util/Iterator.html?is-external=true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.