Example usage for com.squareup.okhttp Cache Cache

List of usage examples for com.squareup.okhttp Cache Cache

Introduction

In this page you can find the example usage for com.squareup.okhttp Cache Cache.

Prototype

public Cache(File directory, long maxSize) 

Source Link

Usage

From source file:com.av.remusic.lastfmapi.RestServiceFactory.java

License:Open Source License

public static <T> T create(final Context context, String baseUrl, Class<T> clazz) {
    final OkHttpClient okHttpClient = new OkHttpClient();

    okHttpClient.setCache(new Cache(context.getApplicationContext().getCacheDir(), CACHE_SIZE));
    okHttpClient.setConnectTimeout(40, TimeUnit.SECONDS);

    RequestInterceptor interceptor = new RequestInterceptor() {
        @Override/*from  w  ww.  jav  a 2  s.c  om*/
        public void intercept(RequestFacade request) {
            //7-days cache
            request.addHeader("Cache-Control", String.format("max-age=%d,max-stale=%d",
                    Integer.valueOf(60 * 60 * 24 * 7), Integer.valueOf(31536000)));
            request.addHeader("Connection", "keep-alive");
        }
    };

    RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(baseUrl)
            .setRequestInterceptor(interceptor).setClient(new OkClient(okHttpClient));

    return builder.build().create(clazz);

}

From source file:com.baoyz.dribble.AppModule.java

License:Open Source License

static OkHttpClient createOkHttpClient(Application app) {
    OkHttpClient client = new OkHttpClient();

    try {/*w  w  w  .j a va2 s  . co m*/
        File cacheDir = new File(app.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        client.setCache(cache);
    } catch (IOException e) {
        Timber.e(e, "Unable to install disk cache.");
    }

    return client;
}

From source file:com.battlelancer.seriesguide.util.ServiceUtils.java

License:Apache License

/**
 * Returns this apps {@link com.squareup.okhttp.OkHttpClient} with enabled response cache.
 * Should be used with API calls.//from w  ww .j a  v a 2s .c om
 */
@NonNull
public static synchronized OkHttpClient getCachingOkHttpClient(Context context) {
    if (cachingHttpClient == null) {
        cachingHttpClient = new OkHttpClient();
        cachingHttpClient.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        cachingHttpClient.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        File cacheDir = createApiCacheDir(context);
        cachingHttpClient.setCache(new Cache(cacheDir, calculateApiDiskCacheSize(cacheDir)));
    }
    return cachingHttpClient;
}

From source file:com.burhan.udacity.popularmovies.data.DataModule.java

License:Apache License

static OkHttpClient createOkHttpClient(Application app) {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(10, SECONDS);
    client.setReadTimeout(10, SECONDS);//ww w  . j  a  va2  s.c  o m
    client.setWriteTimeout(10, SECONDS);

    // Install an HTTP cache in the application cache directory.
    File cacheDir = new File(app.getCacheDir(), "http");
    Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
    client.setCache(cache);

    return client;
}

From source file:com.droiddevil.myuber.data.DataModule.java

License:Apache License

static OkHttpClient createOkHttpClient(Context context) {
    OkHttpClient client = new OkHttpClient();
    File cacheDir = new File(context.getCacheDir(), "http_cache");
    try {/* ww w .ja v  a 2 s  . c o m*/
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        client.setCache(cache);
    } catch (IOException e) {
        Timber.e(e, "Could not create http cache");
    }
    return client;
}

From source file:com.enstage.wibmo.util.HttpUtil.java

License:Apache License

public static Cache createHttpClientCache(Context context) {
    File cacheDir = context.getDir("service_api_cache", Context.MODE_PRIVATE);
    return new Cache(cacheDir, HTTP_CACHE_SIZE);
}

From source file:com.ephemeraldreams.gallyshuttle.data.DataModule.java

License:Apache License

/**
 * Provide a HTTP client.//  w w  w  .j  ava 2  s.  c  o  m
 *
 * @param application Application to get cache directory from.
 * @return Cached HTTP client.
 */
@Provides
@ApplicationScope
OkHttpClient provideOkHttpClient(Application application) {
    OkHttpClient client = new OkHttpClient();
    File cacheDir = new File(application.getCacheDir(), "http");
    Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
    client.setCache(cache);
    return client;
}

From source file:com.example.app.di.modules.ApiModule.java

License:Apache License

@Provides
@Singleton/* www  . jav  a 2s  . c o  m*/
Cache provideCache(Context context) {
    File baseDir = context.getCacheDir();
    File cacheDir = new File(baseDir, "HttpResponseCache");
    return new Cache(cacheDir, CACHE_SIZE_10MB);
}

From source file:com.example.xyzreader.remote.HttpClientProvider.java

License:Apache License

private HttpClientProvider(Context context) {
    httpClient = new OkHttpClient();
    File httpCacheDir = new File(context.getCacheDir(), "http_cache");
    long httpCacheSize = 10 * 1024 * 1024; // 10 MiB
    httpClient.setCache(new Cache(httpCacheDir, httpCacheSize));
    httpClient.setReadTimeout(10, TimeUnit.SECONDS);
    httpClient.setConnectTimeout(5, TimeUnit.SECONDS);
}

From source file:com.geek.geekmall.utils.ImageLoader.java

License:Apache License

/**
 * Create avatar helper//w  w  w. jav a  2s  .c o m
 *
 * @param context
 */
private ImageLoader(final Context context) {
    this.context = context;

    OkHttpClient client = new OkHttpClient();

    // Install an HTTP cache in the application cache directory.
    File cacheDir = new File(SDCardUtils.getSDCardPath(), "geekmall");
    Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
    client.setCache(cache);
    p = new Picasso.Builder(context).downloader(new OkHttpDownloader(client)).build();
    //        p.setIndicatorsEnabled(true);
    //        p.setLoggingEnabled(true);
    bitmapUtils = new BitmapUtils(context, cacheDir.getPath(), 0.15f, DISK_CACHE_SIZE);
    mUniverImageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
            .diskCache(new UnlimitedDiskCache(cacheDir)).diskCacheSize(500 * 1024 * 1024)
            .diskCacheFileCount(500).writeDebugLogs() // Remove for release app
            .build();
    mUniverImageLoader.init(configuration);
}