這是 volley 的第4篇 blog 了,寫完這篇,volley 的大部份用法也都算寫了1遍,所以暫時不會寫 volley 的文章了,如果想看我前面寫的文章,可以點這里 Android volley 解析(3)之文件上傳篇
我們知道,當客戶端在要求網絡數據的時候,是需要消耗流量的,特別是對移動端用戶來講,對流量的控制要求很高。所以在做網絡要求的時候,如果對數據更新要求不是特別高,常常都會用到緩存機制,1方面能減少對服務真個要求,控制流量;另外一方面,當客戶端在沒有網絡的情況下也能看到上1次要求的數據,這樣使用戶體驗更佳,以下圖,微信朋友圈在沒有網絡的情況下,仍然能看到朋友們的動態
看了我前面blog 的朋友1定還記得,我在講 get 要求的時候,講到了volley 的基本流程,首先啟動的是緩存線程mCacheDispatcher來獲得緩存;
那我們就從如何獲得緩存開始分析;
1、初始化緩存
@Override
public void run() {
if (DEBUG) VolleyLog.v("start new dispatcher");
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
// Make a blocking call to initialize the cache.這里對緩存做初始化操作
mCache.initialize();
while (true) {
...
2、獲得緩存
while (true) {
try {
// Get a request from the cache triage queue, blocking until
// at least one is available.從緩存隊列中獲得緩存
final Request request = mCacheQueue.take();
request.addMarker("cache-queue-take");
// If the request has been canceled, don't bother dispatching it.
...
這里有1個問題,緩存隊列在甚么時候添加緩存要求呢?我們回到最開始要求隊列添加要求的地方
public Request add(Request request) {
// Tag the request as belonging to this queue and add it to the set of current requests.
request.setRequestQueue(this);
synchronized (mCurrentRequests) {
mCurrentRequests.add(request);
}
// Process requests in the order they are added.
request.setSequence(getSequenceNumber());
request.addMarker("add-to-queue");
// If the request is uncacheable, skip the cache queue and go straight to the network.如果要求沒有設置緩存,則把要求添加到網絡隊列中
if (!request.shouldCache()) {
mNetworkQueue.add(request);
return request;
}
// Insert request into stage if there's already a request with the same cache key in flight.
synchronized (mWaitingRequests) {
String cacheKey = request.getCacheKey();
if (mWaitingRequests.containsKey(cacheKey)) {
// There is already a request in flight. Queue up.
Queue<Request> stagedRequests = mWaitingRequests.get(cacheKey);
if (stagedRequests == null) {
stagedRequests = new LinkedList<Request>();
}
stagedRequests.add(request);
mWaitingRequests.put(cacheKey, stagedRequests);
if (VolleyLog.DEBUG) {
VolleyLog.v("Request for cacheKey=%s is in flight, putting on hold.", cacheKey);
}
} else {
// Insert 'null' queue for this cacheKey, indicating there is now a request in
// flight.
mWaitingRequests.put(cacheKey, null);
//這里添加要求到緩存隊列中
mCacheQueue.add(request);
}
return request;
}
}
代碼不長,也很好理解,如果我們的要求沒有設置了緩存,則要求添加到網絡要求隊列中,并直接返回了,不往下履行了,這時候緩存隊列中沒法獲得要求,所以這里我們知道了,想要用緩存則需要
在 Request 中把
//設置是不是啟用緩存
public final void setShouldCache(boolean shouldCache) {
mShouldCache = shouldCache;
}
設為 true,固然,我們看mShouldCache 的默許值
/**
* Whether or not responses to this request should be cached.
*/
private boolean mShouldCache = true;
volley默許啟用緩存的。再往下看
// If the request has been canceled, don't bother dispatching it.如果已取消要求,則結束本次要求的所有操作
if (request.isCanceled()) {
request.finish("cache-discard-canceled");
continue;
}
// Attempt to retrieve this item from cache. 通過緩存類獲得緩存
Cache.Entry entry = mCache.get(request.getCacheKey());
//如果獲得的緩存為空,這里有兩種情況為空,1,第1次換取,沒有情求過網絡;2,緩存的數據到達了最大限度,此緩存已被刪除。
if (entry == null) {
request.addMarker("cache-miss");
Log.i("CacheDispatcher", "沒有緩存數據:" + request.getUrl());
mNetworkQueue.put(request);
continue;
}
// If it is completely expired, just send it to the network.緩存已過期,則重新要求網絡
if (entry.isExpired()) {
request.addMarker("cache-hit-expired");
request.setCacheEntry(entry);
Log.i("CacheDispatcher", "緩存數據過期:" + request.getUrl());
mNetworkQueue.put(request);
continue;
}
// We have a cache hit; parse its data for delivery back to the request.
request.addMarker("cache-hit");
//到這里表示已成功獲得緩存數據
Response<?> response = request.parseNetworkResponse(
new NetworkResponse(entry.data, entry.responseHeaders));
request.addMarker("cache-hit-parsed");
//如果緩存需要刷新,則要求網絡
if (!entry.refreshNeeded()) {
// Completely unexpired cache hit. Just deliver the response.
Log.i("CacheDispatcher", "獲得緩存數據:" + request.getUrl());
mDelivery.postResponse(request, response);
} else {
// Soft-expired cache hit. We can deliver the cached response,
// but we need to also send the request to the network for
// refreshing.
request.addMarker("cache-hit-refresh-needed");
request.setCacheEntry(entry);
// Mark the response as intermediate.
response.intermediate = true;
// Post the intermediate response back to the user and have
// the delivery then forward the request along to the network.
mDelivery.postResponse(request, response, new Runnable() {
@Override
public void run() {
try {
mNetworkQueue.put(request);
} catch (InterruptedException e) {
// Not much we can do about this.
}
}
});
}
} catch (InterruptedException e) {
// We may have been interrupted because it was time to quit.
if (mQuit) {
return;
}
continue;
}
}
直接從緩存類Cache獲得,并經過幾次驗證,如果緩存合法則解析然后交給 UI線程分發出去。下面來看看具體的流程
存儲緩存
如果是第1次要求,或緩存已過期,肯定是沒法獲得到緩存的,這時候可根據上圖分析,將會進入網絡要求線程NetworkDispatcher,貯存緩存毫無疑問也是在這里面實現的。
//省略部份代碼
if (networkResponse.notModified && request.hasHadResponseDelivered()) {
request.finish("not-modified");
continue;
}
// Parse the response here on the worker thread.
Response<?> response = request.parseNetworkResponse(networkResponse);
request.addMarker("network-parse-complete");
// Write to cache if applicable.
// TODO: Only update cache metadata instead of entire record for 304s.如果需要緩存,并且用戶已把網絡要求的數據轉換成1份為緩存數據,則通過 Cache 類把緩存存儲。
if (request.shouldCache() && response.cacheEntry != null) {
mCache.put(request.getCacheKey(), response.cacheEntry);
request.addMarker("network-cache-written");
}
// Post the response back.
request.markDelivered();
mDelivery.postResponse(request, response);
} catch (VolleyError volleyError) {
parseAndDeliverNetworkError(request, volleyError);
} catch (Exception e) {
VolleyLog.e(e, "Unhandled exception %s", e.toString());
mDelivery.postError(request, new VolleyError(e));
}
//省略部份代碼
通過以上代碼可以知道,在網絡要求線程要求到數據以后,會交給用戶解析,并把數據轉換1份成緩存數據,通過 Cache 緩存操作類,把數據緩存起來。
網絡數據轉換成緩存數據
上面提到了,把網絡數據轉化成緩存數據,那末,volley 是如何轉換的?
*/
public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
long now = System.currentTimeMillis();
//獲得網絡要求數據的頭信息
Map<String, String> headers = response.headers;
long serverDate = 0;
long serverExpires = 0;
long softExpire = 0;
long maxAge = 0;
boolean hasCacheControl = false;
String serverEtag = null;
String headerValue;
//從頭信息中獲得 Date 數據
headerValue = headers.get("Date");
if (headerValue != null) {
serverDate = parseDateAsEpoch(headerValue);
}
//從頭信息中獲得 Cache-Control 數據,來控制緩存
headerValue = headers.get("Cache-Control");
if (headerValue != null) {
hasCacheControl = true;
String[] tokens = headerValue.split(",");
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i].trim();
if (token.equals("no-cache") || token.equals("no-store")) {
return null;
} else if (token.startsWith("max-age=")) {
try {
maxAge = Long.parseLong(token.substring(8));
} catch (Exception e) {
}
} else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
maxAge = 0;
}
}
}
headerValue = headers.get("Expires");
if (headerValue != null) {
serverExpires = parseDateAsEpoch(headerValue);
}
serverEtag = headers.get("ETag");
// Cache-Control takes precedence over an Expires header, even if both exist and Expires
// is more restrictive.
if (hasCacheControl) {
softExpire = now + maxAge * 1000;
} else if (serverDate > 0 && serverExpires >= serverDate) {
// Default semantic for Expire header in HTTP specification is softExpire.
softExpire = now + (serverExpires - serverDate);
}
Cache.Entry entry = new Cache.Entry();
entry.data = response.data;
entry.etag = serverEtag;
entry.softTtl = softExpire;
entry.ttl = entry.softTtl;
entry.serverDate = serverDate;
entry.responseHeaders = headers;
return entry;
}
前面我們提到的緩存是不是過期和是不是需要刷新,都是通過 response 的頭部信息來判斷,但是在BasicNetwork中只實現了緩存是不是過期的操作,沒有實現緩存是不是需要刷新
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
long requestStart = SystemClock.elapsedRealtime();
while (true) {
HttpResponse httpResponse = null;
byte[] responseContents = null;
Map<String, String> responseHeaders = new HashMap<String, String>();
try {
// Gather headers.
Map<String, String> headers = new HashMap<String, String>();
addCacheHeaders(headers, request.getCacheEntry());
httpResponse = mHttpStack.performRequest(request, headers);
StatusLine statusLine = httpResponse.getStatusLine();
int statusCode = statusLine.getStatusCode();
responseHeaders = convertHeaders(httpResponse.getAllHeaders());
//從這里開始設置緩存信息,如果設置了緩存的緩存時間,則把它添加到頭部信息中,但是沒有實現是不是需要刷新緩存的操作,如果有需要,也能夠在這里實現,這是就需要修改源碼。
if(request.getCacheTime() != 0){
responseHeaders.put("Cache-Control","max-age=" + request.getCacheTime());
}
// Handle cache validation.
if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED,
request.getCacheEntry().data, responseHeaders, true);
}
responseContents = entityToBytes(httpResponse.getEntity());
// if the request is slow, log it.
long requestLifetime = SystemClock.elapsedRealtime() - requestStart;
logSlowRequests(requestLifetime, request, responseContents, statusLine);
if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_NO_CONTENT) {
throw new IOException();
}
return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
} catch (SocketTimeoutException e) {
attemptRetryOnException("socket", request, new TimeoutError());
} catch (ConnectTimeoutException e) {
attemptRetryOnException("connection", request, new TimeoutError());
} catch (MalformedURLException e) {
throw new RuntimeException("Bad URL " + request.getUrl(), e);
} catch (IOException e) {
int statusCode = 0;
NetworkResponse networkResponse = null;
if (httpResponse != null) {
statusCode = httpResponse.getStatusLine().getStatusCode();
} else {
throw new NoConnectionError(e);
}
VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
if (responseContents != null) {
networkResponse = new NetworkResponse(statusCode, responseContents,
responseHeaders, false);
if (statusCode == HttpStatus.SC_UNAUTHORIZED ||
statusCode == HttpStatus.SC_FORBIDDEN) {
attemptRetryOnException("auth",
request, new AuthFailureError(networkResponse));
} else {
// TODO: Only throw ServerError for 5xx status codes.
throw new ServerError(networkResponse);
}
} else {
throw new NetworkError(networkResponse);
}
}
}
}
根據上面分析不難發現,要使用緩存,得具有兩個條件,
1、啟用緩存
public final void setShouldCache(boolean shouldCache) {
mShouldCache = shouldCache;
}
不過這個條件默許情況下是開啟的。
2、設置緩存的時間
public void setCacheTime(long cacheTime) {
mCacheTime = cacheTime;
}
這里 cacheTime 的單位是秒。
接下來看看具體用法
public class CacheRequestActivity extends ActionBarActivity {
/*數據顯示的View*/
private TextView mIdTxt,mNameTxt,mDownloadTxt,mLogoTxt,mVersionTxt ;
/*彈出等待對話框*/
private ProgressDialog mDialog ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get);
mIdTxt = (TextView) findViewById(R.id.id_id) ;
mNameTxt = (TextView) findViewById(R.id.id_name) ;
mDownloadTxt = (TextView) findViewById(R.id.id_download) ;
mLogoTxt = (TextView) findViewById(R.id.id_logo) ;
mVersionTxt = (TextView) findViewById(R.id.id_version) ;
mDialog = new ProgressDialog(this) ;
mDialog.setMessage("get要求中...");
mDialog.show();
/*要求網絡獲得數據*/
MiNongApi.CacheObjectMiNongApi("minongbang", new ResponseListener<TestBean>() {
@Override
public void onErrorResponse(VolleyError error) {
mDialog.dismiss();
}
@Override
public void onResponse(TestBean response) {
Log.v("zgy","=======response======="+response) ;
mDialog.dismiss();
/*顯示數據*/
mIdTxt.setText(response.getId() + "");
mNameTxt.setText(response.getName());
mDownloadTxt.setText(response.getDownload() + "");
mLogoTxt.setText(response.getLogo());
mVersionTxt.setText(response.getVersion() + "");
}
});
}
}
cache api
public static void CacheObjectMiNongApi(String value,ResponseListener listener){
String url ;
try {
url = Constant.MinongHost +"?test="+ URLEncoder.encode(value, "utf⑻") ;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
url = Constant.MinongHost +"?test="+ URLEncoder.encode(value) ;
}
Request request = new GetObjectRequest(url,new TypeToken<TestBean>(){}.getType(),listener) ;
//請用緩存
request.setShouldCache(true);
//設置緩存時間10分鐘
request.setCacheTime(10*60);
VolleyUtil.getRequestQueue().add(request) ;
}
再來看看效果圖,在緩存存在的情況下當把網絡連接斷開的時候,仍然能夠獲得到數據
有1種情況需要注意:當需要獲得緩存,且希望緩存刷新的時候,這類情況就需要修改 Volley 的源碼,前面已提到1點點,相信大家都能實現的。
源碼下載地址
上一篇 ios 唯一標示