缓存

这一小节主要研究下SDWebImage的cache功能是如何实现的,首先找到在SDWebImage中充当缓存功能的类,这一步很简单,直接定位到SDImageCache类。也就是说,接下来主要就研究这个类了。

SDImageCacheConfig 缓存配置

SDWebImage提供了一个专门的类SDImageCacheConfig,来进行Cache的一些配置工作,主要配置的信息包括以下几点

  1. 是否允许解压原图
  2. 是否支持iCloud,默认不支持
  3. 是否允许将图片缓存到内存,默认支持
  4. 最大缓存周期(默认是一周)
  5. 最大缓存大小(默认不设置)

SDImageCache 缓存类

SDImageCache是一个单例,作为执行图片缓存的工具类,整个App只需要有一个全局的实例就可以了,这没毛病。

SDImageCache文件系统

在开始缓存操作之前,除了配置项需要配置外,我个人感觉还有一个地方需要提前准备好的就是缓存的文件系统。

默认的文件存储路径

SDImageCache的默认文件存储路径是由单例去控制的,在单例初始化的时候,传入了默认的文件夹名称default,这样的话,默认的文件路径就是../cache/default/com.hackemist.SDWebImageCache. default/ 文件夹。

用户自己定义的文件路径

SDImageCache还支持用户自己定义的命名空间,所需要做的就是在创建的时候传入namespace,即可,这样的话,假如用户传入了YoukuImages,那么 缓存的文件路径就是../cache/YoukuImages/com.hackemist.SDWebImageCache.YoukuImages/ 文件夹了。

辅助的文件路径方法

这个并不是很复杂,之所以要提一嘴,主要是比较赞赏SDWebImage的函数命名,代码如下

1
2
3
4
- (nullable NSString *)makeDiskCachePath:(nonnull NSString*)fullNamespace {
NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [paths[0] stringByAppendingPathComponent:fullNamespace];
}

图片存储路径

图片的存储路径采用了对URL进行MD5加密的方式,进行处理,通过这种方式防止文件被盗链给服务端增加流量损耗。
MD5也是很常见的加密方式,下面附上代码,方便自己以后查看。注意,这里稍微和别的MD5加密的不同的就是,会把文件扩展名带上,比如.png,.jpg之类的。这个方法,其实是提供了一个URL到文件名的映射

1
2
3
4
5
6
7
8
9
10
11
12
13
- (nullable NSString *)cachedFileNameForKey:(nullable NSString *)key {
const char *str = key.UTF8String;
if (str == NULL) {
str = "";
}
unsigned char r[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, (CC_LONG)strlen(str), r);
NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%@",
r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10],
r[11], r[12], r[13], r[14], r[15], [key.pathExtension isEqualToString:@""] ? @"" : [NSString stringWithFormat:@".%@", key.pathExtension]];

return filename;
}

另外提供俩个函数,方便获取文件默认存储路径下指定key图片的方法

1
2
3
4
5
// 获取指定路径下名称为key的图片路径
- (nullable NSString *)cachePathForKey:(nullable NSString *)key inPath:(nonnull NSString *)path {
NSString *filename = [self cachedFileNameForKey:key];
return [path stringByAppendingPathComponent:filename];
}
1
2
3
4
// 获取默认路径下key的图片路径,一般情况都是直接使用该方法就获取到图片的路径了
- (nullable NSString *)defaultCachePathForKey:(nullable NSString *)key {
return [self cachePathForKey:key inPath:self.diskCachePath];
}

SDImageCache的初始化

自从Xcode8之后,编译器好像做了一些优化,init方法需要去调用其他的NS_DESIGNATED_INITIALIZER标记的初始化方法,SDImageCache显然也是响应了这样的号召,所以提供了俩个自定义的初始化方法initWithNamespace:和initWithNamespace:diskCacheDirectory:,分别支持按照命名空间及存储路径的自定义初始化。但是单例的初始化会调用默认的初始化方法,使用单例的时候,会在App的Cache文件夹下面创建你一个default的文件夹,并在该Default文件夹下创建com.hackmisk.SDWebImageCache.default的一个文件夹,使用该文件夹来存储图片。下面是初始化的一些代码,并不是很复杂。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
+ (nonnull instancetype)sharedImageCache {
static dispatch_once_t once;
static id instance;
dispatch_once(&once, ^{
instance = [self new];
});
return instance;
}

