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.github.baoti.pioneer.data.DataModule.java

License:Apache License

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

    // 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);//from  w  ww  . j a v  a2s  .  c  o m

    return client;
}

From source file:com.github.mobile.util.AvatarLoader.java

License:Apache License

/**
 * Create avatar helper/*w  w w .j  a  v a 2 s .  c om*/
 *
 * @param context
 */
@Inject
public AvatarLoader(final Context context) {
    this.context = context;

    OkHttpClient client = new OkHttpClient();

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

    p = new Picasso.Builder(context).downloader(new OkHttpDownloader(client)).build();

    float density = context.getResources().getDisplayMetrics().density;
    cornerRadius = CORNER_RADIUS_IN_DIP * density;

    if (avatarSize == 0) {
        avatarSize = getMaxAvatarSize(context);
    }

    // TODO remove this eventually
    // Delete the old cache
    final File avatarDir = new File(context.getCacheDir(), "avatars/github.com");
    if (avatarDir.isDirectory())
        deleteCache(avatarDir);
}

From source file:com.github.shredder121.gh_event_api.handler.AbstractHandlerTest.java

License:Apache License

private static GitHub getGitHub() throws IOException {
    OkHttpClient client = new OkHttpClient();
    client.setCache(new Cache(Paths.get(".", ".cache").toFile(), FileUtils.ONE_MB * 10));
    return new GitHubBuilder().withConnector(new RawGitOkHttpConnector(new OkUrlFactory(client))).build();
}

From source file:com.gittalent.service.GithubImportService.java

License:Apache License

public GitHub initGithub() {
    String tmpDirPath = System.getProperty("java.io.tmpdir");
    File cacheDirectoryParent = new File(tmpDirPath);
    File cacheDirectory = new File(cacheDirectoryParent, "okhttpCache");
    if (!cacheDirectory.exists()) {
        cacheDirectory.mkdir();/*w w  w.  j a v  a2s .com*/
    }
    Cache cache = new Cache(cacheDirectory, 100 * 1024 * 1024);
    try {
        return GitHubBuilder.fromCredentials().withRateLimitHandler(RateLimitHandler.WAIT)
                .withAbuseLimitHandler(AbuseLimitHandler.WAIT)
                .withConnector(new OkHttpConnector(new OkUrlFactory(new OkHttpClient().setCache(cache))))
                .build();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.guoxiaoxing.music.lastfmapi.RestServiceFactory.java

License:Open Source License

public static <T> T createStatic(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 w w .ja v a  2 s .c o m*/
        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.jaspersoft.android.jaspermobile.internal.di.modules.app.AppModule.java

License:Open Source License

@Singleton
@Named("webview_client")
@Provides/*from  w  w  w .j ava 2  s  . c  om*/
OkHttpClient provideWebViewClient(@ApplicationContext Context context) {
    OkHttpClient client = new OkHttpClient();

    File cacheDir = context.getApplicationContext().getCacheDir();
    File okCache = new File(cacheDir, "ok-cache");
    if (!okCache.exists()) {
        boolean cachedCreated = okCache.mkdirs();

        if (cachedCreated) {
            int cacheSize = 50 * 1024 * 1024;
            Cache cache = new Cache(okCache, cacheSize);
            client.setCache(cache);
        }
    }

    return client;
}

From source file:com.liferay.mobile.screens.context.LiferayServerContext.java

License:Open Source License

public static OkHttpClient getOkHttpClient() {
    synchronized (LiferayServerContext.class) {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient();
            okHttpClient.setCache(new Cache(LiferayScreensContext.getContext().getCacheDir(), MAX_SIZE));
        }//  ww  w  . ja  v  a2 s .c o  m
    }
    return okHttpClient;
}

From source file:com.linroid.pushapp.module.DataModule.java

License:Apache License

@Provides
@Singleton/*from   w  w  w.  java  2s.  c om*/
Cache provideHttpCache(@HttpCacheDir File httpCacheDir) {
    //100M;
    int cacheSize = 1024 * 1024 * 100;
    try {
        return new Cache(httpCacheDir, cacheSize);
    } catch (Exception e) {
        Timber.e("install http cache false");
    }

    return null;
}

From source file:com.markupartist.sthlmtraveling.data.misc.HttpHelper.java

License:Apache License

/**
 * Install an HTTP cache in the application cache directory.
 *//* w w w .  j  av  a 2  s.  co m*/
private void installHttpCache(final Context context) {
    // Install an HTTP cache in the application cache directory.
    try {
        // Install an HTTP cache in the application cache directory.
        File cacheDir = new File(context.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        mClient.setCache(cache);
    } catch (Throwable e) {
        Log.e("HttpHelper", "Unable to install disk cache.");
    }
}

From source file:com.miz.functions.OkHttpDownloader.java

License:Apache License

/**
 * Create new downloader that uses OkHttp. This will install an image cache into your application
 * cache directory.//w  w  w .  j av  a  2  s.com
 */
public OkHttpDownloader(Context context) {

    // Create cache directory
    mCacheDir = new File(context.getApplicationContext().getCacheDir(), "picasso-cache");
    if (!mCacheDir.exists()) {
        mCacheDir.mkdirs();
    }

    mUrlFactory = new OkUrlFactory(MizuuApplication.getOkHttpClient());

    try {
        mUrlFactory.client().setCache(new Cache(mCacheDir, calculateDiskCacheSize(mCacheDir)));
    } catch (IOException e) {
    }

    // Set context
    mContext = context;
}