Java Code Examples for okhttp3.internal.http.HttpMethod#invalidatesCache()

The following examples show how to use okhttp3.internal.http.HttpMethod#invalidatesCache() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CacheInterceptor.java    From AndroidProjects with MIT License 7 votes vote down vote up
private CacheRequest maybeCache(Response userResponse, Request networkRequest,
    InternalCache responseCache) throws IOException {
  if (responseCache == null) return null;

  // Should we cache this response for this request?
  if (!CacheStrategy.isCacheable(userResponse, networkRequest)) {
    if (HttpMethod.invalidatesCache(networkRequest.method())) {
      try {
        responseCache.remove(networkRequest);
      } catch (IOException ignored) {
        // The cache cannot be written.
      }
    }
    return null;
  }

  // Offer this request to the cache.
  return responseCache.put(userResponse);
}
 
Example 2
Source File: StaleIfErrorInterceptor.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
@Override
public Response intercept(@NonNull final Chain chain) throws IOException {
    Request request = chain.request();
    // Verify that the HTTP method is for loading data only and doesn't have any side-effects,
    // and that the request doesn't contain the 'only-if-cached' Cache-Control directive to
    // force loading from the cache.
    if (!HttpMethod.invalidatesCache(request.method()) &&
            !request.cacheControl().onlyIfCached()) {
        // If the request already has the 'stale-if-error' Cache-Control directive, then proceed
        // the request chain without interference.
        for (final String cacheControlValue : request.headers("Cache-Control")) {
            if (PATTERN_STALE_IF_ERROR.matcher(cacheControlValue).matches()) {
                return chain.proceed(request);
            }
        }
        // Otherwise add a 'stale-if-error' Cache-Control directive, with the maximum stale
        // value set to a very high value.
        request = request.newBuilder()
                .addHeader("Cache-Control", "stale-if-error=" + Integer.MAX_VALUE)
                .build();
    }
    return chain.proceed(request);
}
 
Example 3
Source File: Cache.java    From styT with Apache License 2.0 5 votes vote down vote up
CacheRequest put(Response response) {
  String requestMethod = response.request().method();

  if (HttpMethod.invalidatesCache(response.request().method())) {
    try {
      remove(response.request());
    } catch (IOException ignored) {
      // The cache cannot be written.
    }
    return null;
  }
  if (!requestMethod.equals("GET")) {
    // Don't cache non-GET responses. We're technically allowed to cache
    // HEAD requests and some POST requests, but the complexity of doing
    // so is high and the benefit is low.
    return null;
  }

  if (HttpHeaders.hasVaryAll(response)) {
    return null;
  }

  Entry entry = new Entry(response);
  DiskLruCache.Editor editor = null;
  try {
    editor = cache.edit(key(response.request().url()));
    if (editor == null) {
      return null;
    }
    entry.writeTo(editor);
    return new CacheRequestImpl(editor);
  } catch (IOException e) {
    abortQuietly(editor);
    return null;
  }
}
 
Example 4
Source File: Cache.java    From AndroidProjects with MIT License 5 votes vote down vote up
CacheRequest put(Response response) {
  String requestMethod = response.request().method();

  if (HttpMethod.invalidatesCache(response.request().method())) {
    try {
      remove(response.request());
    } catch (IOException ignored) {
      // The cache cannot be written.
    }
    return null;
  }
  if (!requestMethod.equals("GET")) {
    // Don't cache non-GET responses. We're technically allowed to cache
    // HEAD requests and some POST requests, but the complexity of doing
    // so is high and the benefit is low.
    return null;
  }

  if (HttpHeaders.hasVaryAll(response)) {
    return null;
  }

  Entry entry = new Entry(response);
  DiskLruCache.Editor editor = null;
  try {
    editor = cache.edit(key(response.request().url()));
    if (editor == null) {
      return null;
    }
    entry.writeTo(editor);
    return new CacheRequestImpl(editor);
  } catch (IOException e) {
    abortQuietly(editor);
    return null;
  }
}