本文整理了Java中okhttp3.Cache.<init>()
方法的一些代码示例,展示了Cache.<init>()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.<init>()
方法的具体详情如下:
包路径:okhttp3.Cache
类名称:Cache
方法名:<init>
[英]Create a cache of at most maxSize bytes in directory.
[中]在目录中创建最大maxSize字节数的缓存。
代码示例来源:origin: square/okhttp
public CacheResponse(File cacheDirectory) throws Exception {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(cacheDirectory, cacheSize);
client = new OkHttpClient.Builder()
.cache(cache)
.build();
}
代码示例来源:origin: square/okhttp
public static AndroidShimResponseCache create(File directory, long maxSize) throws IOException {
Cache cache = new Cache(directory, maxSize);
return new AndroidShimResponseCache(cache);
}
代码示例来源:origin: square/okhttp
public RewriteResponseCacheControl(File cacheDirectory) throws Exception {
Cache cache = new Cache(cacheDirectory, 1024 * 1024);
cache.evictAll();
client = new OkHttpClient.Builder()
.cache(cache)
.build();
}
代码示例来源:origin: square/okhttp
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println("Usage: Crawler <cache dir> <root>");
return;
}
int threadCount = 20;
long cacheByteCount = 1024L * 1024L * 100L;
Cache cache = new Cache(new File(args[0]), cacheByteCount);
OkHttpClient client = new OkHttpClient.Builder()
.cache(cache)
.build();
Crawler crawler = new Crawler(client);
crawler.queue.add(HttpUrl.get(args[1]));
crawler.parallelDrainQueue(threadCount);
}
}
代码示例来源:origin: amitshekhariitbhu/Fast-Android-Networking
public static Cache getCache(Context context, int maxCacheSize, String uniqueName) {
return new Cache(getDiskCacheDir(context, uniqueName), maxCacheSize);
}
代码示例来源:origin: Rukey7/MvpApp
/**
* 初始化网络通信服务
*/
public static void init() {
// 指定缓存路径,缓存大小100Mb
Cache cache = new Cache(new File(AndroidApplication.getContext().getCacheDir(), "HttpCache"),
1024 * 1024 * 100);
OkHttpClient okHttpClient = new OkHttpClient.Builder().cache(cache)
.retryOnConnectionFailure(true)
.addInterceptor(sLoggingInterceptor)
.addInterceptor(sRewriteCacheControlInterceptor)
.addNetworkInterceptor(sRewriteCacheControlInterceptor)
.connectTimeout(10, TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(NEWS_HOST)
.build();
sNewsService = retrofit.create(INewsApi.class);
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(WELFARE_HOST)
.build();
sWelfareService = retrofit.create(IWelfareApi.class);
}
代码示例来源:origin: north2016/T-MVP
private Api() {
HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
File cacheFile = new File(App.getAppContext().getCacheDir(), "cache");
Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(7676, TimeUnit.MILLISECONDS)
.connectTimeout(7676, TimeUnit.MILLISECONDS)
.addInterceptor(headInterceptor)
.addInterceptor(logInterceptor)
.addNetworkInterceptor(new HttpCacheInterceptor())
.cache(cache)
.build();
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").serializeNulls().create();
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(C.BASE_URL)
.build();
service = retrofit.create(ApiService.class);
}
代码示例来源:origin: jaydenxiao2016/AndroidFire
Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb
代码示例来源:origin: HotBitmapGG/bilibili-android-client
/**
* 初始化OKHttpClient,设置缓存,设置超时时间,设置打印日志,设置UA拦截器
*/
private static void initOkHttpClient() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
if (mOkHttpClient == null) {
synchronized (RetrofitHelper.class) {
if (mOkHttpClient == null) {
//设置Http缓存
Cache cache = new Cache(new File(BilibiliApp.getInstance().getCacheDir(), "HttpCache"), 1024 * 1024 * 10);
mOkHttpClient = new OkHttpClient.Builder()
.cache(cache)
.addInterceptor(interceptor)
.addNetworkInterceptor(new CacheInterceptor())
.addNetworkInterceptor(new StethoInterceptor())
.retryOnConnectionFailure(true)
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.addInterceptor(new UserAgentInterceptor())
.build();
}
}
}
}
代码示例来源:origin: iMeiji/Toutiao
if (retrofit == null) {
Cache cache = new Cache(new File(InitApp.AppContext.getCacheDir(), "HttpCache"),
1024 * 1024 * 50);
代码示例来源:origin: GitLqr/LQRWeChat
public BaseApiRetrofit() {
/*================== common ==================*/
// Log信息拦截器
// HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
// loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);//这里可以选择拦截级别
//cache
File httpCacheDir = new File(MyApp.getContext().getCacheDir(), "response");
int cacheSize = 10 * 1024 * 1024;// 10 MiB
Cache cache = new Cache(httpCacheDir, cacheSize);
//cookie
ClearableCookieJar cookieJar =
new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(MyApp.getContext()));
//OkHttpClient
mClient = new OkHttpClient.Builder()
.addInterceptor(REWRITE_HEADER_CONTROL_INTERCEPTOR)
.addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
.addInterceptor(new LoggingInterceptor())
// .addInterceptor(loggingInterceptor)//设置 Debug Log 模式
.cache(cache)
.cookieJar(cookieJar)
.build();
}
代码示例来源:origin: hidroh/materialistic
.cache(new Cache(context.getApplicationContext().getCacheDir(), CACHE_SIZE))
.addNetworkInterceptor(new CacheOverrideNetworkInterceptor())
.addInterceptor(new ConnectionAwareInterceptor(context))
代码示例来源:origin: square/picasso
/** Create the {@link Picasso} instance. */
@NonNull
public Picasso build() {
Context context = this.context;
okhttp3.Cache unsharedCache = null;
if (callFactory == null) {
File cacheDir = createDefaultCacheDir(context);
long maxSize = calculateDiskCacheSize(cacheDir);
unsharedCache = new okhttp3.Cache(cacheDir, maxSize);
callFactory = new OkHttpClient.Builder()
.cache(unsharedCache)
.build();
}
if (cache == null) {
cache = new PlatformLruCache(Utils.calculateMemoryCacheSize(context));
}
if (service == null) {
service = new PicassoExecutorService(new PicassoThreadFactory());
}
Stats stats = new Stats(cache);
Dispatcher dispatcher = new Dispatcher(context, service, HANDLER, cache, stats);
return new Picasso(context, dispatcher, callFactory, unsharedCache, cache, listener,
requestTransformers, requestHandlers, stats, defaultBitmapConfig, indicatorsEnabled,
loggingEnabled);
}
}
代码示例来源:origin: square/picasso
@Test public void shutdownClosesUnsharedCache() {
okhttp3.Cache cache = new okhttp3.Cache(temporaryFolder.getRoot(), 100);
Picasso picasso =
new Picasso(context, dispatcher, UNUSED_CALL_FACTORY, cache, this.cache, listener,
NO_TRANSFORMERS, NO_HANDLERS, stats, ARGB_8888, false, false);
picasso.shutdown();
assertThat(cache.isClosed()).isTrue();
}
代码示例来源:origin: apache/nifi
if(etagEnabled) {
final int maxCacheSizeBytes = context.getProperty(PROP_ETAG_MAX_CACHE_SIZE).asDataSize(DataUnit.B).intValue();
okHttpClientBuilder.cache(new Cache(getETagCacheDir(), maxCacheSizeBytes));
代码示例来源:origin: JakeWharton/picasso2-okhttp3-downloader
private static OkHttpClient createOkHttpClient(File cacheDir, long maxSize) {
return new OkHttpClient.Builder()
.cache(new Cache(cacheDir, maxSize))
.build();
}
代码示例来源:origin: kaku2015/ColorfulNews
private OkHttpClient getOkHttpClient() {
if (sOkHttpClient == null) {
synchronized (RetrofitManager.class) {
Cache cache = new Cache(new File(App.getAppContext().getCacheDir(), "HttpCache"),
1024 * 1024 * 100);
if (sOkHttpClient == null) {
sOkHttpClient = new OkHttpClient.Builder().cache(cache)
.connectTimeout(6, TimeUnit.SECONDS)
.readTimeout(6, TimeUnit.SECONDS)
.writeTimeout(6, TimeUnit.SECONDS)
.addInterceptor(mRewriteCacheControlInterceptor)
.addNetworkInterceptor(mRewriteCacheControlInterceptor)
.addInterceptor(mLoggingInterceptor).build();
}
}
}
return sOkHttpClient;
}
代码示例来源:origin: palaima/DebugDrawer
private static OkHttpClient.Builder createOkHttpClientBuilder(Application app) {
// Install an HTTP cache in the application cache directory.
File cacheDir = new File(app.getCacheDir(), "okhttp3");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
return new OkHttpClient.Builder()
.cache(cache)
.addInterceptor(LogsModule.chuckInterceptor(app))
.addInterceptor(NetworkQualityModule.interceptor(app))
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS);
}
}
代码示例来源:origin: palaima/DebugDrawer
private static OkHttpClient.Builder createOkHttpClientBuilder(Application app) {
// Install an HTTP cache in the application cache directory.
File cacheDir = new File(app.getCacheDir(), "okhttp3-cache");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
return new OkHttpClient.Builder()
.cache(cache)
.addInterceptor(LogsModule.chuckInterceptor(app))
.addInterceptor(NetworkQualityModule.interceptor(app))
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS);
}
}
代码示例来源:origin: JakeWharton/picasso2-okhttp3-downloader
/**
* Creates a {@link Cache} that would have otherwise been created by calling
* {@link #OkHttp3Downloader(Context)}. This allows you to build your own {@link OkHttpClient}
* while still getting the default disk cache.
*/
public static Cache createDefaultCache(Context context) {
File dir = defaultCacheDir(context);
return new Cache(dir, calculateDiskCacheSize(dir));
}
内容来源于网络,如有侵权,请联系作者删除!