如何更改凌空中的默认磁盘缓存行为?

Tes*_*est 5 android android-volley

我用来获取图像的服务,像许多这样的网站没有缓存控制标题,指示图像应缓存多长时间.默认情况下,Volley使用http缓存控件头来决定在磁盘上缓存图像的时间.我怎样才能覆盖此默认行为并将这些图像保留一段时间?

谢谢

Ita*_*ski 11

我需要将默认缓存策略更改为"全部缓存"策略,而不考虑HTTP标头.

您希望缓存一段时间.有几种方法可以做到这一点,因为代码中有很多地方可以"触摸"网络响应.我建议编辑HttpHeaderParser(parseCacheHeaders第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;
Run Code Online (Sandbox Code Playgroud)

和另一个Cache.Entry上课:

/** 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();
}
Run Code Online (Sandbox Code Playgroud)

where GLOBAL_TTL是一个常量,表示您希望每个图像在缓存中生存的时间.