Set expiration policy for cache using Google's Volley
Asked Answered
V

2

21

Is it possible to modify the expiration policy for the cache on Google's Volley lib? I believe you can implement your own cache, however is there an easy way to do this with the default implementation?

Vickeyvicki answered 28/5, 2013 at 2:55 Comment(0)
M
19

It seems that currently the cache expiration is controlled solely by the "Cache" headers in the response so basically you are left to the mercy of the server. Whatever server returns in the "Expires" or "max-age=" that will be used. We will have to use our own cache if we need custom expiry policy...

Mucin answered 29/5, 2013 at 8:50 Comment(3)
This is incorrect. Using the answer provided by oleksandr_yefremov you can override the cache expiration headers.Bunkmate
@Laurence You absolutely correct that the answer by oleksandr is better one but there is one(2) fine point(s): a) If you have control over the web server/backend you may set the desired expiration interval in there; b) if you don't have control over the web server may be there is a good reason why the expiration policy is set that way and may not be very good idea to try to override it. My point is: the developer needs to think what the consequences may be and not blindly override it just because the existing exp policy does not fit in easily in the current app.Mucin
True but with the method provided by oleksandr_yefremov you still have to explicitly set the new header rules per request. It's not as if ALL of your requests will follow this rule.Bunkmate
W
24

No need to write your own cache implementation. You can change method com.android.volley.toolbox.HttpHeaderParser.parseCacheHeaders(NetworkResponse response), ignore these headers, set entry.softTtl and entry.ttl fields to whatever value works for you and use your method in your request class.

There is an example in my answer to similar question - Android Volley + JSONObjectRequest Caching

Wormy answered 31/5, 2013 at 7:55 Comment(0)
M
19

It seems that currently the cache expiration is controlled solely by the "Cache" headers in the response so basically you are left to the mercy of the server. Whatever server returns in the "Expires" or "max-age=" that will be used. We will have to use our own cache if we need custom expiry policy...

Mucin answered 29/5, 2013 at 8:50 Comment(3)
This is incorrect. Using the answer provided by oleksandr_yefremov you can override the cache expiration headers.Bunkmate
@Laurence You absolutely correct that the answer by oleksandr is better one but there is one(2) fine point(s): a) If you have control over the web server/backend you may set the desired expiration interval in there; b) if you don't have control over the web server may be there is a good reason why the expiration policy is set that way and may not be very good idea to try to override it. My point is: the developer needs to think what the consequences may be and not blindly override it just because the existing exp policy does not fit in easily in the current app.Mucin
True but with the method provided by oleksandr_yefremov you still have to explicitly set the new header rules per request. It's not as if ALL of your requests will follow this rule.Bunkmate

© 2022 - 2024 — McMap. All rights reserved.