示例#1
0
 private void parseCache(Request request, T object, String string, String mimeType)
     throws UnsupportedEncodingException {
   com.squareup.okhttp.CacheControl cacheControl = request.cacheControl();
   if (cacheControl != null) {
     if (!cacheControl.noCache() && !cacheControl.noStore()) {
       if (object instanceof EAResult) {
         EAResult kResult = (EAResult) object;
         if (kResult != null && kResult.isSuccess()) {
           long now = System.currentTimeMillis();
           long maxAge = cacheControl.maxAgeSeconds();
           long softExpire = now + maxAge * 1000;
           System.out.println("缓存时长:" + (softExpire - now) / 1000 + "秒");
           Cache.Entry entry = new Cache.Entry();
           entry.softTtl = softExpire;
           entry.ttl = entry.softTtl;
           // entry.serverDate = serverDate;
           // entry.responseHeaders = headers;
           entry.mimeType = mimeType;
           System.out.println("request.cacheControl()==" + request.cacheControl());
           entry.data = string.getBytes(UTF8);
           cache.put(request.urlString(), entry);
         }
       }
     }
   }
 }
示例#2
0
 /**
  * Returns the number of milliseconds that the response was fresh for, starting from the served
  * date.
  */
 private long computeFreshnessLifetime() {
   CacheControl responseCaching = cacheResponse.cacheControl();
   if (responseCaching.maxAgeSeconds() != -1) {
     return SECONDS.toMillis(responseCaching.maxAgeSeconds());
   } else if (expires != null) {
     long servedMillis = servedDate != null ? servedDate.getTime() : receivedResponseMillis;
     long delta = expires.getTime() - servedMillis;
     return delta > 0 ? delta : 0;
   } else if (lastModified != null && cacheResponse.request().url().getQuery() == null) {
     // As recommended by the HTTP RFC and implemented in Firefox, the
     // max age of a document should be defaulted to 10% of the
     // document's age at the time it was served. Default expiration
     // dates aren't used for URIs containing a query.
     long servedMillis = servedDate != null ? servedDate.getTime() : sentRequestMillis;
     long delta = servedMillis - lastModified.getTime();
     return delta > 0 ? (delta / 10) : 0;
   }
   return 0;
 }
示例#3
0
    /** Returns a strategy to use assuming the request can use the network. */
    private CacheStrategy getCandidate() {
      // No cached response.
      if (cacheResponse == null) {
        return new CacheStrategy(request, null);
      }

      // Drop the cached response if it's missing a required handshake.
      if (request.isHttps() && cacheResponse.handshake() == null) {
        return new CacheStrategy(request, null);
      }

      // If this response shouldn't have been stored, it should never be used
      // as a response source. This check should be redundant as long as the
      // persistence store is well-behaved and the rules are constant.
      if (!isCacheable(cacheResponse, request)) {
        return new CacheStrategy(request, null);
      }

      CacheControl requestCaching = request.cacheControl();
      if (requestCaching.noCache() || hasConditions(request)) {
        return new CacheStrategy(request, null);
      }

      long ageMillis = cacheResponseAge();
      long freshMillis = computeFreshnessLifetime();

      if (requestCaching.maxAgeSeconds() != -1) {
        freshMillis = Math.min(freshMillis, SECONDS.toMillis(requestCaching.maxAgeSeconds()));
      }

      long minFreshMillis = 0;
      if (requestCaching.minFreshSeconds() != -1) {
        minFreshMillis = SECONDS.toMillis(requestCaching.minFreshSeconds());
      }

      long maxStaleMillis = 0;
      CacheControl responseCaching = cacheResponse.cacheControl();
      if (!responseCaching.mustRevalidate() && requestCaching.maxStaleSeconds() != -1) {
        maxStaleMillis = SECONDS.toMillis(requestCaching.maxStaleSeconds());
      }

      if (!responseCaching.noCache() && ageMillis + minFreshMillis < freshMillis + maxStaleMillis) {
        Response.Builder builder = cacheResponse.newBuilder();
        if (ageMillis + minFreshMillis >= freshMillis) {
          builder.addHeader("Warning", "110 HttpURLConnection \"Response is stale\"");
        }
        long oneDayMillis = 24 * 60 * 60 * 1000L;
        if (ageMillis > oneDayMillis && isFreshnessLifetimeHeuristic()) {
          builder.addHeader("Warning", "113 HttpURLConnection \"Heuristic expiration\"");
        }
        return new CacheStrategy(null, builder.build());
      }

      Request.Builder conditionalRequestBuilder = request.newBuilder();

      if (etag != null) {
        conditionalRequestBuilder.header("If-None-Match", etag);
      } else if (lastModified != null) {
        conditionalRequestBuilder.header("If-Modified-Since", lastModifiedString);
      } else if (servedDate != null) {
        conditionalRequestBuilder.header("If-Modified-Since", servedDateString);
      }

      Request conditionalRequest = conditionalRequestBuilder.build();
      return hasConditions(conditionalRequest)
          ? new CacheStrategy(conditionalRequest, cacheResponse)
          : new CacheStrategy(conditionalRequest, null);
    }