【问题标题】:how can one change the default disk cache behavior in volley?如何更改 volley 中的默认磁盘缓存行为?
【发布时间】:2013-08-05 18:53:29
【问题描述】:

我用来获取图像的服务,与许多此类网站一样,没有缓存控制标头,指示图像应缓存多长时间。 Volley 默认使用 http 缓存控制标头来决定在磁盘上缓存图像多长时间。如何覆盖此默认行为并将此类图像保留一段时间?

谢谢

【问题讨论】:

    标签: android android-volley


    【解决方案1】:

    我需要将默认缓存策略更改为“全部缓存”策略,而不考虑 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 是一个常数,表示您希望每张图片在缓存中存在的时间。

    【讨论】:

      猜你喜欢
      • 2014-02-13
      • 2014-03-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 2013-07-26
      • 2015-11-05
      • 1970-01-01
      相关资源
      最近更新 更多