Skip to content

Commit

Permalink
Do not cache /me responses
Browse files Browse the repository at this point in the history
  • Loading branch information
shalzz committed May 4, 2018
1 parent 3df4135 commit 19b0ebe
Showing 1 changed file with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import timber.log.Timber;

public class CacheControlInterceptor implements Interceptor {
private Context mContext;
Expand All @@ -26,27 +27,34 @@ public CacheControlInterceptor(@ApplicationContext Context context) {

@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request();
if (NetworkUtil.isNetworkConnected(mContext)) {
Response originalResponse = chain.proceed(chain.request());
int maxAge = 60; // read from cache for 1 minute
return originalResponse.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.build();

} else {
Request request = chain.request();
// only for the 'verify' api route
if (request.url().encodedPath().equals("/api/v1/verify")) {
CacheControl cacheControl = new CacheControl.Builder()
.onlyIfCached()
.maxStale(7, TimeUnit.DAYS)
// Do not cache the '/me' api route
if (request.url().encodedPath().equals("/api/v1/me")) {
Response originalResponse = chain.proceed(request);
return originalResponse.newBuilder()
.header("Cache-Control", "public, max-age=0")
.build();

request = request.newBuilder()
.cacheControl(cacheControl)
}
else {
Timber.d("Caching: %s",request.url().encodedPath());
Response originalResponse = chain.proceed(request);
int maxAge = 60; // read from cache for 1 minute
return originalResponse.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
}
return chain.proceed(request);
// only for the 'verify' api route
} else if (request.url().encodedPath().equals("/api/v1/verify")) {
CacheControl cacheControl = new CacheControl.Builder()
.onlyIfCached()
.maxStale(7, TimeUnit.DAYS)
.build();

request = request.newBuilder()
.cacheControl(cacheControl)
.build();
}
return chain.proceed(request);
}
}

0 comments on commit 19b0ebe

Please sign in to comment.