简体   繁体   中英

Cache Control gets ignored

I am currently working with Retrofit and Okhttp and I am trying to cache some GET responses. My Code is:

OkHttpClient okHttpClient = new OkHttpClient();
    File cacheDir = new File(System.getProperty("java.io.tmpdir"),
            "ddcache");
    HttpResponseCache cache = new HttpResponseCache(cacheDir, 2024);
    okHttpClient.setResponseCache(cache);
    OkClient cl=new OkClient(okHttpClient);
    restAdapter = new RestAdapter.Builder().setEndpoint(API_URL)
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setClient(cl).build();

And the Log shows this header:

HTTP/1.1 200 OK
Cache-Control: max-age=7200
Connection: Keep-Alive
Content-Type: text/html
Date: Tue, 18 Mar 2014 18:38:16 GMT
Keep-Alive: timeout=3, max=100
OkHttp-Received-Millis: 1395167895452
OkHttp-Response-Source: NETWORK 200
OkHttp-Sent-Millis: 1395167895378
Server: Apache/2.2.26 (Unix)
Transfer-Encoding: chunked
X-Powered-By: PHP/5.3.28

I check the response by returning the Server Unix Time on every call and it always returns a new one which means the

Cache-Control: max-age=7200

gets totally ignored The Journal File in the Cache gets also updated with "CLEAN" and "DIRTY" notes, but nothing gets cached. Is there something obvious I do not see?

I think I had a similar problem. The size of the cache is set in kilobytes and you are setting it to only 2024 kilobytes. That not enough space for almost anything. Try setting it to "10L * 1024 * 1024" (10Mb) and see if that helps.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM