@property sharedURLCache
@abstract Returns the shared NSURLCache instance or
sets the NSURLCache instance shared by all clients of
the current process. This will be the new object returned when
calls to the <tt>sharedURLCache</tt> method are made.
@discussion Unless set explicitly through a call to
<tt>+setSharedURLCache:</tt>, this method returns an NSURLCache
instance created with the following default values:
<ul>
<li>Memory capacity: 4 megabytes (4 * 1024 * 1024 bytes)
<li>Disk capacity: 20 megabytes (20 * 1024 * 1024 bytes)
<li>Disk path: <nobr>(user home directory)/Library/Caches/(application bundle id)</nobr>
</ul>
<p>Users who do not have special caching requirements or
constraints should find the default shared cache instance
acceptable. If this default shared cache instance is not
acceptable, <tt>+setSharedURLCache:</tt> can be called to set a
different NSURLCache instance to be returned from this method.
Callers should take care to ensure that the setter is called
at a time when no other caller has a reference to the previously-set
shared URL cache. This is to prevent storing cache data from
becoming unexpectedly unretrievable.
@result the shared NSURLCache instance.
NSURLCache
1、默认值
缓存路径 /沙盒/library/caches/bundleid/.......
内存4mb
磁盘缓存20Mb
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
//TODO set the default HTTP headers
configuration.HTTPShouldSetCookies = YES;
configuration.HTTPShouldUsePipelining = NO;
configuration.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
configuration.allowsCellularAccess = YES;
configuration.timeoutIntervalForRequest = 60.0;
//这个缓存如何工作的
configuration.URLCache = [AFImageDownloader defaultURLCache];
2、 NSURLCache自定义值 20Mb, 150Mb
In iOS, path is the name of a subdirectory of the application’s default cache directory in which to store the on-disk cache (the subdirectory is created if it does not exist).
//直接传子目录的名字进来就可以了
[[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024
diskCapacity:150 * 1024 * 1024
diskPath:@"com.alamofire.imagedownloader"];
/沙盒/library/caches/bundleid/subdirectory
/Users/huchu/Library/Developer/CoreSimulator/Devices/7BB37B11-B021-495A-870A-BCC64380AB11/data/Containers/Data/Application/AE6F188D-70CF-4F55-8B3B-C9DECA97C078/Library/Caches/com.sysjones.af-serialization/com.alamofire.imagedownloader/Cache.db
下载器。
1, 需要缓存, 还需要缓存超过阈值的淘汰机制:
1,是在内存,还是在 磁盘, 还是两者
2,如果是在内存, 最大分配的内存, 超过最大内存阈值,需要 清除;
淘汰机制:
FIFO 如果一个数据最先进入缓存中,则应该最早淘汰掉;
LFU(Least Frequently Used)最近最少使用算法。 ---- 访问频率
如果一个数据在最近一段时间内使用次数很少,那么在将来一段时间内被使用的可能性也很小
LRU全称是Least Recently Used,即最近最久未使用的意思。---- 访问时间
如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小。也就是说,当限定的空间已存满数据时,应当把最久没有被访问到的数据淘汰。
3,如果是存磁盘, 也需要一个超过最大磁盘上限,清除缓存文件的机制。
4,如果是app应用级的收到全局内存报警,需要清除所有内存。
2,多线程下载。 需要一个同时下载数量的控制。 其余下载任务需要排队。
queuedMergedTasks
出列, 优先级
FIFO 先进先出(插入到对列末尾)
LIFO 最新入队的被优先调用 (插入对列,插入到下标第0位)
3,同一个url对应的请求request,可能在多个地方调用。
重复请求合并的, 但是仍然能得到的回调的机制。
4, 还需要提供一个随时可以取消下载任务的支持
Append the success and failure blocks to a pre-existing request if it already exists
Attempt to load the image from the image cache if the cache policy allows it
Create the request and set up authentication, validation and response serialization
Store the response handler for use when the request completes
Either start the request or enqueue it depending on the current active request count
将成功和失败块附加到已经存在的请求中
如果缓存策略允许,尝试从图像缓存加载图像。
创建请求并设置身份验证、验证和响应序列化
存储响应处理程序,以便在请求完成时使用
要么启动请求,要么根据当前活动请求计数对其进行排队
网友评论