Disable Volley cache management

Is there a way I could disable the Volley cache management? My app is using Google Volley library to manage the transport layer, but I have my own cache manager implementation because the server does not uses Cache-Control header. I want to save the space that Volley cache is using because it is totally useless.

Is there any easy way? or should I implement my own version of RequestQueue?

Any suggestion appreciated.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

If you use any of the default Request classes implemented in volley(e.g. StringRequest, JsonRequest, etc.), then call setShouldCache(false) right before adding the request object to the volley RequestQueue:

request.setShouldCache(false);
myQueue.add(request);

If you have your own implementation of the Request class, then you can call setShouldCache(false) in the constructor of your class.

This solution disables caching for each requests individually. If you want to disable caching globally from the volley library, you can permanently set the mShouldCache variable to false in the Request class.

Solution 2

You can create your RequestQueue from constructor and pass a NoCache object as the first parameter. The second parameter is a network transport based on your choice of AndroidHttpClient or HttpURLConnection.

RequestQueue queue = new RequestQueue(new NoCache(), new BasicNetwork(new HurlStack()));

For more details see this documentation.

According to the documentation, BasicNetwork is Volley’s default network implementation.

Solution 3

request.setShouldCache(false);

does not seem to be enough for GET requests.
however, clearing the cache before adding to the queue seems to help

myRequestQueue.getCache().clear();

I put this in my getRequestQueue() method in my Volley singleton. before returning the queue.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply