日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全IT社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > php開源 > 綜合技術(shù) > Android volley 解析(四)之緩存篇

Android volley 解析(四)之緩存篇

來源:程序員人生   發(fā)布時間:2015-04-08 08:54:03 閱讀次數(shù):3196次

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

為何要用緩存

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

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.如果要求沒有設(shè)置緩存,則把要求添加到網(wǎng)絡(luò)隊列中 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; } }

代碼不長,也很好理解,如果我們的要求沒有設(shè)置了緩存,則要求添加到網(wǎng)絡(luò)要求隊列中,并直接返回了,不往下履行了,這時候緩存隊列中沒法獲得要求,所以這里我們知道了,想要用緩存則需要
在 Request 中把

//設(shè)置是不是啟用緩存 public final void setShouldCache(boolean shouldCache) { mShouldCache = shouldCache; }

設(shè)為 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.如果已取消要求,則結(jié)束本次要求的所有操作 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次換取,沒有情求過網(wǎng)絡(luò);2,緩存的數(shù)據(jù)到達了最大限度,此緩存已被刪除。 if (entry == null) { request.addMarker("cache-miss"); Log.i("CacheDispatcher", "沒有緩存數(shù)據(jù):" + request.getUrl()); mNetworkQueue.put(request); continue; } // If it is completely expired, just send it to the network.緩存已過期,則重新要求網(wǎng)絡(luò) if (entry.isExpired()) { request.addMarker("cache-hit-expired"); request.setCacheEntry(entry); Log.i("CacheDispatcher", "緩存數(shù)據(jù)過期:" + request.getUrl()); mNetworkQueue.put(request); continue; } // We have a cache hit; parse its data for delivery back to the request. request.addMarker("cache-hit"); //到這里表示已成功獲得緩存數(shù)據(jù) Response<?> response = request.parseNetworkResponse( new NetworkResponse(entry.data, entry.responseHeaders)); request.addMarker("cache-hit-parsed"); //如果緩存需要刷新,則要求網(wǎng)絡(luò) if (!entry.refreshNeeded()) { // Completely unexpired cache hit. Just deliver the response. Log.i("CacheDispatcher", "獲得緩存數(shù)據(jù):" + 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獲得,并經(jīng)過幾次驗證,如果緩存合法則解析然后交給 UI線程分發(fā)出去。下面來看看具體的流程
這里寫圖片描述

存儲緩存
如果是第1次要求,或緩存已過期,肯定是沒法獲得到緩存的,這時候可根據(jù)上圖分析,將會進入網(wǎng)絡(luò)要求線程NetworkDispatcher,貯存緩存毫無疑問也是在這里面實現(xiàn)的。

//省略部份代碼 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.如果需要緩存,并且用戶已把網(wǎng)絡(luò)要求的數(shù)據(jù)轉(zhuǎn)換成1份為緩存數(shù)據(jù),則通過 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)); } //省略部份代碼

通過以上代碼可以知道,在網(wǎng)絡(luò)要求線程要求到數(shù)據(jù)以后,會交給用戶解析,并把數(shù)據(jù)轉(zhuǎn)換1份成緩存數(shù)據(jù),通過 Cache 緩存操作類,把數(shù)據(jù)緩存起來。

網(wǎng)絡(luò)數(shù)據(jù)轉(zhuǎn)換成緩存數(shù)據(jù)
上面提到了,把網(wǎng)絡(luò)數(shù)據(jù)轉(zhuǎn)化成緩存數(shù)據(jù),那末,volley 是如何轉(zhuǎn)換的?

*/ public static Cache.Entry parseCacheHeaders(NetworkResponse response) { long now = System.currentTimeMillis(); //獲得網(wǎng)絡(luò)要求數(shù)據(jù)的頭信息 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 數(shù)據(jù) headerValue = headers.get("Date"); if (headerValue != null) { serverDate = parseDateAsEpoch(headerValue); } //從頭信息中獲得 Cache-Control 數(shù)據(jù),來控制緩存 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中只實現(xiàn)了緩存是不是過期的操作,沒有實現(xiàn)緩存是不是需要刷新

@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()); //從這里開始設(shè)置緩存信息,如果設(shè)置了緩存的緩存時間,則把它添加到頭部信息中,但是沒有實現(xiàn)是不是需要刷新緩存的操作,如果有需要,也能夠在這里實現(xiàn),這是就需要修改源碼。 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); } } } }

如何使用緩存

根據(jù)上面分析不難發(fā)現(xiàn),要使用緩存,得具有兩個條件,
1、啟用緩存

public final void setShouldCache(boolean shouldCache) { mShouldCache = shouldCache; }

不過這個條件默許情況下是開啟的。
2、設(shè)置緩存的時間

public void setCacheTime(long cacheTime) { mCacheTime = cacheTime; }

這里 cacheTime 的單位是秒。
接下來看看具體用法

public class CacheRequestActivity extends ActionBarActivity { /*數(shù)據(jù)顯示的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(); /*要求網(wǎng)絡(luò)獲得數(shù)據(jù)*/ 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(); /*顯示數(shù)據(jù)*/ 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); //設(shè)置緩存時間10分鐘 request.setCacheTime(10*60); VolleyUtil.getRequestQueue().add(request) ; }

再來看看效果圖,在緩存存在的情況下當(dāng)把網(wǎng)絡(luò)連接斷開的時候,仍然能夠獲得到數(shù)據(jù)
這里寫圖片描述

有1種情況需要注意:當(dāng)需要獲得緩存,且希望緩存刷新的時候,這類情況就需要修改 Volley 的源碼,前面已提到1點點,相信大家都能實現(xiàn)的。

源碼下載地址

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 日韩视频免费在线观看 | 日韩国产欧美一区二区三区 | 在线成人精品国产区免费 | 91精品国产高清一区二区三区 | 国产在线网站 | 久久久看片 | 精品视频网站 | 欧美插插视频 | 日韩电影一区 | 这里精品 | 国产成人精品自拍 | 99精品视频在线免费观看 | 性爱免费视频 | 国产精品成人国产乱一区 | 自拍视频一区 | 国产91在线 | 中日 | 亚洲电影免费 | 免费的黄色 | 精品国产乱 | 国产精品羞羞 | 91免费国产在线 | 国产午夜亚洲精品理论片色戒 | 91视视频在线观看入口直接观看 | 成人免费视频网 | 日韩精品一区二区三区四区视频 | 欧美激情视频一区二区三区 | 天天色天天 | 亚洲欧洲成人 | 欧美一区二区三区啪啪 | 国产精品a久久久久 | 久久久久久久一区二区三区 | 中文字幕精品一区 | 日本一区二区视频在线 | 天天干狠狠操 | 日韩综合网 | 亚洲欧美中文日韩在线v日本 | 福利视频在线看 | 欧美国产中文字幕 | 精品成人免费一区二区在线播放 | 操到喷水 | 久热久热 |