Use global low prio queue instead of a dedicated queue for caching I/Os

This commit is contained in:
Olivier Poitrey 2012-11-06 03:01:16 +01:00
parent a8a9b12896
commit 13210a6925
1 changed files with 6 additions and 17 deletions

View File

@ -13,7 +13,6 @@
#import <mach/mach.h> #import <mach/mach.h>
#import <mach/mach_host.h> #import <mach/mach_host.h>
const char *kDiskIOQueueName = "com.hackemist.SDWebImageDiskCache";
static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
@interface SDImageCache () @interface SDImageCache ()
@ -116,8 +115,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
if (toDisk) if (toDisk)
{ {
dispatch_queue_t queue = dispatch_queue_create(kDiskIOQueueName, nil); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
dispatch_async(queue, ^
{ {
NSData *data = imageData; NSData *data = imageData;
@ -146,7 +144,6 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
[fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil]; [fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
} }
}); });
dispatch_release(queue);
} }
} }
@ -179,9 +176,8 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
} }
NSString *path = [self cachePathForKey:key]; NSString *path = [self cachePathForKey:key];
dispatch_queue_t queue = dispatch_queue_create(kDiskIOQueueName, nil); dispatch_io_t ioChannel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path.UTF8String, O_RDONLY, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), nil);
dispatch_io_t ioChannel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path.UTF8String, O_RDONLY, 0, queue, nil); dispatch_io_read(ioChannel, 0, SIZE_MAX, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(bool done, dispatch_data_t dispatchedData, int error)
dispatch_io_read(ioChannel, 0, SIZE_MAX, queue, ^(bool done, dispatch_data_t dispatchedData, int error)
{ {
if (error) if (error)
{ {
@ -214,7 +210,6 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
}); });
}); });
dispatch_release(queue);
dispatch_release(ioChannel); dispatch_release(ioChannel);
} }
@ -235,12 +230,10 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
if (fromDisk) if (fromDisk)
{ {
dispatch_queue_t queue = dispatch_queue_create(kDiskIOQueueName, nil); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
dispatch_async(queue, ^
{ {
[[NSFileManager defaultManager] removeItemAtPath:[self cachePathForKey:key] error:nil]; [[NSFileManager defaultManager] removeItemAtPath:[self cachePathForKey:key] error:nil];
}); });
dispatch_release(queue);
} }
} }
@ -251,8 +244,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
- (void)clearDisk - (void)clearDisk
{ {
dispatch_queue_t queue = dispatch_queue_create(kDiskIOQueueName, nil); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
dispatch_async(queue, ^
{ {
[[NSFileManager defaultManager] removeItemAtPath:self.diskCachePath error:nil]; [[NSFileManager defaultManager] removeItemAtPath:self.diskCachePath error:nil];
[[NSFileManager defaultManager] createDirectoryAtPath:self.diskCachePath [[NSFileManager defaultManager] createDirectoryAtPath:self.diskCachePath
@ -260,13 +252,11 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
attributes:nil attributes:nil
error:NULL]; error:NULL];
}); });
dispatch_release(queue);
} }
- (void)cleanDisk - (void)cleanDisk
{ {
dispatch_queue_t queue = dispatch_queue_create(kDiskIOQueueName, nil); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
dispatch_async(queue, ^
{ {
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.maxCacheAge]; NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.maxCacheAge];
NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:self.diskCachePath]; NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:self.diskCachePath];
@ -280,7 +270,6 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
} }
} }
}); });
dispatch_release(queue);
} }
-(int)getSize -(int)getSize