13

I want to clear the request queue each 30 minutes for example.

So What is the best way to clear volley cache automatically?

Override methods by extending the volley cache class?

Or build a timer which will clear the cache every times i need?

JohnnyBeGoody
  • 253
  • 1
  • 3
  • 15
  • http://stackoverflow.com/questions/17230431/google-volley-when-to-use-cache-remove-and-cache-invalidate – IntelliJ Amiya Jun 28 '14 at 07:26
  • So, do you think i just have to invalidate the data? My question is how to invalidate or clear the data automatically, without any button for refresh.. – JohnnyBeGoody Jun 28 '14 at 07:41
  • 1
    I post my solution, maybe it could help someone. I use a class who extends TimeTask and i clear the volley cache with run method. – JohnnyBeGoody Jun 29 '14 at 09:39

3 Answers3

23

Google Volley provides 2 ways to clear an item from the Cache:

AppController.getInstance().getRequestQueue().getCache().remove(key);

and

AppController.getInstance().getRequestQueue().getCache().invalidate(key, fullExpire);

Remove means you are removing the actual cached data.

Invalidate means you are just marking the data as invalid. So volley will check with the server whether the data is still valid. The full expire determines whether to use the data before volley has validated it with the server.

To clear cache in each 30 minutes use below code:-

you can use volley's serverDate to get the date for when the response was originally received as

AppController.getInstance().getRequestQueue().getCache().get(url).serverDate

So in your code use getMinutesDifference function as

  public static long getMinutesDifference(long timeStart,long timeStop){
            long diff = timeStop - timeStart;
            long diffMinutes = diff / (60 * 1000);

            return  diffMinutes;
        }

and Call this function in your code as

Calendar calendar = Calendar.getInstance();
long serverDate = AppController.getInstance().getRequestQueue().getCache().get(url).serverDate;
if(getMinutesDifference(serverDate, calendar.getTimeInMillis()) >=30){
   AppController.getInstance().getRequestQueue().getCache().invalidate(url, true);
}

It will invalidate the cache,if previous url response >=30 minutes.

Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
  • 1
    Hello Giru Bhai, AppController.getInstance().getRequestQueue().getCache().remove(key); In above line what is "key"? I am facing volley cache issue... – patel135 Apr 21 '16 at 10:34
1

Easy way to do that is override onRequestFinished method and clear cache. Or you can run inside the timer after 30 min.

final RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);

requestQueue.addRequestFinishedListener(new RequestQueue.RequestFinishedListener<Object>() {
            @Override
            public void onRequestFinished(Request<Object> request) {
                requestQueue.getCache().clear();
            }
        });
Dinith Rukshan Kumara
  • 638
  • 2
  • 10
  • 19
0

I was trying to remove bitmap from cache by using remove(key) but it was not working, so I have check url received by putBitmap(String url, Bitmap bitmap). I found url has some prefix like #W0#H#S7http... this is because volley call getCacheKey(String url, int maxWidth, int maxHeight, ScaleType scaleType) for each url. SO if you wants to remove url from cache then you also have to call this function for getting key for url.

 String key = mImageLoader.getCacheKey(url, 0, 0, ImageView.ScaleType.CENTER_INSIDE);

 mRequestQueue.getCache().remove(key);

Pass 0,0 and ImageView.ScaleType.CENTER_INSIDE if you are using imageLoader.get(String requestUrl,ImageLoader.ImageListener listener) else pass min height and width and scale type you are using.

NOTE getCacheKey() is private function of ImageLoader class so you have to change it to public for using it inside you app.

Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67