1

I want to enable cache in my android app, so for this I asked my web team to do this https://stackoverflow.com/a/16809399/1741671

This is OK. I have one question that if cache control is allow and expire time is not expired and before that expire time if I hit same web service which I have already hit and cache is allow and expire time is also not expired then volley will hit a web server or it will send response from cache to onResponse.

@Override
public void onResponse(String response) {
    result=response;
    System.out.println(response);
}
Community
  • 1
  • 1
N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • IMO, you can read its logic from this link `https://android.googlesource.com/platform/frameworks/volley/+/master/src/main/java/com/android/volley/CacheDispatcher.java` (from the line#79) – BNK Mar 07 '16 at 09:17
  • @BNK how to get cache data or volley will return cache data in onResponse callback ?? – N Sharma Mar 07 '16 at 12:19
  • Sorry, can you clarify more or post some more codes. I have not well understand your issue yet – BNK Mar 07 '16 at 14:21
  • I mean if volley send me cache response on calling web service then in which method i will get response. Generally volley return `onResponse` method but what about cached response – N Sharma Mar 07 '16 at 15:13
  • @BNK can you please show me this by source code of volley ?? – N Sharma Mar 08 '16 at 12:58
  • You can see in the java file I commented above – BNK Mar 08 '16 at 13:24
  • Or by running a sample project with cached response – BNK Mar 08 '16 at 13:29
  • @BNK If parameter value of same web services is different then will it send response me to from sever or cached.. ?? can we do caching on basis of request params. if same then return cached otherwise hit to server to get fresh data. – N Sharma Mar 09 '16 at 06:19
  • You can find more inside `CacheDispatcher.java` I commented above. Pay attention to `// Attempt to retrieve this item from cache. Cache.Entry entry = mCache.get(request.getCacheKey()); if (entry == null) { request.addMarker("cache-miss"); // Cache miss; send off to the network dispatcher. mNetworkQueue.put(request); continue; }` – BNK Mar 09 '16 at 06:36
  • then `request.getCacheKey()` you will find `/** * Returns the cache key for this request. By default, this is the URL. */ public String getCacheKey() { return getUrl(); }` – BNK Mar 09 '16 at 06:36
  • @BNK Sorry I could not understood :( can you confirm please `If parameter value of same web services is different at the time of hitting web service second time then will it send response me to from sever or cached.. ` – N Sharma Mar 09 '16 at 09:42
  • For GET requests, different params mean different URL, right? So, if data from that URL not cached before, Volley will get from server – BNK Mar 09 '16 at 09:46
  • I am using web services where post approach is being used. For ex : sometime I send one param "amount" : "100" of that api and second time "amount" : "200" so second time will it return response of cached or from server. – N Sharma Mar 09 '16 at 10:23
  • Pls post your sample response headers from your web service – BNK Mar 09 '16 at 11:12

2 Answers2

0

Volley decides to cache or not based on the headers you've specified. So for your question in case you hit the same web service before the validity period expires it will send the response from cache.

I think this answer has will better help you. :)

Community
  • 1
  • 1
KaHeL
  • 4,301
  • 16
  • 54
  • 78
0

You'll get the response from volley on the same callback. Volley makes sure that you won't have to deal with whether you got it from cache or not, and you should not care about it - you'll get it on the usual callback (onResponse).

If you want to make sure your request always goes out to the network then you can specify setShouldCache(false) on your specific request.

MarkySmarky
  • 1,609
  • 14
  • 17