Merge pull request #3460 from dreampiggy/feat/ioqueue_concurrent_config

Added `ioQueueAttributes` to use concurrent or control QoS for image cache internal IO Queue
This commit is contained in:
DreamPiggy 2022-12-27 17:57:09 +08:00 committed by GitHub
commit 7bc087b081
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 3 deletions

View File

@ -109,14 +109,16 @@ static NSString * _defaultDiskCacheDirectory;
if ((self = [super init])) {
NSAssert(ns, @"Cache namespace should not be nil");
// Create IO serial queue
_ioQueue = dispatch_queue_create("com.hackemist.SDImageCache", DISPATCH_QUEUE_SERIAL);
if (!config) {
config = SDImageCacheConfig.defaultCacheConfig;
}
_config = [config copy];
// Create IO queue
dispatch_queue_attr_t ioQueueAttributes = _config.ioQueueAttributes;
_ioQueue = dispatch_queue_create("com.hackemist.SDImageCache", ioQueueAttributes);
NSAssert(_ioQueue, @"The IO queue should not be nil. Your configured `ioQueueAttributes` may be wrong");
// Init the memory cache
NSAssert([config.memoryCacheClass conformsToProtocol:@protocol(SDMemoryCache)], @"Custom memory cache class must conform to `SDMemoryCache` protocol");
_memoryCache = [[config.memoryCacheClass alloc] initWithConfig:_config];

View File

@ -127,6 +127,15 @@ typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) {
*/
@property (strong, nonatomic, nullable) NSFileManager *fileManager;
/**
* The dispatch queue attr for ioQueue. You can config the QoS and concurrent/serial to internal IO queue. The ioQueue is used by SDImageCache to access read/write for disk data.
* Defaults we use `DISPATCH_QUEUE_SERIAL`(NULL), to use serial dispatch queue to ensure single access for disk data. It's safe but may be slow.
* @note You can override this to use `DISPATCH_QUEUE_CONCURRENT`, use concurrent queue.
* @warning **MAKE SURE** to keep `diskCacheWritingOptions` to use `NSDataWritingAtomic`, or concurrent queue may cause corrupted disk data (because multiple threads read/write same file without atomic is not IO-safe).
* @note This value does not support dynamic changes. Which means further modification on this value after cache initialized has no effect.
*/
@property (strong, nonatomic, nullable) dispatch_queue_attr_t ioQueueAttributes;
/**
* The custom memory cache class. Provided class instance must conform to `SDMemoryCache` protocol to allow usage.
* Defaults to built-in `SDMemoryCache` class.

View File

@ -35,6 +35,8 @@ static const NSInteger kDefaultCacheMaxDiskAge = 60 * 60 * 24 * 7; // 1 week
_maxDiskAge = kDefaultCacheMaxDiskAge;
_maxDiskSize = 0;
_diskCacheExpireType = SDImageCacheConfigExpireTypeModificationDate;
_fileManager = nil;
_ioQueueAttributes = DISPATCH_QUEUE_SERIAL; // NULL
_memoryCacheClass = [SDMemoryCache class];
_diskCacheClass = [SDDiskCache class];
}
@ -56,6 +58,7 @@ static const NSInteger kDefaultCacheMaxDiskAge = 60 * 60 * 24 * 7; // 1 week
config.maxMemoryCount = self.maxMemoryCount;
config.diskCacheExpireType = self.diskCacheExpireType;
config.fileManager = self.fileManager; // NSFileManager does not conform to NSCopying, just pass the reference
config.ioQueueAttributes = self.ioQueueAttributes; // Pass the reference
config.memoryCacheClass = self.memoryCacheClass;
config.diskCacheClass = self.diskCacheClass;

View File

@ -650,6 +650,27 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
[self waitForExpectationsWithCommonTimeout];
}
- (void)test48CacheUseConcurrentIOQueue {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache concurrent ioQueue"];
expectation.expectedFulfillmentCount = 2;
SDImageCacheConfig *config = [SDImageCacheConfig new];
dispatch_queue_attr_t attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_CONCURRENT, QOS_CLASS_BACKGROUND, 0);
config.ioQueueAttributes = attr;
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:@"Concurrent" diskCacheDirectory:@"/" config:config];
NSData *pngData = [NSData dataWithContentsOfFile:[self testPNGPath]];
[cache queryCacheOperationForKey:@"Key1" done:^(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType) {
expect(data).beNil();
[expectation fulfill];
}];
[cache storeImageData:pngData forKey:@"Key1" completion:^{
[expectation fulfill];
}];
[self waitForExpectationsWithCommonTimeout];
}
#pragma mark - SDImageCache & SDImageCachesManager
- (void)test49SDImageCacheQueryOp {
XCTestExpectation *expectation = [self expectationWithDescription:@"SDImageCache query op works"];