I'm using Retrofit and OkHttp client for making network calls. My server supports Etag caching and I have added cache to okHttp
client . There are some APIs which I don't want to cache
This is my okHttpClient
config
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?
If you are using only OkHttp library then you could specify cache control policy as CacheControl.FORCE_NETWORK
to your request object. More info here: 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)
@Headers("Cache-Control: no-cache")
@GET("users/me")
Call<User> getUser();
Okhttp3 Provides an Iterator for urls request in cache
public Iterator<String> urls() throws IOException {
hasNext();
next();
remove();
}
You can simple use this to check against the APIs of interest and ignore it. see doc http://java.sun.com/javase/6/docs/api/java/util/Iterator.html?is-external=true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.