Java 类com.squareup.okhttp.internal.InternalCache 实例源码

项目:spdymcsclient    文件:HttpEngine.java   
private void maybeCache() throws IOException {
  InternalCache responseCache = Internal.instance.internalCache(client);
  if (responseCache == null) return;

  // 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;
  }

  // Offer this request to the cache.
  storeRequest = responseCache.put(stripBody(userResponse));
}
项目:boohee_v5.6    文件:HttpEngine.java   
private void maybeCache() throws IOException {
    InternalCache responseCache = Internal.instance.internalCache(this.client);
    if (responseCache != null) {
        if (CacheStrategy.isCacheable(this.userResponse, this.networkRequest)) {
            this.storeRequest = responseCache.put(stripBody(this.userResponse));
        } else if (HttpMethod.invalidatesCache(this.networkRequest.method())) {
            try {
                responseCache.remove(this.networkRequest);
            } catch (IOException e) {
            }
        }
    }
}
项目:FMTech    文件:OkHttpClient.java   
OkHttpClient(OkHttpClient paramOkHttpClient)
{
  this.routeDatabase = paramOkHttpClient.routeDatabase;
  this.dispatcher = paramOkHttpClient.dispatcher;
  this.proxy = paramOkHttpClient.proxy;
  this.protocols = paramOkHttpClient.protocols;
  this.connectionSpecs = paramOkHttpClient.connectionSpecs;
  this.interceptors.addAll(paramOkHttpClient.interceptors);
  this.networkInterceptors.addAll(paramOkHttpClient.networkInterceptors);
  this.proxySelector = paramOkHttpClient.proxySelector;
  this.cookieHandler = paramOkHttpClient.cookieHandler;
  this.cache = paramOkHttpClient.cache;
  if (this.cache != null) {}
  for (InternalCache localInternalCache = this.cache.internalCache;; localInternalCache = paramOkHttpClient.internalCache)
  {
    this.internalCache = localInternalCache;
    this.socketFactory = paramOkHttpClient.socketFactory;
    this.sslSocketFactory = paramOkHttpClient.sslSocketFactory;
    this.hostnameVerifier = paramOkHttpClient.hostnameVerifier;
    this.certificatePinner = paramOkHttpClient.certificatePinner;
    this.authenticator = paramOkHttpClient.authenticator;
    this.connectionPool = paramOkHttpClient.connectionPool;
    this.network = paramOkHttpClient.network;
    this.followSslRedirects = paramOkHttpClient.followSslRedirects;
    this.followRedirects = paramOkHttpClient.followRedirects;
    this.retryOnConnectionFailure = paramOkHttpClient.retryOnConnectionFailure;
    this.connectTimeout = paramOkHttpClient.connectTimeout;
    this.readTimeout = paramOkHttpClient.readTimeout;
    this.writeTimeout = paramOkHttpClient.writeTimeout;
    return;
  }
}
项目:boohee_v5.6    文件:OkHttpClient.java   
public void setCache(OkHttpClient client, InternalCache internalCache) {
    client.setInternalCache(internalCache);
}
项目:boohee_v5.6    文件:OkHttpClient.java   
public InternalCache internalCache(OkHttpClient client) {
    return client.internalCache();
}
项目:boohee_v5.6    文件:OkHttpClient.java   
void setInternalCache(InternalCache internalCache) {
    this.internalCache = internalCache;
    this.cache = null;
}
项目:boohee_v5.6    文件:OkHttpClient.java   
InternalCache internalCache() {
    return this.internalCache;
}
项目:boohee_v5.6    文件:HttpEngine.java   
public void sendRequest() throws RequestException, RouteException, IOException {
    if (this.cacheStrategy == null) {
        if (this.httpStream != null) {
            throw new IllegalStateException();
        }
        Request request = networkRequest(this.userRequest);
        InternalCache responseCache = Internal.instance.internalCache(this.client);
        Response cacheCandidate = responseCache != null ? responseCache.get(request) : null;
        this.cacheStrategy = new Factory(System.currentTimeMillis(), request, cacheCandidate)
                .get();
        this.networkRequest = this.cacheStrategy.networkRequest;
        this.cacheResponse = this.cacheStrategy.cacheResponse;
        if (responseCache != null) {
            responseCache.trackResponse(this.cacheStrategy);
        }
        if (cacheCandidate != null && this.cacheResponse == null) {
            Util.closeQuietly(cacheCandidate.body());
        }
        if (this.networkRequest != null) {
            this.httpStream = connect();
            this.httpStream.setHttpEngine(this);
            if (this.callerWritesRequestBody && permitsRequestBody(this.networkRequest) &&
                    this.requestBodyOut == null) {
                long contentLength = OkHeaders.contentLength(request);
                if (!this.bufferRequestBody) {
                    this.httpStream.writeRequestHeaders(this.networkRequest);
                    this.requestBodyOut = this.httpStream.createRequestBody(this
                            .networkRequest, contentLength);
                    return;
                } else if (contentLength > 2147483647L) {
                    throw new IllegalStateException("Use setFixedLengthStreamingMode() or " +
                            "setChunkedStreamingMode() for requests larger than 2 GiB.");
                } else if (contentLength != -1) {
                    this.httpStream.writeRequestHeaders(this.networkRequest);
                    this.requestBodyOut = new RetryableSink((int) contentLength);
                    return;
                } else {
                    this.requestBodyOut = new RetryableSink();
                    return;
                }
            }
            return;
        }
        if (this.cacheResponse != null) {
            this.userResponse = this.cacheResponse.newBuilder().request(this.userRequest)
                    .priorResponse(stripBody(this.priorResponse)).cacheResponse(stripBody
                            (this.cacheResponse)).build();
        } else {
            this.userResponse = new Builder().request(this.userRequest).priorResponse
                    (stripBody(this.priorResponse)).protocol(Protocol.HTTP_1_1).code(504)
                    .message("Unsatisfiable Request (only-if-cached)").body(EMPTY_BODY).build();
        }
        this.userResponse = unzip(this.userResponse);
    }
}
项目:boohee_v5.6    文件:HttpEngine.java   
public void readResponse() throws IOException {
    if (this.userResponse == null) {
        if (this.networkRequest == null && this.cacheResponse == null) {
            throw new IllegalStateException("call sendRequest() first!");
        } else if (this.networkRequest != null) {
            Response networkResponse;
            if (this.forWebSocket) {
                this.httpStream.writeRequestHeaders(this.networkRequest);
                networkResponse = readNetworkResponse();
            } else if (this.callerWritesRequestBody) {
                if (this.bufferedRequestBody != null && this.bufferedRequestBody.buffer()
                        .size() > 0) {
                    this.bufferedRequestBody.emit();
                }
                if (this.sentRequestMillis == -1) {
                    if (OkHeaders.contentLength(this.networkRequest) == -1 && (this
                            .requestBodyOut instanceof RetryableSink)) {
                        this.networkRequest = this.networkRequest.newBuilder().header
                                ("Content-Length", Long.toString(((RetryableSink) this
                                        .requestBodyOut).contentLength())).build();
                    }
                    this.httpStream.writeRequestHeaders(this.networkRequest);
                }
                if (this.requestBodyOut != null) {
                    if (this.bufferedRequestBody != null) {
                        this.bufferedRequestBody.close();
                    } else {
                        this.requestBodyOut.close();
                    }
                    if (this.requestBodyOut instanceof RetryableSink) {
                        this.httpStream.writeRequestBody((RetryableSink) this.requestBodyOut);
                    }
                }
                networkResponse = readNetworkResponse();
            } else {
                networkResponse = new NetworkInterceptorChain(this, 0, this.networkRequest)
                        .proceed(this.networkRequest);
            }
            receiveHeaders(networkResponse.headers());
            if (this.cacheResponse != null) {
                if (validate(this.cacheResponse, networkResponse)) {
                    this.userResponse = this.cacheResponse.newBuilder().request(this
                            .userRequest).priorResponse(stripBody(this.priorResponse))
                            .headers(combine(this.cacheResponse.headers(), networkResponse
                                    .headers())).cacheResponse(stripBody(this.cacheResponse))
                            .networkResponse(stripBody(networkResponse)).build();
                    networkResponse.body().close();
                    releaseStreamAllocation();
                    InternalCache responseCache = Internal.instance.internalCache(this.client);
                    responseCache.trackConditionalCacheHit();
                    responseCache.update(this.cacheResponse, stripBody(this.userResponse));
                    this.userResponse = unzip(this.userResponse);
                    return;
                }
                Util.closeQuietly(this.cacheResponse.body());
            }
            this.userResponse = networkResponse.newBuilder().request(this.userRequest)
                    .priorResponse(stripBody(this.priorResponse)).cacheResponse(stripBody
                            (this.cacheResponse)).networkResponse(stripBody(networkResponse))
                    .build();
            if (hasBody(this.userResponse)) {
                maybeCache();
                this.userResponse = unzip(cacheWritingResponse(this.storeRequest, this
                        .userResponse));
            }
        }
    }
}
项目:spdymcsclient    文件:OkUrlFactory.java   
ResponseCache getResponseCache() {
  InternalCache cache = client.internalCache();
  return cache instanceof CacheAdapter ? ((CacheAdapter) cache).getDelegate() : null;
}
项目:spdymcsclient    文件:OkHttpClient.java   
@Override public void setCache(OkHttpClient client, InternalCache internalCache) {
  client.setInternalCache(internalCache);
}
项目:spdymcsclient    文件:OkHttpClient.java   
@Override public InternalCache internalCache(OkHttpClient client) {
  return client.internalCache();
}
项目:spdymcsclient    文件:OkHttpClient.java   
/** Sets the response cache to be used to read and write cached responses. */
OkHttpClient setInternalCache(InternalCache internalCache) {
  this.internalCache = internalCache;
  this.cache = null;
  return this;
}
项目:spdymcsclient    文件:OkHttpClient.java   
InternalCache internalCache() {
  return internalCache;
}
项目:spdymcsclient    文件:HttpEngine.java   
/**
 * Figures out what the response source will be, and opens a socket to that
 * source if necessary. Prepares the request headers and gets ready to start
 * writing the request body if it exists.
 */