- (instancetype)init {
return [self initWithNamespace:@"default"];
}

- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns {
NSString *path = [self makeDiskCachePath:ns];
return [self initWithNamespace:ns diskCacheDirectory:path];
}

- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
diskCacheDirectory:(nonnull NSString *)directory {
if ((self = [super init])) {
NSString *fullNamespace = [@"com.hackemist.SDWebImageCache." stringByAppendingString:ns];

// Create IO serial queue
_ioQueue = dispatch_queue_create("com.hackemist.SDWebImageCache", DISPATCH_QUEUE_SERIAL);

//目前这里使用的都是默认配置
_config = [[SDImageCacheConfig alloc] init];

// Init the memory cache
// 创建内存缓存
_memCache = [[AutoPurgeCache alloc] init];
_memCache.name = fullNamespace;

// Init the disk cache
if (directory != nil) {
// 单例形式创建的时候,这里肯定是能执行到的,之后就会创建com.hackmisk.SDWebImageCache.default的缓存路径
_diskCachePath = [directory stringByAppendingPathComponent:fullNamespace];
} else {
NSString *path = [self makeDiskCachePath:ns];
_diskCachePath = path;
}
// 此处需要稍微注意一下,因为io队列主要是对图片进行增删读写等操作,需要保证fileManager创建在io队列上。
dispatch_sync(_ioQueue, ^{
_fileManager = [NSFileManager new];
});

#if SD_UIKIT
// Subscribe to app events
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(clearMemory)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deleteOldFiles)
name:UIApplicationWillTerminateNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(backgroundDeleteOldFiles)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
#endif
}

return self;
}
//辅助方法,便捷式创建缓存路径
- (nullable NSString *)makeDiskCachePath:(nonnull NSString*)fullNamespace {
NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [paths[0] stringByAppendingPathComponent:fullNamespace];
}

SDImageCache的缓存操作

这里同样提供了三个方法方便使用者根据不同的需求进行图片存储功能的选择。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 使用默认的存储路径,并默认存储到磁盘
- (void)storeImage:(nullable UIImage *)image
forKey:(nullable NSString *)key
completion:(nullable SDWebImageNoParamsBlock)completionBlock {
[self storeImage:image imageData:nil forKey:key toDisk:YES completion:completionBlock];
}

// 该方法可供用户选择是否能够保存到磁盘
- (void)storeImage:(nullable UIImage *)image
forKey:(nullable NSString *)key
toDisk:(BOOL)toDisk
completion:(nullable SDWebImageNoParamsBlock)completionBlock {
[self storeImage:image imageData:nil forKey:key toDisk:toDisk completion:completionBlock];
}

// 此处提供了图片的NSdata数据,可供用户直接存储。
- (void)storeImage:(nullable UIImage *)image
imageData:(nullable NSData *)imageData
forKey:(nullable NSString *)key
toDisk:(BOOL)toDisk
completion:(nullable SDWebImageNoParamsBlock)completionBlock {
// 做下异常校验,如果图片或者key不存在,直接返回
if (!image || !key) {
if (completionBlock) {
completionBlock();
}
return;
}
// 检验下配置类是否允许将图片保存到内存,如果允许就进行保存处理
if (self.config.shouldCacheImagesInMemory) {
NSUInteger cost = SDCacheCostForImage(image);
[self.memCache setObject:image forKey:key cost:cost];
}
// 是否需要保存到磁盘
if (toDisk) {
// 保存需要在IO队列中进行,先检验图片NSdata是否存在,如果只有UIImage对象,需要先转换成Data类型
dispatch_async(self.ioQueue, ^{
NSData *data = imageData;

if (!data && image) {
SDImageFormat imageFormatFromData = [NSData sd_imageFormatForImageData:data];
data = [image sd_imageDataAsFormat:imageFormatFromData];
}

[self storeImageDataToDisk:data forKey:key];
if (completionBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock();
});
}
});
} else {
if (completionBlock) {
completionBlock();
}
}
}

