0
votes

I'm using OkHttp to cache responses to PUT requests that use an "Authorize" header for authentication, and am not seeing any caching.

The Android client code sets the cache like this:

int cacheSize = 10 * 1024 * 1024; // 10 MiB

cache = new Cache(context.getCacheDir(), cacheSize);

client.setCache(cache);

My server is responding with:

Cache-Control: public, max-age=60, s-maxage=60

When I log the cache, getRequestCount() increments, but there are no hits, or any urls in the cache.

Any idea what I'm doing wrong?

Thanks!

1
Are you using 2.2? The cache policy changed recently.Jesse Wilson
Hi Jesse - yes I'm using 2.2, the latest. It looks like response caching only works for GET, not POSTMartin
Yes. Response caching only works with GET.Jesse Wilson

1 Answers

0
votes

Only GET responses are cached, so my fix was to convert to GETs. In OkHttp Cache.java:-

if (!requestMethod.equals("GET")) {
  // Don't cache non-GET responses. We're technically allowed to cache
  // HEAD requests and some POST requests, but the complexity of doing
  // so is high and the benefit is low.
  return null;
}