繁体   English   中英

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

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

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

谢谢

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

您希望缓存一段时间。 有几种方法可以做到这一点,因为代码中有很多地方可以“触摸”网络响应。 我建议编辑HttpHeaderParser (第39行的parseCacheHeaders方法):

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;

另一个是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();
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM