From aa890c5d9a8966db320396c64037687530b6cae1 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 1 Apr 2019 16:00:13 +0800 Subject: [PATCH] Guarantee caches of CachesManager thread-safe --- SDWebImage/SDImageCachesManager.h | 2 +- SDWebImage/SDImageCachesManager.m | 42 ++++++++++++++++--------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/SDWebImage/SDImageCachesManager.h b/SDWebImage/SDImageCachesManager.h index 2c0d7b03..db6a9a43 100644 --- a/SDWebImage/SDImageCachesManager.h +++ b/SDWebImage/SDImageCachesManager.h @@ -58,7 +58,7 @@ typedef NS_ENUM(NSUInteger, SDImageCachesManagerOperationPolicy) { /** All caches in caches manager. The caches array is a priority queue, which means the later added cache will have the highest priority */ -@property (nonatomic, copy, readwrite, nullable) NSArray> *caches; +@property (nonatomic, copy, nullable) NSArray> *caches; /** Add a new cache to the end of caches array. Which has the highest priority. diff --git a/SDWebImage/SDImageCachesManager.m b/SDWebImage/SDImageCachesManager.m index 52694704..18657853 100644 --- a/SDWebImage/SDImageCachesManager.m +++ b/SDWebImage/SDImageCachesManager.m @@ -17,6 +17,9 @@ @end @implementation SDImageCachesManager +{ + NSMutableArray> *caches_; +} + (SDImageCachesManager *)sharedManager { static dispatch_once_t onceToken; @@ -36,12 +39,28 @@ self.containsOperationPolicy = SDImageCachesManagerOperationPolicySerial; self.clearOperationPolicy = SDImageCachesManagerOperationPolicyConcurrent; // initialize with default image caches - _caches = @[[SDImageCache sharedImageCache]]; + caches_ = [NSMutableArray arrayWithObject:[SDImageCache sharedImageCache]]; _cachesLock = dispatch_semaphore_create(1); } return self; } +- (NSArray> *)caches { + SD_LOCK(self.cachesLock); + NSArray> *caches = [caches_ copy]; + SD_UNLOCK(self.cachesLock); + return caches; +} + +- (void)setCaches:(NSArray> *)caches { + SD_LOCK(self.cachesLock); + [caches_ removeAllObjects]; + if (caches.count) { + [caches_ addObjectsFromArray:caches]; + } + SD_UNLOCK(self.cachesLock); +} + #pragma mark - Cache IO operations - (void)addCache:(id)cache { @@ -49,12 +68,7 @@ return; } SD_LOCK(self.cachesLock); - NSMutableArray> *mutableCaches = [self.caches mutableCopy]; - if (!mutableCaches) { - mutableCaches = [NSMutableArray array]; - } - [mutableCaches addObject:cache]; - self.caches = [mutableCaches copy]; + [caches_ addObject:cache]; SD_UNLOCK(self.cachesLock); } @@ -63,9 +77,7 @@ return; } SD_LOCK(self.cachesLock); - NSMutableArray> *mutableCaches = [self.caches mutableCopy]; - [mutableCaches removeObject:cache]; - self.caches = [mutableCaches copy]; + [caches_ removeObject:cache]; SD_UNLOCK(self.cachesLock); } @@ -75,9 +87,7 @@ if (!key) { return nil; } - SD_LOCK(self.cachesLock); NSArray> *caches = self.caches; - SD_UNLOCK(self.cachesLock); NSUInteger count = caches.count; if (count == 0) { return nil; @@ -119,9 +129,7 @@ if (!key) { return; } - SD_LOCK(self.cachesLock); NSArray> *caches = self.caches; - SD_UNLOCK(self.cachesLock); NSUInteger count = caches.count; if (count == 0) { return; @@ -159,9 +167,7 @@ if (!key) { return; } - SD_LOCK(self.cachesLock); NSArray> *caches = self.caches; - SD_UNLOCK(self.cachesLock); NSUInteger count = caches.count; if (count == 0) { return; @@ -199,9 +205,7 @@ if (!key) { return; } - SD_LOCK(self.cachesLock); NSArray> *caches = self.caches; - SD_UNLOCK(self.cachesLock); NSUInteger count = caches.count; if (count == 0) { return; @@ -238,9 +242,7 @@ } - (void)clearWithCacheType:(SDImageCacheType)cacheType completion:(SDWebImageNoParamsBlock)completionBlock { - SD_LOCK(self.cachesLock); NSArray> *caches = self.caches; - SD_UNLOCK(self.cachesLock); NSUInteger count = caches.count; if (count == 0) { return;