// 保存数据到磁盘,注意保存操作要在IO队列上进行。并根据是否要保存到iCloud对URL进行设置
- (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key {
if (!imageData || !key) {
return;
}

// 检查下是否是在IO队列上,因为读写操作要在单独的异步队列进行
[self checkIfQueueIsIOQueue];

// 检查是否在当前的目录下存在文件夹,如不存在,先创建之
if (![_fileManager fileExistsAtPath:_diskCachePath]) {
[_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
}

// 获取缓存图片地址文件夹路径
NSString *cachePathForKey = [self defaultCachePathForKey:key];
// 转变为文件URL
NSURL *fileURL = [NSURL fileURLWithPath:cachePathForKey];

// 保存操作
[_fileManager createFileAtPath:cachePathForKey contents:imageData attributes:nil];

// 阻止图片保存到iCloud
if (self.config.shouldDisableiCloud) {
[fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
}
}

取&查

SDWebImageCache提供了查询内存图片、磁盘图片的功能,还提供了查询默认路径和自定义路径下某个key对应的图片。

  • 检测图片是否在磁盘中有缓存
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
        - (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock {
    dispatch_async(_ioQueue, ^{
    BOOL exists = [_fileManager fileExistsAtPath:[self defaultCachePathForKey:key]];

    // fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
    // checking the key with and without the extension
    if (!exists) {
    exists = [_fileManager fileExistsAtPath:[self defaultCachePathForKey:key].stringByDeletingPathExtension];
    }

    if (completionBlock) {
    dispatch_async(dispatch_get_main_queue(), ^{
    completionBlock(exists);
    });
    }
    });
    }
  • 获取内存/磁盘上缓存的图片
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
        - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key {
    return [self.memCache objectForKey:key];
    }
    - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key {
    UIImage *diskImage = [self diskImageForKey:key];
    if (diskImage && self.config.shouldCacheImagesInMemory) {
    NSUInteger cost = SDCacheCostForImage(diskImage);
    [self.memCache setObject:diskImage forKey:key cost:cost];
    }
    - (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key {
    // First check the in-memory cache...
    UIImage *image = [self imageFromMemoryCacheForKey:key];
    if (image) {
    return image;
    }

    // Second check the disk cache...
    image = [self imageFromDiskCacheForKey:key];

    return diskImage;
    }
  • 辅助方法,获取所有路径下是否有该key缓存的图片
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
        - (nullable NSData *)diskImageDataBySearchingAllPathsForKey:(nullable NSString *)key {
    NSString *defaultPath = [self defaultCachePathForKey:key];
    NSData *data = [NSData dataWithContentsOfFile:defaultPath];
    if (data) {
    return data;
    }

    // fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
    // checking the key with and without the extension
    data = [NSData dataWithContentsOfFile:defaultPath.stringByDeletingPathExtension];
    if (data) {
    return data;
    }

    NSArray<NSString *> *customPaths = [self.customPaths copy];
    for (NSString *path in customPaths) {
    NSString *filePath = [self cachePathForKey:key inPath:path];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    if (imageData) {
    return imageData;
    }

    // fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
    // checking the key with and without the extension
    imageData = [NSData dataWithContentsOfFile:filePath.stringByDeletingPathExtension];
    if (imageData) {
    return imageData;
    }
    }

    return nil;
    }
    - (nullable UIImage *)diskImageForKey:(nullable NSString *)key {
    NSData *data = [self diskImageDataBySearchingAllPathsForKey:key];
    if (data) {
    UIImage *image = [UIImage sd_imageWithData:data];
    image = [self scaledImageForKey:key image:image];
    if (self.config.shouldDecompressImages) {
    image = [UIImage decodedImageWithImage:image];
    }
    return image;
    }
    else {
    return nil;
    }
    }
  • 提供供外部调用的,且返回一个Operation对象的查询某Key的图片
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
        - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable SDCacheQueryCompletedBlock)doneBlock {
    if (!key) {
    if (doneBlock) {
    doneBlock(nil, nil, SDImageCacheTypeNone);
    }
    return nil;
    }

    // First check the in-memory cache...
    UIImage *image = [self imageFromMemoryCacheForKey:key];
    if (image) {
    NSData *diskData = nil;
    if ([image isGIF]) {
    diskData = [self diskImageDataBySearchingAllPathsForKey:key];
    }
    if (doneBlock) {
    doneBlock(image, diskData, SDImageCacheTypeMemory);
    }
    return nil;
    }

    NSOperation *operation = [NSOperation new];
    dispatch_async(self.ioQueue, ^{
    if (operation.isCancelled) {
    // do not call the completion if cancelled
    return;
    }

    @autoreleasepool {
    NSData *diskData = [self diskImageDataBySearchingAllPathsForKey:key];
    UIImage *diskImage = [self diskImageForKey:key];
    if (diskImage && self.config.shouldCacheImagesInMemory) {
    NSUInteger cost = SDCacheCostForImage(diskImage);
    [self.memCache setObject:diskImage forKey:key cost:cost];
    }

    if (doneBlock) {
    dispatch_async(dispatch_get_main_queue(), ^{
    doneBlock(diskImage, diskData, SDImageCacheTypeDisk);
    });
    }
    }
    });

    return operation;
    }

    同样,删除也包括删除内存中的图片和删除磁盘中的图片,俩种,比较简单,下面是源码,可以参考下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
         - (void)clearMemory {
    [self.memCache removeAllObjects];
    }

    - (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion {
    dispatch_async(self.ioQueue, ^{
    [_fileManager removeItemAtPath:self.diskCachePath error:nil];
    [_fileManager createDirectoryAtPath:self.diskCachePath
    withIntermediateDirectories:YES
    attributes:nil
    error:NULL];

    if (completion) {
    dispatch_async(dispatch_get_main_queue(), ^{
    completion();
    });
    }
    });
    }
    删除这块,唯一需要特别注意的就是删除过期的文件,以及对文件大小的存储上限做校验,如果超过最大上限,将最老的文件删除掉
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
        - (void)deleteOldFiles {
    [self deleteOldFilesWithCompletionBlock:nil];
    }

    - (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock {
    dispatch_async(self.ioQueue, ^{
    NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
    NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey];

    // This enumerator prefetches useful properties for our cache files.
    NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
    includingPropertiesForKeys:resourceKeys
    options:NSDirectoryEnumerationSkipsHiddenFiles
    errorHandler:NULL];

    NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxCacheAge];
    NSMutableDictionary<NSURL *, NSDictionary<NSString *, id> *> *cacheFiles = [NSMutableDictionary dictionary];
    NSUInteger currentCacheSize = 0;

    // Enumerate all of the files in the cache directory. This loop has two purposes:
    //
    // 1. Removing files that are older than the expiration date.
    // 2. Storing file attributes for the size-based cleanup pass.
    NSMutableArray<NSURL *> *urlsToDelete = [[NSMutableArray alloc] init];
    for (NSURL *fileURL in fileEnumerator) {
    NSError *error;
    NSDictionary<NSString *, id> *resourceValues = [fileURL resourceValuesForKeys:resourceKeys error:&error];

    // Skip directories and errors.
    if (error || !resourceValues || [resourceValues[NSURLIsDirectoryKey] boolValue]) {
    continue;
    }

    // Remove files that are older than the expiration date;
    NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey];
    if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
    [urlsToDelete addObject:fileURL];
    continue;
    }

    // Store a reference to this file and account for its total size.
    NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
    currentCacheSize += totalAllocatedSize.unsignedIntegerValue;
    cacheFiles[fileURL] = resourceValues;
    }

    for (NSURL *fileURL in urlsToDelete) {
    [_fileManager removeItemAtURL:fileURL error:nil];
    }

    // If our remaining disk cache exceeds a configured maximum size, perform a second
    // size-based cleanup pass. We delete the oldest files first.
    if (self.config.maxCacheSize > 0 && currentCacheSize > self.config.maxCacheSize) {
    // Target half of our maximum cache size for this cleanup pass.
    const NSUInteger desiredCacheSize = self.config.maxCacheSize / 2;

    // Sort the remaining cache files by their last modification time (oldest first).
    NSArray<NSURL *> *sortedFiles = [cacheFiles keysSortedByValueWithOptions:NSSortConcurrent
    usingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [obj1[NSURLContentModificationDateKey] compare:obj2[NSURLContentModificationDateKey]];
    }];

    // Delete files until we fall below our desired cache size.
    for (NSURL *fileURL in sortedFiles) {
    if ([_fileManager removeItemAtURL:fileURL error:nil]) {
    NSDictionary<NSString *, id> *resourceValues = cacheFiles[fileURL];
    NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
    currentCacheSize -= totalAllocatedSize.unsignedIntegerValue;

    if (currentCacheSize < desiredCacheSize) {
    break;
    }
    }
    }
    }
    if (completionBlock) {
    dispatch_async(dispatch_get_main_queue(), ^{
    completionBlock();
    });
    }
    });
    }
    对后台删除的支持,当APP在删除的时候,切换到后台,需要支持后台删除文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
        - (void)backgroundDeleteOldFiles {
    Class UIApplicationClass = NSClassFromString(@"UIApplication");
    if(!UIApplicationClass || ![UIApplicationClass respondsToSelector:@selector(sharedApplication)]) {
    return;
    }
    UIApplication *application = [UIApplication performSelector:@selector(sharedApplication)];
    __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
    // Clean up any unfinished task business by marking where you
    // stopped or ending the task outright.
    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    [self deleteOldFilesWithCompletionBlock:^{
    [application endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
    }];
    }

    缓存的信息

    除了支持存、查、取、删,之外,SDWebImageCache还提供了方便查看缓存的文件数、缓存的总大小、以及实时计算文件大小的block异步回调,代码如下
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
        - (NSUInteger)getSize {
    __block NSUInteger size = 0;
    dispatch_sync(self.ioQueue, ^{
    NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtPath:self.diskCachePath];
    for (NSString *fileName in fileEnumerator) {
    NSString *filePath = [self.diskCachePath stringByAppendingPathComponent:fileName];
    NSDictionary<NSString *, id> *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
    size += [attrs fileSize];
    }
    });
    return size;
    }

    - (NSUInteger)getDiskCount {
    __block NSUInteger count = 0;
    dispatch_sync(self.ioQueue, ^{
    NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtPath:self.diskCachePath];
    count = fileEnumerator.allObjects.count;
    });
    return count;
    }

    - (void)calculateSizeWithCompletionBlock:(nullable SDWebImageCalculateSizeBlock)completionBlock {
    NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];

    dispatch_async(self.ioQueue, ^{
    NSUInteger fileCount = 0;
    NSUInteger totalSize = 0;

    NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
    includingPropertiesForKeys:@[NSFileSize]
    options:NSDirectoryEnumerationSkipsHiddenFiles
    errorHandler:NULL];

    for (NSURL *fileURL in fileEnumerator) {
    NSNumber *fileSize;
    [fileURL getResourceValue:&fileSize forKey:NSURLFileSizeKey error:NULL];
    totalSize += fileSize.unsignedIntegerValue;
    fileCount += 1;
    }

    if (completionBlock) {
    dispatch_async(dispatch_get_main_queue(), ^{
    completionBlock(fileCount, totalSize);
    });
    }
    });
    }

    小结

    通过这一小节对源码的分析,我们大概知道了SDWebImage的缓存工作原理。也对文件操作有了一个更深刻的理解,下一小节将研究下SDWebImageManager是如果对下载和缓存进行综合管理的。