Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure the Http Cache when using Volley with OkHttp?

I want to try Volley combining with OkHttp but Volley cache system and OkHttp both rely on the HTTP cache as defined in the HTTP specification. So how can be disabled the cache of OkHttp for keeping one copy of HTTP cache?

EDIT: what I have done

public class VolleyUtil {
    // http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/
    private volatile static RequestQueue sRequestQueue;

    /** get the single instance of RequestQueue **/
    public static RequestQueue getQueue(Context context) {
        if (sRequestQueue == null) {
            synchronized (VolleyUtil.class) {
                if (sRequestQueue == null) {
                    OkHttpClient client = new OkHttpClient();
                    client.networkInterceptors().add(new StethoInterceptor());
                    client.setCache(null);
                    sRequestQueue = Volley.newRequestQueue(context.getApplicationContext(), new OkHttpStack(client));
                    VolleyLog.DEBUG = true;
                }
            }
        }
        return sRequestQueue;
    }
}

Which OkHttpClient is referenced from https://gist.github.com/bryanstern/4e8f1cb5a8e14c202750

like image 998
Xiaozou Avatar asked May 04 '15 07:05

Xiaozou


2 Answers

OkHttp is a kind of HTTP client like HttpUrlConnection which implements HTTP cache, we can disable the cache of OkHttp like below:

OkHttpClient client = new OkHttpClient();
client.setCache(null);

Then, we can keep one copy of HTTP cache maintained by Volley.

IMPROVED:

I'd like to try to answer Sotti's questions.

1 I would like to know what is a good cache setup when using Volley and OkHttp.

In my project, i'm using one Volley requestQueue instance across all of restful APIs, and OkHttp worked as the transport layer for Volley like below.

public class VolleyUtil {
// http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/
private volatile static RequestQueue sRequestQueue;

/** get the single instance of RequestQueue **/
public static RequestQueue getQueue(Context context) {
    if (sRequestQueue == null) {
        synchronized (VolleyUtil.class) {
            if (sRequestQueue == null) {
                OkHttpClient client = new OkHttpClient();
                client.setCache(null);
                sRequestQueue = Volley.newRequestQueue(context.getApplicationContext(), new OkHttpStack(client));
                VolleyLog.DEBUG = true;
            }
        }
    }
    return sRequestQueue;
}}

2 Should we rely on Volley or on the OkHttp cache?

Yes, i'm using Volley cache for my HTTP Cache instead of OkHttp Cache; It works great for me.

3 What's the default behaviour out of the box?

For Volley:

it will create a "volley" default cache directory for you automatically.

/** Default on-disk cache directory. */
private static final String DEFAULT_CACHE_DIR = "volley";


public static RequestQueue newRequestQueue(Context context, HttpStack stack, int maxDiskCacheBytes) {
    File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
    ……
}

For OkHttp:

i can't find the default cache in the source code, and we can set the response cache like this post http://blog.denevell.org/android-okhttp-retrofit-using-cache.html

4. What's the recommended behaviour and how to achieve it?

As this post says:

Volley takes care of requesting, loading, caching, threading, synchronization and more. It’s ready to deal with JSON, images, caching, raw text and allow some customization.

I prefer to using Volley HTTP Cache because of the ease of customization.

For example, we can have much more control on the cache like this Android Volley + JSONObjectRequest Caching.

like image 51
Xiaozou Avatar answered Oct 16 '22 23:10

Xiaozou


The graceful way for OkHttp to ignore caches is:

request.setCacheControl(CacheControl.FORCE_NETWORK);
like image 39
SilentKnight Avatar answered Oct 17 '22 01:10

SilentKnight