Skip to content

Commit

Permalink
variable caching duration
Browse files Browse the repository at this point in the history
  • Loading branch information
janvde committed Feb 19, 2018
1 parent a1413b7 commit 3dc7b52
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ public class Rome2RioApiClient {

private Rome2RioService rome2RioService;
private Context context;
private int cacheDuration = 30; //cache duration of 60 seconds

public Rome2RioApiClient(Context context, String key) {
this(context, key, 30);
}

public Rome2RioApiClient(Context context, String key, int cacheDuration){
this.context = context;

Retrofit retrofit = new Retrofit.Builder()
Expand All @@ -46,6 +51,7 @@ public Rome2RioApiClient(Context context, String key) {
.client(getClient(key))
.build();

this.cacheDuration = cacheDuration;
// Create an instance of our API interface.
rome2RioService = retrofit.create(Rome2RioService.class);
}
Expand Down Expand Up @@ -76,12 +82,11 @@ public Response intercept(Chain chain) throws IOException {
Response originalResponse = chain.proceed(request);

if (ConnectionUtils.isNetworkConnected(context)) {
int maxAge = 30; // read from cache for 30 seconds
return originalResponse.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.header("Cache-Control", "public, max-age=" + cacheDuration)
.build();
} else {
int maxStale = 60 * 60; // tolerate 1 hour stale
int maxStale = 60 * 60 * 24; // tolerate 24 hour stale
return originalResponse.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
Expand Down

0 comments on commit 3dc7b52

Please sign in to comment.