Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can one change the default disk cache behavior in volley?

The service I am using to obtain images, like many such sites does not have a cache control header indicating how long the image should be cached. Volley uses an http cache control header by default to decide how long to cache images on disk. How could I override this default behavior and keep such images for a set period of time?

Thanks

like image 233
TestBest Avatar asked Aug 05 '13 18:08

TestBest


1 Answers

I needed to change the default caching strategy to a "cache all" policy, without taking into account the HTTP headers.

You want to cache for a set period of time. There are several ways you can do this, since there are many places in the code that "touch" the network response. I suggest an edit to the HttpHeaderParser (parseCacheHeaders method at line 39):

Cache.Entry entry = new Cache.Entry();
entry.data = response.data;
entry.etag = serverEtag;
entry.softTtl = softExpire;
entry.ttl = now; // **Edited**
entry.serverDate = serverDate;
entry.responseHeaders = headers;

and another to Cache.Entry class:

/** True if the entry is expired. */
public boolean isExpired() {
    return this.ttl + GLOBAL_TTL < System.currentTimeMillis();
}

/** True if a refresh is needed from the original data source. */
public boolean refreshNeeded() {
    return this.softTtl + GLOBAL_TTL < System.currentTimeMillis();
}

where GLOBAL_TTL is a constant representing the time you want each image to live in the cache.

like image 130
Itai Hanski Avatar answered Oct 05 '22 11:10

Itai Hanski