@Test public void closeRemovesReference() throws Exception { HttpResponseCache cache = HttpResponseCache.install(File.createTempFile("foo", "bar"), 42); cache.close(); assertThat(HttpResponseCache.getInstalled()).isNull(); } }
@Test public void deleteRemovesReference() throws Exception { HttpResponseCache cache = HttpResponseCache.install(File.createTempFile("foo", "bar"), 42); cache.delete(); assertThat(HttpResponseCache.getInstalled()).isNull(); }
@Test public void countsStartAtZero() throws Exception { HttpResponseCache cache = HttpResponseCache.install(File.createTempFile("foo", "bar"), 42); assertThat(cache.getHitCount()).isEqualTo(0); assertThat(cache.getNetworkCount()).isEqualTo(0); assertThat(cache.getRequestCount()).isEqualTo(0); }
@Test public void installedCacheIsReturned() throws Exception { assertThat(HttpResponseCache.getInstalled()).isNull(); HttpResponseCache cache = HttpResponseCache.install(File.createTempFile("foo", "bar"), 42); HttpResponseCache installed = HttpResponseCache.getInstalled(); assertThat(installed).isSameAs(cache); assertThat(installed.maxSize()).isEqualTo(42); }
@Override public void onCreate() { super.onCreate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { final File dir = new File(getCacheDir(), "http"); if (!dir.exists()) { dir.mkdirs(); } try { final StatFs stat = new StatFs(dir.getAbsolutePath()); final long blocks = stat.getBlockCount(); final long blockSize = stat.getBlockSize(); this.cache = HttpResponseCache.install(dir, blocks * blockSize); logger.debug("Install HTTP response cache at %s", dir.getAbsolutePath()); } catch (final Exception e) { logger.error("Install HTTP response cache error", e); } } }
@Override protected void onStop() { CookieManager.getInstance().flush(); unregisterReceiver(mUrlResolvedReceiver); HttpResponseCache cache = HttpResponseCache.getInstalled(); if (cache != null) { cache.flush(); } super.onStop(); }
File cacheDir = new File(System.getProperty("java.io.tmpdir"), "okhttp-cache"); cache = new HttpResponseCache(cacheDir, 10L * 1024 * 1024); okHttpClient = new OkHttpClient(); okHttpClient.setResponseCache(cache);
@Override public void onTerminate() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { if (null != this.cache) { try { ((HttpResponseCache) this.cache).close(); } catch (final IOException e) { logger.error("Close HTTP response cache error", e); } } } super.onTerminate(); }
File httpCacheDir = new File(getCacheDir(), "suggestion_responses"); long httpCacheSize = 1024 * 1024; // 1 MiB HttpResponseCache.install(httpCacheDir, httpCacheSize); } catch (IOException e) { Log.i(TAG, "HTTP response cache installation failed:" + e);
// Obtain the external cache directory File cacheDir = context.getExternalCacheDir(); if (cacheDir == null) { // Fall back to using the internal cache directory cacheDir = context().getCacheDir(); } // Create a response cache using the cache directory and size restriction HttpResponseCache responseCache = new HttpResponseCache( cacheDir, 10 * 1024 * 1024); // Prepare OkHttp httpClient = new OkHttpClient(); httpClient.setResponseCache(responseCache); // Build Picasso with this custom Downloader new Picasso.Builder(getContext()) .downloader(new OkHttpDownloader(httpClient)) .build();
File httpCacheDirectory = new File(application.getApplicationContext() .getCacheDir().getAbsolutePath(), "HttpCache"); HttpResponseCache httpResponseCache = null; try { httpResponseCache = new HttpResponseCache(httpCacheDirectory, 10 * 1024); } catch (IOException e) { Log.e(getClass().getSimpleName(), "Could not create http cache", e); } OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setResponseCache(httpResponseCache); builder.setClient(new OkClient(okHttpClient));
// Create an HTTP client that uses a cache on the file system. Android applications should use // their Context to get a cache directory. OkHttpClient okHttpClient = new OkHttpClient(); File cacheDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); HttpResponseCache cache = new HttpResponseCache(cacheDir, 1024); okHttpClient.setResponseCache(cache);