Parameterized the duration of maxAge and maxStale of the request cache

This commit is contained in:
CappielloAntonio
2021-09-04 15:46:49 +02:00
parent 374a9c0334
commit ea7b79893d
9 changed files with 13 additions and 11 deletions

View File

@@ -9,14 +9,17 @@ import okhttp3.Request;
public class CacheUtil {
private final Context context;
private int maxAge; // 60 seconds
private int maxStale; // 60 * 60 * 24 * 30 = 30 days (60 seconds * 60 minutes * 24 hours * 30 days)
public CacheUtil(Context context) {
public CacheUtil(Context context, int maxAge, int maxStale) {
this.context = context;
this.maxAge = maxAge;
this.maxStale = maxStale;
}
public Interceptor onlineInterceptor = chain -> {
okhttp3.Response response = chain.proceed(chain.request());
int maxAge = 60; // 60 seconds
return response.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.removeHeader("Pragma")
@@ -26,7 +29,6 @@ public class CacheUtil {
public Interceptor offlineInterceptor = chain -> {
Request request = chain.request();
if (!isConnected()) {
int maxStale = 60 * 60 * 24 * 30; // 30 days (60 seconds * 60 minutes * 24 hours * 30 days)
request = request.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.removeHeader("Pragma")