Guarantee caches of CachesManager thread-safe
This commit is contained in:
parent
1faab6c863
commit
aa890c5d9a
|
@ -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<id<SDImageCache>> *caches;
|
||||
@property (nonatomic, copy, nullable) NSArray<id<SDImageCache>> *caches;
|
||||
|
||||
/**
|
||||
Add a new cache to the end of caches array. Which has the highest priority.
|
||||
|
|
|
@ -17,6 +17,9 @@
|
|||
@end
|
||||
|
||||
@implementation SDImageCachesManager
|
||||
{
|
||||
NSMutableArray<id<SDImageCache>> *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<id<SDImageCache>> *)caches {
|
||||
SD_LOCK(self.cachesLock);
|
||||
NSArray<id<SDImageCache>> *caches = [caches_ copy];
|
||||
SD_UNLOCK(self.cachesLock);
|
||||
return caches;
|
||||
}
|
||||
|
||||
- (void)setCaches:(NSArray<id<SDImageCache>> *)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<SDImageCache>)cache {
|
||||
|
@ -49,12 +68,7 @@
|
|||
return;
|
||||
}
|
||||
SD_LOCK(self.cachesLock);
|
||||
NSMutableArray<id<SDImageCache>> *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<id<SDImageCache>> *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<id<SDImageCache>> *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<id<SDImageCache>> *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<id<SDImageCache>> *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<id<SDImageCache>> *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<id<SDImageCache>> *caches = self.caches;
|
||||
SD_UNLOCK(self.cachesLock);
|
||||
NSUInteger count = caches.count;
|
||||
if (count == 0) {
|
||||
return;
|
||||
|
|
Loading…
Reference in New Issue