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

Explanation of caching and ETAG? #678

Closed
AAverin opened this Issue Dec 15, 2014 · 4 comments

Comments

2 participants
@AAverin
Copy link

AAverin commented Dec 15, 2014

Is there a proper explanation on how to add caching and ETAG/If-None-Match support to Retrofit+OkHttp?
I'm struggling adding Etag support on 2 projects, and if at first I suspected that there might be an issue with HTTP Headers, another project has everything set correctly and caching still doesn't work as expected.

Following are my attempts to make it work. Results show that caching seems to be working within the same instance of the application, but as soon as I restart - everything loads long again.
Also, in my logs I didn't see If-None-Match being added to a request, so I assume that server isn't aware of ETag and still recalculates the response completely.

Here are some code samples:

 public class RetrofitHttpClient extends UrlConnectionClient {

    private OkUrlFactory generateDefaultOkUrlFactory() {
        OkHttpClient client = new com.squareup.okhttp.OkHttpClient();

        try {
            Cache responseCache = new Cache(baseContext.getCacheDir(), SIZE_OF_CACHE);
            client.setCache(responseCache);
        } catch (Exception e) {
            Logger.log(this, e, "Unable to set http cache");
        }

        client.setConnectTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS);
        client.setReadTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS);
        return new OkUrlFactory(client);
    }

    private final OkUrlFactory factory;

    public RetrofitHttpClient() {
        factory = generateDefaultOkUrlFactory();
    }

    @Override
    protected HttpURLConnection openConnection(retrofit.client.Request request) throws IOException {
        return factory.open(new URL(request.getUrl()));
    }
}

Rest adapter is then created with FULL log level and a custom tag:

restAdapter = new RestAdapter.Builder()
            .setClient(new RetrofitHttpClient())
            .setEndpoint(Config.BASE_URL)
            .setRequestInterceptor(new SignatureSetter())
            .setConverter(new JacksonConverter(JsonHelper.getObjectMapper()))
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setLog(new AndroidLog("=NETWORK="))
            .build();

I have a long request on the first screen of the app for testing.
When I open the app - it takes 7 seconds to complete the request. If I pause and resume the app - same request takes 250ms, clearly hitting the cache. If I close the app completely and restart - it again takes 7 seconds.

@AAverin

This comment has been minimized.

@JakeWharton

This comment has been minimized.

Copy link
Collaborator

JakeWharton commented Jan 2, 2015

Retrofit has nothing to do with caching so there's nothing to talk about there. OkHttp just implements RFC 2616 caching which is the spec for HTTP. Not all of the flags are implemented, but most are. Ensure you are using the latest version of OkHttp (2.2.0) to be sure.

I found this article to be a good summary of how cache header values work: http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/. If you server is sending proper cache headers this should just work. If you aren't seeing proper behavior then please file an issue on OkHttp with the response headers from the server, the expected behavior, and the observed behavior.

Retrofit is just an HTTP client consumer so it's 100% agnostic and opaque to any caching strategy implemented by that client.

@JakeWharton JakeWharton closed this Jan 2, 2015

@AAverin

This comment has been minimized.

Copy link

AAverin commented Jan 2, 2015

Should I add a question about ETag in general and missing If-None-Match header in request in OkHttp repo?

@JakeWharton

This comment has been minimized.

Copy link
Collaborator

JakeWharton commented Jan 2, 2015

Sure. I'm not familiar enough with that code to know the behavior but it'll get answered there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment