Use the more intuitive and suitable cache path for SDImageCache. Defaults now to `~Library/Caches/com.hackemist.SDImageCache/default/`, namespace controls the sub-directory name.

Remove the namespacePrefix property because now it's not follows what it says
This commit is contained in:
DreamPiggy 2018-11-17 13:09:11 +08:00
parent b15abdd792
commit b68730bedf
5 changed files with 23 additions and 34 deletions

View File

@ -87,23 +87,25 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns;
/**
* Init a new cache store with a specific namespace and directory
* Init a new cache store with a specific namespace and directory.
* If you don't provide the disk cache directory, we will use the User Cache directory with prefix (~/Library/Caches/com.hackemist.SDImageCache/).
*
* @param ns The namespace to use for this cache store
* @param directory Directory to cache disk images in
*/
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
diskCacheDirectory:(nonnull NSString *)directory;
diskCacheDirectory:(nullable NSString *)directory;
/**
* Init a new cache store with a specific namespace, directory and file manager
* The final disk cache directory should looks like ($directory/$namespace). And the default config of shared cache, should result in (~/Library/Caches/com.hackemist.SDImageCache/default/)
*
* @param ns The namespace to use for this cache store
* @param directory Directory to cache disk images in
* @param config The cache config to be used to create the cache. You can provide custom memory cache or disk cache class in the cache config
*/
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
diskCacheDirectory:(nonnull NSString *)directory
diskCacheDirectory:(nullable NSString *)directory
config:(nullable SDImageCacheConfig *)config NS_DESIGNATED_INITIALIZER;
#pragma mark - Cache paths

View File

@ -45,24 +45,19 @@
}
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns {
NSString *path = [self makeDiskCachePath:ns];
return [self initWithNamespace:ns diskCacheDirectory:path];
return [self initWithNamespace:ns diskCacheDirectory:nil];
}
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
diskCacheDirectory:(nonnull NSString *)directory {
diskCacheDirectory:(nullable NSString *)directory {
return [self initWithNamespace:ns diskCacheDirectory:directory config:SDImageCacheConfig.defaultCacheConfig];
}
- (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
diskCacheDirectory:(nonnull NSString *)directory
diskCacheDirectory:(nullable NSString *)directory
config:(nullable SDImageCacheConfig *)config {
if ((self = [super init])) {
NSString *namespacePrefix = config.namespacePrefix;
if (!namespacePrefix) {
namespacePrefix = @"";
}
NSString *fullNamespace = [namespacePrefix stringByAppendingString:ns];
NSAssert(ns, @"Cache namespace should not be nil");
// Create IO serial queue
_ioQueue = dispatch_queue_create("com.hackemist.SDImageCache", DISPATCH_QUEUE_SERIAL);
@ -78,9 +73,9 @@
// Init the disk cache
if (directory != nil) {
_diskCachePath = [directory stringByAppendingPathComponent:fullNamespace];
_diskCachePath = [directory stringByAppendingPathComponent:ns];
} else {
NSString *path = [self makeDiskCachePath:ns];
NSString *path = [[[self userCacheDirectory] stringByAppendingPathComponent:@"com.hackemist.SDImageCache"] stringByAppendingPathComponent:ns];
_diskCachePath = path;
}
@ -126,17 +121,19 @@
return [self.diskCache cachePathForKey:key];
}
- (nullable NSString *)makeDiskCachePath:(nonnull NSString *)fullNamespace {
- (nullable NSString *)userCacheDirectory {
NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [paths.firstObject stringByAppendingPathComponent:fullNamespace];
return paths.firstObject;
}
- (void)migrateDiskCacheDirectory {
if ([self.diskCache isKindOfClass:[SDDiskCache class]]) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *newDefaultPath = [[self makeDiskCachePath:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDImageCache.default"];
NSString *oldDefaultPath = [[self makeDiskCachePath:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
// ~/Library/Caches/com.hackemist.SDImageCache/default/
NSString *newDefaultPath = [[[self userCacheDirectory] stringByAppendingPathComponent:@"com.hackemist.SDImageCache"] stringByAppendingPathComponent:@"default"];
// ~/Library/Caches/default/com.hackemist.SDImageCache.default/
NSString *oldDefaultPath = [[[self userCacheDirectory] stringByAppendingPathComponent:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
dispatch_async(self.ioQueue, ^{
[((SDDiskCache *)self.diskCache) moveCacheDirectoryFromPath:oldDefaultPath toPath:newDefaultPath];
});

View File

@ -99,12 +99,6 @@ typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) {
*/
@property (assign, nonatomic) SDImageCacheConfigExpireType diskCacheExpireType;
/**
* The namespace prefix of cache. It's used to prefix the namespace you provide to the caches's initializer. You 'd better name it with reverse domain name notation and keep the final dot.
* Defautls to `com.hackemist.SDImageCache.`, which will prefix your namespace such as `default` to final `com.hackemist.SDImageCache.default`. If you specify nil, it will be treated equals to an empty string.
*/
@property (copy, nonatomic, nullable) NSString *namespacePrefix;
/**
* The custom file manager for disk cache. Pass nil to let disk cache choose the proper file manager.
* Defaults to nil.

View File

@ -34,7 +34,6 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
_maxCacheAge = kDefaultCacheMaxCacheAge;
_maxCacheSize = 0;
_diskCacheExpireType = SDImageCacheConfigExpireTypeModificationDate;
_namespacePrefix = @"com.hackemist.SDImageCache.";
_memoryCacheClass = [SDMemoryCache class];
_diskCacheClass = [SDDiskCache class];
}
@ -54,7 +53,6 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
config.maxMemoryCost = self.maxMemoryCost;
config.maxMemoryCount = self.maxMemoryCount;
config.diskCacheExpireType = self.diskCacheExpireType;
config.namespacePrefix = self.namespacePrefix;
config.fileManager = self.fileManager; // NSFileManager does not conform to NSCopying, just pass the reference
config.memoryCacheClass = self.memoryCacheClass;
config.diskCacheClass = self.diskCacheClass;

View File

@ -356,8 +356,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
SDImageCacheConfig *config = [[SDImageCacheConfig alloc] init];
config.memoryCacheClass = [SDWebImageTestMemoryCache class];
NSString *nameSpace = @"SDWebImageTestMemoryCache";
NSString *cacheDictionary = [self makeDiskCachePath:nameSpace];
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:nameSpace diskCacheDirectory:cacheDictionary config:config];
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:nameSpace diskCacheDirectory:nil config:config];
SDWebImageTestMemoryCache *memCache = cache.memCache;
expect([memCache isKindOfClass:[SDWebImageTestMemoryCache class]]).to.beTruthy();
}
@ -366,8 +365,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
SDImageCacheConfig *config = [[SDImageCacheConfig alloc] init];
config.diskCacheClass = [SDWebImageTestDiskCache class];
NSString *nameSpace = @"SDWebImageTestDiskCache";
NSString *cacheDictionary = [self makeDiskCachePath:nameSpace];
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:nameSpace diskCacheDirectory:cacheDictionary config:config];
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:nameSpace diskCacheDirectory:nil config:config];
SDWebImageTestDiskCache *diskCache = cache.diskCache;
expect([diskCache isKindOfClass:[SDWebImageTestDiskCache class]]).to.beTruthy();
}
@ -378,8 +376,8 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
config.fileManager = fileManager;
// Fake to store a.png into old path
NSString *newDefaultPath = [[self makeDiskCachePath:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDImageCache.default"];
NSString *oldDefaultPath = [[self makeDiskCachePath:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
NSString *newDefaultPath = [[[self userCacheDirectory] stringByAppendingPathComponent:@"com.hackemist.SDImageCache"] stringByAppendingPathComponent:@"default"];
NSString *oldDefaultPath = [[[self userCacheDirectory] stringByAppendingPathComponent:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
[fileManager createDirectoryAtPath:oldDefaultPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager createFileAtPath:[oldDefaultPath stringByAppendingPathComponent:@"a.png"] contents:[NSData dataWithContentsOfFile:[self testPNGPath]] attributes:nil];
// Call migration
@ -609,9 +607,9 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
return [testBundle pathForResource:@"TestImage" ofType:@"png"];
}
- (nullable NSString *)makeDiskCachePath:(nonnull NSString*)fullNamespace {
- (nullable NSString *)userCacheDirectory {
NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [paths[0] stringByAppendingPathComponent:fullNamespace];
return paths.firstObject;
}
@end