List of usage examples for com.squareup.okhttp Cache Cache
public Cache(File directory, long maxSize)
From source file:com.miz.mizuu.MizuuApplication.java
License:Apache License
/** * OkHttpClient singleton with 2 MB cache. * @return/*from ww w .jav a 2s .c om*/ */ public static OkHttpClient getOkHttpClient() { if (mOkHttpClient == null) { mOkHttpClient = new OkHttpClient(); File cacheDir = getContext().getCacheDir(); Cache cache = new Cache(cacheDir, 2 * 1024 * 1024); mOkHttpClient.setCache(cache); } return mOkHttpClient; }
From source file:com.nabilhachicha.kc.di.DataModule.java
License:Apache License
static OkHttpClient createOkHttpClient(Application app) { OkHttpClient client = new OkHttpClient(); client.setConnectTimeout(SOCKET_TIMEOUT, TimeUnit.SECONDS); client.setReadTimeout(SOCKET_TIMEOUT, TimeUnit.SECONDS); // Install an HTTP cache in the application cache directory. try {/*from w w w . ja v a 2s.c o m*/ File cacheDir = new File(app.getCacheDir(), "http"); Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE); client.setCache(cache); } catch (IOException e) { e.printStackTrace(); } return client; }
From source file:com.nbonnec.mediaseb.di.modules.ApiModule.java
License:Apache License
private static OkHttpClient createOkHttpClient(Application app) { OkHttpClient client = new OkHttpClient(); CookieManager cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); client.setCookieHandler(cookieManager); client.networkInterceptors().add(new UserAgentInterceptor(USER_AGENT)); try {//from w ww. j ava 2s . c o m File cacheDir = new File(app.getCacheDir(), "http"); Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE); client.setCache(cache); } catch (NullPointerException e) { Log.e(TAG, "Unable to initialize OkHttpclient with disk cache", e); } return client; }
From source file:com.nizlumina.utils.WebUnitMaster.java
License:Open Source License
private WebUnitMaster() { mainClient = new OkHttpClient(); int cacheSize = 10 * 1024 * 1024; //10 MB try {/* ww w . j a v a 2 s . c o m*/ mCache = new Cache(new File(cacheLocation), cacheSize); mainClient.setCache(mCache); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.pacoworks.dereference.dependencies.modules.NetworkModule.java
License:Open Source License
@Provides
@Singleton
Cache provideCache() {
return new Cache(cacheDir, cacheSize);
}
From source file:com.raskasa.dropwizard.okhttp.OkHttpClientBuilder.java
License:Apache License
/** Builds the {@link OkHttpClient}. */ public OkHttpClient build() { final OkHttpClient rawClient = new OkHttpClient(); if (configuration.getConnectTimeout() > 0L) { rawClient.setConnectTimeout(configuration.getConnectTimeout(), TimeUnit.MILLISECONDS); }//w w w .j a va 2s . c o m if (configuration.getReadTimeout() > 0L) { rawClient.setReadTimeout(configuration.getReadTimeout(), TimeUnit.MILLISECONDS); } if (configuration.getWriteTimeout() > 0L) { rawClient.setWriteTimeout(configuration.getWriteTimeout(), TimeUnit.MILLISECONDS); } if (configuration.getCacheDir() != null && configuration.getCacheSize() > 0L) { rawClient.setCache(new Cache(configuration.getCacheDir(), configuration.getCacheSize())); } final OkHttpClient client = InstrumentedOkHttpClients.create(registry, rawClient, name); // If the environment is present, we tie the client with the server lifecycle if (environment != null) { environment.lifecycle().manage(new Managed() { @Override public void start() throws Exception { } @Override public void stop() throws Exception { client.getConnectionPool().evictAll(); } }); } return client; }
From source file:com.raskasa.metrics.okhttp.InstrumentedOkHttpClientTest.java
License:Apache License
@Test public void httpCacheIsInstrumented() throws Exception { MockResponse mockResponse = new MockResponse().addHeader("Cache-Control:public, max-age=31536000") .addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS)) .addHeader("Expires: " + formatDate(1, TimeUnit.HOURS)).setBody("one"); server.enqueue(mockResponse);/* ww w .ja va 2s. c o m*/ HttpUrl baseUrl = server.url("/"); Cache cache = new Cache(cacheRule.getRoot(), Long.MAX_VALUE); rawClient.setCache(cache); InstrumentedOkHttpClient client = new InstrumentedOkHttpClient(registry, rawClient, null); assertThat(registry.getGauges().get(client.metricId("cache-max-size")).getValue()) .isEqualTo(Long.MAX_VALUE); assertThat(registry.getGauges().get(client.metricId("cache-current-size")).getValue()).isEqualTo(0L); Request request = new Request.Builder().url(baseUrl).build(); Response response = client.newCall(request).execute(); assertThat(registry.getGauges().get(client.metricId("cache-current-size")).getValue()) .isEqualTo(rawClient.getCache().getSize()); response.body().close(); }
From source file:com.raskasa.oksolr.SolrClient.java
License:Apache License
public void installCache() { File cacheDir = new File(System.getProperty("user.home"), "oksolr/http"); Cache cache = new Cache(cacheDir, 50 * 1024 * 1024); client.setCache(cache);//from ww w . jav a2 s.c o m }
From source file:com.shahul3d.indiasatelliteweather.service.DownloaderService.java
License:Open Source License
void initializeHTTPClient() { httpClient = new OkHttpClient(); try {//w ww . j a v a2 s. c om int cacheSize = 10 * 1024 * 1024; // 10 MiB Cache responseCache = new Cache(getApplicationContext().getCacheDir(), cacheSize); httpClient.setCache(responseCache); } catch (Exception e) { trackException("Can't set HTTP cache", e); } httpClient.setReadTimeout(90, TimeUnit.SECONDS); httpClient.setConnectTimeout(30, TimeUnit.SECONDS); }
From source file:com.squareup.picasso.OkHttp3DownloaderTest.java
License:Apache License
@Test public void shutdownClosesCache() throws Exception { OkHttpClient client = new OkHttpClient(); Cache cache = new Cache(temporaryFolder.getRoot(), 100); client.setCache(cache);//from w w w. ja v a 2s .c om new OkHttpDownloader(client).shutdown(); assertThat(cache.isClosed()).isTrue(); }