public void sendRequest() throws IOException {
  if (cacheStrategy != null) return; // Already sent.
  if (transport != null) throw new IllegalStateException();

  Request request = networkRequest(userRequest);

  InternalCache responseCache = Internal.instance.internalCache(client);
  Response cacheCandidate = responseCache != null
      ? responseCache.get(request)
      : null;

  long now = System.currentTimeMillis();
  cacheStrategy = new CacheStrategy.Factory(now, request, cacheCandidate).get();
  networkRequest = cacheStrategy.networkRequest;
  cacheResponse = cacheStrategy.cacheResponse;

  if (responseCache != null) {
    responseCache.trackResponse(cacheStrategy);
  }

  if (cacheCandidate != null && cacheResponse == null) {
    closeQuietly(cacheCandidate.body()); // The cache candidate wasn't applicable. Close it.
  }

  if (networkRequest != null) {
    // Open a connection unless we inherited one from a redirect.
    if (connection == null) {
      connect(networkRequest);
    }

    // Blow up if we aren't the current owner of the connection.
    if (Internal.instance.getOwner(connection) != this && !Internal.instance.isSpdy(connection)) {
      throw new AssertionError();
    }

    transport = Internal.instance.newTransport(connection, this);

    // Create a request body if we don't have one already. We'll already have
    // one if we're retrying a failed POST.
    if (hasRequestBody() && requestBodyOut == null) {
      requestBodyOut = transport.createRequestBody(request);
    }

  } else {
    // We aren't using the network. Recycle a connection we may have inherited from a redirect.
    if (connection != null) {
      Internal.instance.recycle(client.getConnectionPool(), connection);
      connection = null;
    }

    if (cacheResponse != null) {
      // We have a valid cached response. Promote it to the user response immediately.
      this.userResponse = cacheResponse.newBuilder()
          .request(userRequest)
          .priorResponse(stripBody(priorResponse))
          .cacheResponse(stripBody(cacheResponse))
          .build();
    } else {
      // We're forbidden from using the network, and the cache is insufficient.
      this.userResponse = new Response.Builder()
          .request(userRequest)
          .priorResponse(stripBody(priorResponse))
          .protocol(Protocol.HTTP_1_1)
          .code(504)
          .message("Unsatisfiable Request (only-if-cached)")
          .body(EMPTY_BODY)
          .build();
    }

    if (userResponse.body() != null) {
      initContentStream(userResponse.body().source());
    }
  }
}