国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > Android volley 解析(四)之缓存篇

Android volley 解析(四)之缓存篇

来源:程序员人生   发布时间:2015-04-08 08:54:03 阅读次数:3001次

这是 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点点,相信大家都能实现的。

源码下载地址

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生