多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Android volley 解析(四)之緩存篇

Android volley 解析(四)之緩存篇

來源:程序員人生   發布時間:2015-04-08 08:54:03 閱讀次數:3186次

這是 volley 的第4篇 blog 了,寫完這篇,volley 的大部份用法也都算寫了1遍,所以暫時不會寫 volley 的文章了,如果想看我前面寫的文章,可以點這里 Android volley 解析(3)之文件上傳篇

為何要用緩存

我們知道,當客戶端在要求網絡數據的時候,是需要消耗流量的,特別是對移動端用戶來講,對流量的控制要求很高。所以在做網絡要求的時候,如果對數據更新要求不是特別高,常常都會用到緩存機制,1方面能減少對服務真個要求,控制流量;另外一方面,當客戶端在沒有網絡的情況下也能看到上1次要求的數據,這樣使用戶體驗更佳,以下圖,微信朋友圈在沒有網絡的情況下,仍然能看到朋友們的動態
這里寫圖片描述

volley 緩存的原理

看了我前面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點點,相信大家都能實現的。

源碼下載地址

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲蜜桃 | 中文字幕看片在线a免费 | 中文字幕日韩欧美一区二区三区 | 91精品国产一区二区三区左线 | 亚洲欧洲精品国产二码 | 国产精品福利影院 | 国产美女亚洲精品久久久毛片 | 国内一区二区三区精品视频 | 亚洲精品αv一区二区三区 亚洲精品播放 | 亚洲精品一区亚洲精品 | 国产最新精品2020好看的 | freexxx性欧美hd丝袜 | 国内精品视频免费观看 | 欧美日韩综合一区 | 国产日本韩国 | 精品日韩欧美一区二区三区在线播放 | 人操人视频 | 最新lutube亚洲看片在线观看 | 2020国产精品永久在线观看 | 日本理论午夜中文字幕第一页 | 欧美日韩一区二区三区视频播 | 在线综合亚洲欧美网站天堂 | 亚洲欧美国产一区二区三区 | 免费网站在线观看高清版 | 国产精品热久久毛片 | 国产欧美综合在线一区二区三区 | 波多野结衣资源在线 | 欧美日韩性生活视频 | 日韩欧美一区二区久久黑人 | 色综合天天综一个色天天综合网 | 另类欧美日韩 | 免费国产福利 | 久久在线免费观看视频 | 五月香福利 | 欧美一区二区三区久久久 | 欧美国产成人在线 | 精品一区二区影院在线 | 国产成人免费不卡在线观看 | 国产区图片区小说区亚洲区 | 国内精品久久久久影院网站 | 国产欧美成人免费观看视频 |