34

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.

AADProgramming
  • 6,077
  • 11
  • 38
  • 58
Juampa
  • 2,035
  • 2
  • 25
  • 35
  • If you don't mind me asking but what alternative to volley cache do you have implemented? Are you using SQLLite or an AndroidORM? Maybe you are just writing them to your own files? Thanks :) – Unknownweirdo Feb 23 '16 at 16:54

3 Answers3

84

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.

Hungry Coder
  • 1,800
  • 17
  • 22
  • 1
    I added it directly to my custom request. Thanks. – Juampa Aug 15 '14 at 20:47
  • 1
    When you say disabling it globally do you mean have the source code of volley as part of your project and set mShouldCache to false? or by some other means? – TypingPanda Jun 18 '15 at 14:22
  • @TypingPanda I meant, changing the variable in the volley library. The reason for that is: you may have your own multiple implementations of the Request class. If you want to disable caching for all of them, it would be less time consuming to do that. Otherwise, if you don't want to change anything in the ibrary, you can call setShouldCache(false) in each of your implementations. – Hungry Coder Jul 10 '15 at 03:26
  • Hi, i tried to call setShouldCache in the constructor of my custom request class but i don't really see how to do it, can someone help me plase ? – thib sig Apr 05 '17 at 16:01
  • Thanks it saved a lot of time. – Randhir Kumar Jul 03 '17 at 15:11
14

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.

computingfreak
  • 4,939
  • 1
  • 34
  • 51
Anor
  • 1,110
  • 13
  • 14
9
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.

Versa
  • 605
  • 7
  • 11
  • Never had a issue like this. thanks for this answer. Everything was getting cached, clearing the cache before returning the queue helped. – Abir Hasan Shawon Jul 11 '16 at 06:41