/** * Init a new cache store with a specific namespace * * @param ns The namespace to use for this cache store */ - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns;
/* * The attribute which the clear cache will be checked against when clearing the disk cache * Default is Modified Date */ @property (assign, nonatomic) SDImageCacheConfigExpireType diskCacheExpireType;
@end
2、SDImageCachesManager
管理NSArray<id> *caches;
一张图片就是一份SDImageCache元素对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/** A caches manager to manage multiple caches. */ @interface SDImageCachesManager : NSObject <SDImageCache>
- (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url options:(SDWebImageDownloaderOptions)options context:(nullable SDWebImageContext *)context progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock { // ....省略一堆代码 SD_LOCK(self.operationsLock); id downloadOperationCancelToken; NSOperation<SDWebImageDownloaderOperation> *operation = [self.URLOperations objectForKey:url]; if (!operation || operation.isFinished || operation.isCancelled) { // ....省略一堆代码 } else { // When we reuse the download operation to attach more callbacks, there may be thread safe issue because the getter of callbacks may in another queue (decoding queue or delegate queue) // So we lock the operation here, and in `SDWebImageDownloaderOperation`, we use `@synchonzied (self)`, to ensure the thread safe between these two classes. @synchronized (operation) { downloadOperationCancelToken = [operation addHandlersForProgress:progressBlock completed:completedBlock]; } // ....省略一堆代码 } SD_UNLOCK(self.operationsLock); // 虽然重复的URL只有一个SDWebImageDownloaderOperation。但是SDWebImageDownloadToken是每个URL都会有一个的,只是他们的SDWebImageDownloaderOperation是同一个。 SDWebImageDownloadToken *token = [[SDWebImageDownloadToken alloc] initWithDownloadOperation:operation]; token.url = url; token.request = operation.request; token.downloadOperationCancelToken = downloadOperationCancelToken; return token; }
// SDWebImageDownloaderConfig.h /** * The maximum number of concurrent downloads. * Defaults to 6. */ @property (nonatomic, assign) NSInteger maxConcurrentDownloads;
[sself.downloadQueue addOperation:operation]; if (sself.executionOrder == SDWebImageDownloaderLIFOExecutionOrder) { // Emulate LIFO execution order by systematically adding new operations as last operation's dependency [sself.lastAddedOperation addDependency:operation]; sself.lastAddedOperation = operation; }