android - Issues with Caching on OkHttpClient 2.0 -
i have issue caching using okhttpclient 2.0. seems response ignoring cache-control header completely. how setting client , cache.
okhttpclient client = new okhttpclient(); cache = new cache(new file(session.getinstance().getcontext().getcachedir(),"http"), 10 * 1024 * 1024); client.setcache(cache); client.setcookiehandler(cookiehandler.getdefault()); client.setconnecttimeout(connection_timeout, timeunit.milliseconds); client.setreadtimeout(socket_timeout, timeunit.milliseconds); i believe cache directory created correctly. see in journal in /cache/http directory of application.
libcore.io.disklrucache 1 201105 2 this how creating request.
request mrequest = new request.builder().url(murl).get().build(); getting response :
response response = client.newcall(mrequest).execute(); when using curl, headers as follows.
< http/1.1 200 ok < date: fri, 27 jun 2014 19:39:40 gmt * server apache-coyote/1.1 not blacklisted < server: apache-coyote/1.1 < cache-control: no-transform, max-age=1800 < content-type: application/json < transfer-encoding: chunked the okhttp response headers follows.
connection:keep-alive content-type:application/json date:fri, 27 jun 2014 18:58:30 gmt keep-alive:timeout=5, max=100 okhttp-received-millis:1403895511337 okhttp-selected-protocol:http/1.1 okhttp-sent-millis:1403895511140 server:apache-coyote/1.1 transfer-encoding:chunked the responses never cached , call client.getcache().gethitcount() gives 0. can please suggest changes might required here make cache work? thanks.
okay, problem , post requests using authorization bearer xxxx header , http://www.w3.org/protocols/rfc2616/rfc2616-sec14.html section 14.8 states these requests can't cached. solution use s-maxage on server instead of max age according :
when shared cache (see section 13.7) receives request containing authorization field, must not return corresponding response reply other request, unless 1 of following specific exceptions holds:
if response includes "s-maxage" cache-control directive, cache may use response in replying subsequent request.
Comments
Post a Comment