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
|
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.
|
Add a new cache to the end of caches array. Which has the highest priority.
|
||||||
|
|
|
@ -17,6 +17,9 @@
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation SDImageCachesManager
|
@implementation SDImageCachesManager
|
||||||
|
{
|
||||||
|
NSMutableArray<id<SDImageCache>> *caches_;
|
||||||
|
}
|
||||||
|
|
||||||
+ (SDImageCachesManager *)sharedManager {
|
+ (SDImageCachesManager *)sharedManager {
|
||||||
static dispatch_once_t onceToken;
|
static dispatch_once_t onceToken;
|
||||||
|
@ -36,12 +39,28 @@
|
||||||
self.containsOperationPolicy = SDImageCachesManagerOperationPolicySerial;
|
self.containsOperationPolicy = SDImageCachesManagerOperationPolicySerial;
|
||||||
self.clearOperationPolicy = SDImageCachesManagerOperationPolicyConcurrent;
|
self.clearOperationPolicy = SDImageCachesManagerOperationPolicyConcurrent;
|
||||||
// initialize with default image caches
|
// initialize with default image caches
|
||||||
_caches = @[[SDImageCache sharedImageCache]];
|
caches_ = [NSMutableArray arrayWithObject:[SDImageCache sharedImageCache]];
|
||||||
_cachesLock = dispatch_semaphore_create(1);
|
_cachesLock = dispatch_semaphore_create(1);
|
||||||
}
|
}
|
||||||
return self;
|
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
|
#pragma mark - Cache IO operations
|
||||||
|
|
||||||
- (void)addCache:(id<SDImageCache>)cache {
|
- (void)addCache:(id<SDImageCache>)cache {
|
||||||
|
@ -49,12 +68,7 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SD_LOCK(self.cachesLock);
|
SD_LOCK(self.cachesLock);
|
||||||
NSMutableArray<id<SDImageCache>> *mutableCaches = [self.caches mutableCopy];
|
[caches_ addObject:cache];
|
||||||
if (!mutableCaches) {
|
|
||||||
mutableCaches = [NSMutableArray array];
|
|
||||||
}
|
|
||||||
[mutableCaches addObject:cache];
|
|
||||||
self.caches = [mutableCaches copy];
|
|
||||||
SD_UNLOCK(self.cachesLock);
|
SD_UNLOCK(self.cachesLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,9 +77,7 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SD_LOCK(self.cachesLock);
|
SD_LOCK(self.cachesLock);
|
||||||
NSMutableArray<id<SDImageCache>> *mutableCaches = [self.caches mutableCopy];
|
[caches_ removeObject:cache];
|
||||||
[mutableCaches removeObject:cache];
|
|
||||||
self.caches = [mutableCaches copy];
|
|
||||||
SD_UNLOCK(self.cachesLock);
|
SD_UNLOCK(self.cachesLock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,9 +87,7 @@
|
||||||
if (!key) {
|
if (!key) {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
SD_LOCK(self.cachesLock);
|
|
||||||
NSArray<id<SDImageCache>> *caches = self.caches;
|
NSArray<id<SDImageCache>> *caches = self.caches;
|
||||||
SD_UNLOCK(self.cachesLock);
|
|
||||||
NSUInteger count = caches.count;
|
NSUInteger count = caches.count;
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
return nil;
|
return nil;
|
||||||
|
@ -119,9 +129,7 @@
|
||||||
if (!key) {
|
if (!key) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SD_LOCK(self.cachesLock);
|
|
||||||
NSArray<id<SDImageCache>> *caches = self.caches;
|
NSArray<id<SDImageCache>> *caches = self.caches;
|
||||||
SD_UNLOCK(self.cachesLock);
|
|
||||||
NSUInteger count = caches.count;
|
NSUInteger count = caches.count;
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
return;
|
return;
|
||||||
|
@ -159,9 +167,7 @@
|
||||||
if (!key) {
|
if (!key) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SD_LOCK(self.cachesLock);
|
|
||||||
NSArray<id<SDImageCache>> *caches = self.caches;
|
NSArray<id<SDImageCache>> *caches = self.caches;
|
||||||
SD_UNLOCK(self.cachesLock);
|
|
||||||
NSUInteger count = caches.count;
|
NSUInteger count = caches.count;
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
return;
|
return;
|
||||||
|
@ -199,9 +205,7 @@
|
||||||
if (!key) {
|
if (!key) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SD_LOCK(self.cachesLock);
|
|
||||||
NSArray<id<SDImageCache>> *caches = self.caches;
|
NSArray<id<SDImageCache>> *caches = self.caches;
|
||||||
SD_UNLOCK(self.cachesLock);
|
|
||||||
NSUInteger count = caches.count;
|
NSUInteger count = caches.count;
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
return;
|
return;
|
||||||
|
@ -238,9 +242,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)clearWithCacheType:(SDImageCacheType)cacheType completion:(SDWebImageNoParamsBlock)completionBlock {
|
- (void)clearWithCacheType:(SDImageCacheType)cacheType completion:(SDWebImageNoParamsBlock)completionBlock {
|
||||||
SD_LOCK(self.cachesLock);
|
|
||||||
NSArray<id<SDImageCache>> *caches = self.caches;
|
NSArray<id<SDImageCache>> *caches = self.caches;
|
||||||
SD_UNLOCK(self.cachesLock);
|
|
||||||
NSUInteger count = caches.count;
|
NSUInteger count = caches.count;
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
return;
|
return;
|
||||||
|
|
Loading…
Reference in New Issue