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

1

5

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

Pelagias answered 5/8, 2013 at 18:53 Comment(0)
D
11

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.

Deck answered 6/8, 2013 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.