2013-08-05 27 views
5

我用來獲取圖像的服務,像許多這樣的網站沒有緩存控制標頭,指示圖像應該被緩存多長時間。 Volley默認使用http緩存控制標題來決定在磁盤上緩存圖像的時間。我如何覆蓋這種默認行爲並將這些圖像保留一段時間?如何更改volley中的默認磁盤緩存行爲?

感謝

回答

11

我需要的默認緩存策略更改爲「緩存」的政策,沒有考慮到的HTTP標頭。

您想緩存一段時間。有幾種方法可以做到這一點,因爲代碼中有許多地方可以「觸摸」網絡響應。我建議(在第39行parseCacheHeaders法)的編輯到HttpHeaderParser

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是一個常數,表示你希望每個形象活在時間緩存。