1
어떤 이유로 OkHttp response.cacheResponse()
은 요청을 캐시해야한다고해도 항상 null을 반환합니다. 다음 코드에서는 http://httpbin.org/cache/60
두 요청을 만들고 있는데 두 번째 요청은 캐시에서 가져올 것으로 예상됩니다. 그러나 출력에서 볼 수있는 것처럼 그렇지 않습니다.OkHttp는 요청을 캐시하지 않습니다.
무엇이 누락 되었습니까?
코드 :
@Test
public void test2_okhttp() throws IOException {
int cacheSize = 100 * 1024 * 1024; // 100 MiB
File cacheDir = new File("/tmp/ok_cache");
cacheDir.mkdirs();
Cache cache = new Cache(cacheDir, cacheSize);
OkHttpClient client = new OkHttpClient.Builder()
.cache(cache)
.build();
String url = "http://httpbin.org/cache/60";
for(int i = 0; i < 2; i++) {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
System.out.println("response1.cacheResponse() = " + response.cacheResponse());
System.out.println("response1.networkResponse() = " + response.networkResponse());
}
}
출력 :
response1.cacheResponse() = null
response1.networkResponse() = Response{protocol=http/1.1, code=200, message=OK, url=http://httpbin.org/cache/60}
response1.cacheResponse() = null
response1.networkResponse() = Response{protocol=http/1.1, code=200, message=OK, url=http://httpbin.org/cache/60}