Skip to content

Commit ce22279

Browse files
committed
cache response with okhttp
1 parent 23ecf62 commit ce22279

File tree

3 files changed

+175
-115
lines changed

3 files changed

+175
-115
lines changed

Book-RESTful-Service/src/main/java/FcmTest.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

okhttp-examples/src/main/java/com/howtoprogram/okhttp/cache/CacheOkHttp.java

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package com.howtoprogram.okhttp.cache;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.concurrent.TimeUnit;
6+
7+
import okhttp3.Cache;
8+
import okhttp3.CacheControl;
9+
import okhttp3.OkHttpClient;
10+
import okhttp3.Request;
11+
import okhttp3.Response;
12+
13+
public class CacheOkHttpExample {
14+
15+
private final OkHttpClient client;
16+
17+
public CacheOkHttpExample(File cacheDirectory) throws Exception {
18+
int cacheSize = 10 * 1024 * 1024; // 10 MiB
19+
Cache cache = new Cache(cacheDirectory, cacheSize);
20+
// Just to clear all previous caches on the given directory to make sure
21+
// all our tests run correctly
22+
cache.evictAll();
23+
client = new OkHttpClient.Builder().cache(cache).build();
24+
}
25+
26+
public void getMasterData() throws Exception {
27+
28+
// The below URL will produce the response with cache in 20 seconds
29+
Request request = new Request.Builder().url("http://httpbin.org/cache/20").build();
30+
31+
Response response1 = client.newCall(request).execute();
32+
if (!response1.isSuccessful()) {
33+
throw new IOException("Unexpected code " + response1);
34+
}
35+
36+
String response1Body = response1.body().string();
37+
System.out.println("Res1 response: " + response1);
38+
System.out.println("Res1 cache response: " + response1.cacheResponse());
39+
System.out.println("Res1 network response:" + response1.networkResponse());
40+
41+
Response response2 = client.newCall(request).execute();
42+
if (!response2.isSuccessful()) {
43+
throw new IOException("Unexpected code " + response2);
44+
}
45+
46+
String response2Body = response2.body().string();
47+
System.out.println("Res2 response: " + response2);
48+
System.out.println("Res2 cache response: " + response2.cacheResponse());
49+
System.out.println("Res2 network response: " + response2.networkResponse());
50+
System.out.println("Res2 equals Res1? " + response1Body.equals(response2Body));
51+
52+
// To make sure the previous cached was stale
53+
Thread.sleep(21000);
54+
55+
Response response3 = client.newCall(request).execute();
56+
if (!response3.isSuccessful()) {
57+
throw new IOException("Unexpected code " + response3);
58+
}
59+
String response3Body = response3.body().string();
60+
System.out.println("Res3 response: " + response3);
61+
System.out.println("Res3 cache response: " + response3.cacheResponse());
62+
System.out.println("Res3 network response:" + response3.networkResponse());
63+
64+
}
65+
66+
public void getLocationMaterData() throws Exception {
67+
// The below URL will produce the response with cache in 20 seconds
68+
Request request = new Request.Builder().url("http://httpbin.org/cache/20").build();
69+
70+
Response response4 = client.newCall(request).execute();
71+
if (!response4.isSuccessful()) {
72+
throw new IOException("Unexpected code " + response4);
73+
}
74+
String response4Body = response4.body().string();
75+
System.out.println("Res4 response: " + response4);
76+
System.out.println("Res4 cache response: " + response4.cacheResponse());
77+
System.out.println("Res4 network response:" + response4.networkResponse());
78+
79+
request = new Request.Builder().cacheControl(new CacheControl.Builder().noCache().build())
80+
.url("http://httpbin.org/cache/20").build();
81+
82+
Response response5 = client.newCall(request).execute();
83+
if (!response5.isSuccessful()) {
84+
throw new IOException("Unexpected code " + response5);
85+
}
86+
String response5Body = response5.body().string();
87+
System.out.println("Res5 response: " + response5);
88+
System.out.println("Res5 cache response: " + response5.cacheResponse());
89+
System.out.println("Res5 network response: " + response5.networkResponse());
90+
}
91+
92+
public void getOperatorMaterData() throws Exception {
93+
// The below URL will produce the response with cache in 5 seconds
94+
Request request = new Request.Builder().url("http://httpbin.org/cache/20").build();
95+
96+
Response response6 = client.newCall(request).execute();
97+
if (!response6.isSuccessful()) {
98+
throw new IOException("Unexpected code " + response6);
99+
}
100+
String response6Body = response6.body().string();
101+
System.out.println("Res6 response: " + response6);
102+
System.out.println("Res6 cache response: " + response6.cacheResponse());
103+
System.out.println("Res6 network response:" + response6.networkResponse());
104+
105+
// Only accept the response if it is in the cache. If the response isn't
106+
// cached, a 504 Unsatisfiable Request response will be returned.
107+
108+
request = new Request.Builder()
109+
.cacheControl(new CacheControl.Builder().onlyIfCached().build())
110+
.url("http://httpbin.org/cache/20").build();
111+
112+
Response response7 = client.newCall(request).execute();
113+
114+
String response7Body = response7.body().string();
115+
System.out.println("Res7 response: " + response7);
116+
System.out.println("Res7 cache response: " + response7.cacheResponse());
117+
System.out.println("Res7 network response: " + response7.networkResponse());
118+
119+
// Sleep 20 seconds to make sure the cache will be stale. Then the 504
120+
// error will be thrown.
121+
Thread.sleep(20000);
122+
123+
Response response8 = client.newCall(request).execute();
124+
125+
String response8Body = response8.body().string();
126+
System.out.println("Res8 response: " + response8);
127+
System.out.println("Res8 cache response: " + response8.cacheResponse());
128+
System.out.println("Res8 network response: " + response8.networkResponse());
129+
130+
}
131+
132+
public void getZoneMaterData() throws Exception {
133+
// The below URL will produce the response with cache in 20 seconds
134+
Request request = new Request.Builder().url("http://httpbin.org/cache/20").build();
135+
136+
Response response9 = client.newCall(request).execute();
137+
if (!response9.isSuccessful()) {
138+
throw new IOException("Unexpected code " + response9);
139+
}
140+
String response9Body = response9.body().string();
141+
System.out.println("Res9 response: " + response9);
142+
System.out.println("Res9 cache response: " + response9.cacheResponse());
143+
System.out.println("Res9 network response:" + response9.networkResponse());
144+
145+
// Sleep 22 seconds to make sure the cache will be stale.
146+
Thread.sleep(22000);
147+
request = new Request.Builder()
148+
.cacheControl(new CacheControl.Builder().onlyIfCached().build())
149+
.url("http://httpbin.org/cache/20").build();
150+
151+
Response response10 = client.newCall(request).execute();
152+
153+
String response10Body = response10.body().string();
154+
System.out.println("Res10 response: " + response10);
155+
156+
request = new Request.Builder()
157+
.cacheControl(new CacheControl.Builder().maxStale(7, TimeUnit.DAYS).build())
158+
.url("http://httpbin.org/cache/20").build();
159+
160+
Response response11 = client.newCall(request).execute();
161+
String response11Body = response11.body().string();
162+
System.out.println("Res11 response: " + response11);
163+
System.out.println("Res11 cache response: " + response11.cacheResponse());
164+
System.out.println("Res11 network response:" + response11.networkResponse());
165+
166+
}
167+
168+
public static void main(String args[]) throws Exception {
169+
File cacheLoc = new File("/var/tmp/okhttp");
170+
CacheOkHttpExample cacheOkHttp = new CacheOkHttpExample(cacheLoc);
171+
cacheOkHttp.getLocationMaterData();
172+
// cacheOkHttp.getOperatorMaterData();
173+
// cacheOkHttp.getZoneMaterData();
174+
}
175+
}

0 commit comments

Comments
 (0)