深入Volley(一)volley缓存文件结构
来源:互联网 发布:ubuntu14.04 阿里云源 编辑:程序博客网 时间:2024/05/17 23:29
还是要再感谢下其于我之讨论,希望本篇文章能够如他一般帮助到别人;话不说直接上正文吧;
缓存流程分析:
首先:
public <T> Request<T> add(Request<T> 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; } }
/** * Called from {@link Request#finish(String)}, indicating that processing of the given request * has finished. * * <p>Releases waiting requests for <code>request.getCacheKey()</code> if * <code>request.shouldCache()</code>.</p> */ void finish(Request<?> request) { // Remove from the set of requests currently being processed. synchronized (mCurrentRequests) { mCurrentRequests.remove(request); } if (request.shouldCache()) { synchronized (mWaitingRequests) { String cacheKey = request.getCacheKey(); Queue<Request<?>> waitingRequests = mWaitingRequests.remove(cacheKey); if (waitingRequests != null) { if (VolleyLog.DEBUG) { VolleyLog.v("Releasing %d waiting requests for cacheKey=%s.", waitingRequests.size(), cacheKey); } // Process all queued up requests. They won't be considered as in flight, but // that's not a problem as the cache has been primed by 'request'. mCacheQueue.addAll(waitingRequests); } } } }
那么在中途还在队列没有被消耗的时候如何取消掉request呢
/** * A simple predicate or filter interface for Requests, for use by * {@link RequestQueue#cancelAll(RequestFilter)}. */ public interface RequestFilter { public boolean apply(Request<?> request); } /** * Cancels all requests in this queue for which the given filter applies. * @param filter The filtering function to use */ public void cancelAll(RequestFilter filter) { synchronized (mCurrentRequests) { for (Request<?> request : mCurrentRequests) { if (filter.apply(request)) { request.cancel(); } } } } /** * Cancels all requests in this queue with the given tag. Tag must be non-null * and equality is by identity. */ public void cancelAll(final Object tag) { if (tag == null) { throw new IllegalArgumentException("Cannot cancelAll with a null tag"); } cancelAll(new RequestFilter() { @Override public boolean apply(Request<?> request) { return request.getTag() == tag; } }); }
这里直接调用重载的cancelAll方法就好了,反正他也是调用cancel方法就不需要考虑是否需要缓存的问题了;
还有一点大家要注意的是:善用锁在整个队列里面是十分重要的,可以看到当操作两个容器类的时候volley都加上了一把锁,这是因为Java里面没有锁用这些容器类是十分容易出错的,所以当大家在封装自己的框架的时候一定要善用java锁机制,锁用好了不但可以使得容器类操作保证了原子性和线程可见性并且还可以对线程做很棒的处理,具体的建议大家可以看看wait notify ...等方法和参照《java并发实战这本书》;但如果读者是在不了解他那么直接在方法上加入一个synchronized修饰符也是不错的选择;
其次:
@Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); Request<?> request; while (true) { try { // Take a request from the queue. request = mQueue.take(); } catch (InterruptedException e) { // We may have been interrupted because it was time to quit. if (mQuit) { return; } continue; } try { request.addMarker("network-queue-take"); // If the request was cancelled already, do not perform the // network request. if (request.isCanceled()) { request.finish("network-discard-cancelled"); continue; } addTrafficStatsTag(request); // Perform the network request. NetworkResponse networkResponse = mNetwork.performRequest(request); request.addMarker("network-http-complete"); // If the server returned 304 AND we delivered a response already, // we're done -- don't deliver a second identical response. 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. 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)); } }
mCache.put(request.getCacheKey(),response.cacheEntry);将其缓存从这里看到我们的mNetworkQueue将request先调用performRequest方法将他转化成netWorkResponce,之后再调用
最后:
/** * Data and metadata for an entry returned by the cache. */ public static class Entry { /** The data returned from cache. */ public byte[] data; /** ETag for cache coherency. */ public String etag; /** Date of this response as reported by the server. */ public long serverDate; /** TTL for this record. */ public long ttl; /** Soft TTL for this record. */ public long softTtl; /** Immutable response headers as received from server; must be non-null. */ public Map<String, String> responseHeaders = Collections.emptyMap(); /** True if the entry is expired. */ public boolean isExpired() { return this.ttl < System.currentTimeMillis(); } /** True if a refresh is needed from the original data source. */ public boolean refreshNeeded() { return this.softTtl < System.currentTimeMillis(); } }
堆堆数据类型,但是不要管他,因为在put缓存过程中已经将url和ectity进行合并了
合并调用的是CacheHeader e = new CacheHeader(key, entry);这个东西
代码:
/** The size of the data identified by this CacheHeader. (This is not * serialized to disk. */ public long size; /** The key that identifies the cache entry. */ public String key; /** ETag for cache coherence. */ public String etag; /** Date of this response as reported by the server. */ public long serverDate; /** TTL for this record. */ public long ttl; /** Soft TTL for this record. */ public long softTtl; /** Headers from the response resulting in this cache entry. */ public Map<String, String> responseHeaders;
CacheHeader 类的其他方法先不管我们先看数据类型:
key ----就是request url
etag ---和缓存有关的response数据类型
ttl和softttl ---和缓存有关的response数据类型
responseHeaders -- 一些response头
serverDate -- 服务器时间
size -- response数据的长度
缓存文件分析:
上面我们分析了大致缓存流程,已经一些启动细节,现在正片开始,进行了实际缓存流程:
缓存FileName产生:
可以直观看出从这段代码产生 File file = getFileForKey(key); 而getFileForKey里面调用了getFileNameForKey(String url)得到fileName
getFileForKey代码:
private String getFilenameForKey(String key) { int firstHalfLength = key.length() / 2; String localFilename = String.valueOf(key.substring(0, firstHalfLength).hashCode()); localFilename += String.valueOf(key.substring(firstHalfLength).hashCode()); return localFilename; }
其实这个过程很简单就是通过URL转化成一串字符串;没啥好说的,看官们只需要自己复制这个方法然后调用试试就好
内容开始写入代码:
<span style="white-space:pre"></span>/** * Writes the contents of this CacheHeader to the specified OutputStream. */ public boolean writeHeader(OutputStream os) { try { writeInt(os, CACHE_MAGIC); writeString(os, key); writeString(os, etag == null ? "" : etag); writeLong(os, serverDate); writeLong(os, ttl); writeLong(os, softTtl); writeStringStringMap(responseHeaders, os); os.flush(); return true; } catch (IOException e) { VolleyLog.d("%s", e.toString()); return false; } }
魔数Magic:
/** Magic number for current version of cache file format. */ private static final int CACHE_MAGIC = 0x20120504;
static void writeInt(OutputStream os, int n) throws IOException { os.write((n >> 0) & 0xff); os.write((n >> 8) & 0xff); os.write((n >> 16) & 0xff); os.write((n >> 24) & 0xff); }
request Key的写入(Url)
static void writeString(OutputStream os, String s) throws IOException { byte[] b = s.getBytes("UTF-8"); writeLong(os, b.length); os.write(b, 0, b.length); }
Etag、serverDate、ttl、softTtl字段:
responseHead字段
static void writeStringStringMap(Map<String, String> map, OutputStream os) throws IOException { if (map != null) { writeInt(os, map.size()); for (Map.Entry<String, String> entry : map.entrySet()) { writeString(os, entry.getKey()); writeString(os, entry.getValue()); } } else { writeInt(os, 0); } }
response内容字段
本例中返回的是一串url;
最后
附上全字段解析图:
原:
析:
本文为本人原创的辛苦成果,如果您觉得帮助到了您请评论一个赞吧~
转载请注明出处: http://blog.csdn.net/u013178436/article/details/38406103
- 深入Volley(一)volley缓存文件结构
- 深入volley(二)volley缓存细节
- Volley 加载图片,文件缓存
- Volley源码(一)--不考虑缓存
- 4、Volley解析(二),源码的深入分析一,缓存线程和网络请求线程
- Volley
- Volley
- volley
- volley
- Volley
- Volley
- volley
- Volley
- Volley
- volley
- Volley
- volley
- Volley
- Android 开发尝试总结二:Android版HelloWorld项目文件结构分析
- 创建基于wicket的web项目(二)-常用控件
- UVA - 10474 Where is the Marble?
- (六)8天快速掌握Android视频教程_电话拨号器
- python的列表解析
- 深入Volley(一)volley缓存文件结构
- Linux Socket编程(不限Linux)
- AIX获取N天前系统日期及应用日志备份脚本
- 输入总结
- 杭电acm2170hdu-acm-2170解题报告
- 杭电 2089
- 百度面试——基础架构部
- Network error IOException: Connection refused: connect这是怎么回事?
- __int64 与long long 的区别