20

Is there a way to detect if a Retrofit response comes from the configured OkHttp cache or is a live response?

Client definition:

Cache cache = new Cache(getCacheDirectory(context), 1024 * 1024 * 10);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .cache(cache)
            .build();

Api definition:

@GET("/object")
Observable<Result<SomeObject>> getSomeObject();

Example call:

RetroApi retroApi = new Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl(baseUrl)
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(RetroApi.class);

result = retroApi.getSomeObject().subscribe((Result<SomeObject> someObjectResult) -> {
     isFromCache(someObjectResult); // ???
});

5 Answers 5

27

Any time you have an okhttp3.Response (retrofit2.Response.raw()), you can check if the response is from the cache.

To quote Jesse Wilson:

There are a few combos.

.networkResponse() only – your request was served from network exclusively.

.cacheResponse() only – your request was served from cache exclusively.

.networkResponse() and .cacheResponse() – your request was a conditional GET, so headers are from the network and body is from the cache.

So for your example, the isFromCache method would look like:

boolean isFromCache(Result<?> result) {
  return result.response().raw().networkResponse() == null;
}
3
  • Does this quotation come from a talk or some other online resource? If so, could you please provide a link? Jan 7, 2018 at 22:18
  • 2
    It's actually better to check result.response().raw().cacheResponse() != null. If the client re-validates the cached response and receives a 304, both networkResponse() and cacheResponse() will be non-null.
    – apottere
    Mar 6, 2018 at 17:25
  • Yes, depending on the semantics and what your desired app behavior is. Mar 7, 2018 at 0:24
8

Solution 1:

By using the okhttp3.Response.cacheResponse() you can check if the response was received from the cache:

Returns the raw response received from the cache. Will be null if this response didn't use the cache

To get the raw OkHttp response from your retrofit Response obect use .raw(). i.e.:

public boolean isFromCache(Result<?> retroResult) {
    return retroResult.response().raw().cacheResponse() != null;
}

Solution 2:

You can add an Interceptor to the OkHttp Client via new OkHttpClient.Builder().addInterceptor(...). In this Interceptor retrieve the response via chain.proceed(chain.request). Then use Response.cacheResponse() respectively Response.networkResponse() to determine where the response came from.

Full code:

new OkHttpClient.Builder().addInterceptor(chain -> {
        Response response = chain.proceed(chain.request());
        if (response.cacheResponse != null) {
            // from cache
        } else if (response.networkResponse != null) {
            // from network
        }
        return response;
});
3
  • 3
    it's always indicating that the response comes from cache ! Even if I uninstall the application !
    – Fakher
    May 19, 2017 at 1:15
  • 3
    Solution 2 contains logical error. According to OkHttp documentation "cacheControl() is never null, even if this response contains no Cache-Control header"
    – ITurchenko
    Apr 5, 2019 at 9:36
  • 1
    Should be cacheResponse(), not cacheControl() Nov 8, 2020 at 1:34
3

Too late but maybe this would be helpful for someone. When content comes from network: response.networkResponse() and response.cacheResponse() are not null, when content comes from cache response.networkResponse() is null, so code from @sebastian:

new OkHttpClient.Builder().addInterceptor(chain -> {
        Response response = chain.proceed(chain.request());
        if (response.cacheControl() != null) {
            // from cache
        } else if (response.networkResponse() != null) {
            // from network
        }
        return response;
});

just replace with this code:

OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
okHttpClientBuilder.addInterceptor(new Interceptor() {
    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        if (response.networkResponse() != null) {
            Log.v(TAG, "Response from networkResponse(): " + response.networkResponse());
        } else if (response.cacheResponse() != null) {
            Log.v(TAG, "Response from cacheControl(): " + response.cacheResponse());
        }
        return response;
    }
});
2
if (response.raw().cacheResponse() != null) {  
    // true: response was served from cache
}

if (response.raw().networkResponse() != null) {  
    // true: response was served from network/server
}

But I would suggest you to check if your networkResponse is null, since once the request is cached and you have a networkResponse too, your response passed down the stream is from network and not cache(Depending on the maxStale you might have specified on CacheControl). So check if networkResponse is not null, if null and your cacheResponse is not null, the response is served from cache else vice versa.

0

The response comes from cache if cacheResponse exists AND networkResponse (if exists) has a response code of 304. See: https://github.com/square/okhttp/issues/4539

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.