示例#1
0
  public CacheResponse(File cacheDirectory) throws Exception {
    int cacheSize = 10 * 1024 * 1024; // 10 MiB
    Cache cache = new Cache(cacheDirectory, cacheSize);

    client = new OkHttpClient();
    client.setCache(cache);
  }
示例#2
0
 /** Sets the response cache to be used to read and write cached responses. */
 public static void setResponseCache(OkUrlFactory okUrlFactory, ResponseCache responseCache) {
   OkHttpClient client = okUrlFactory.client();
   if (responseCache instanceof OkCacheContainer) {
     // Avoid adding layers of wrappers. Rather than wrap the ResponseCache in yet another layer to
     // make the ResponseCache look like an InternalCache, we can unwrap the Cache instead.
     // This means that Cache stats will be correctly updated.
     OkCacheContainer okCacheContainer = (OkCacheContainer) responseCache;
     client.setCache(okCacheContainer.getCache());
   } else {
     client.setInternalCache(responseCache != null ? new CacheAdapter(responseCache) : null);
   }
 }