From b68730bedfe16fa56ed5f6fe8a1d6ee6da51dbc7 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 17 Nov 2018 13:09:11 +0800 Subject: [PATCH 1/4] 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 --- SDWebImage/SDImageCache.h | 8 +++++--- SDWebImage/SDImageCache.m | 27 ++++++++++++--------------- SDWebImage/SDImageCacheConfig.h | 6 ------ SDWebImage/SDImageCacheConfig.m | 2 -- Tests/Tests/SDImageCacheTests.m | 14 ++++++-------- 5 files changed, 23 insertions(+), 34 deletions(-) diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index bf8dfc6d..558ead6a 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -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 diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 6a093511..db5a02ea 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -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 *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]; }); diff --git a/SDWebImage/SDImageCacheConfig.h b/SDWebImage/SDImageCacheConfig.h index d7f86107..24f47c45 100644 --- a/SDWebImage/SDImageCacheConfig.h +++ b/SDWebImage/SDImageCacheConfig.h @@ -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. diff --git a/SDWebImage/SDImageCacheConfig.m b/SDWebImage/SDImageCacheConfig.m index 99a035f8..085e2d79 100644 --- a/SDWebImage/SDImageCacheConfig.m +++ b/SDWebImage/SDImageCacheConfig.m @@ -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; diff --git a/Tests/Tests/SDImageCacheTests.m b/Tests/Tests/SDImageCacheTests.m index b2bdd77c..dda01837 100644 --- a/Tests/Tests/SDImageCacheTests.m +++ b/Tests/Tests/SDImageCacheTests.m @@ -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 *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - return [paths[0] stringByAppendingPathComponent:fullNamespace]; + return paths.firstObject; } @end From 1fc1de5da93e88f061c5c3f60b278048ff789152 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 17 Nov 2018 13:18:45 +0800 Subject: [PATCH 2/4] Update the migration guide for cache path --- Docs/SDWebImage-5.0-Migration-guide.md | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/Docs/SDWebImage-5.0-Migration-guide.md b/Docs/SDWebImage-5.0-Migration-guide.md index e684823e..96a6038c 100644 --- a/Docs/SDWebImage-5.0-Migration-guide.md +++ b/Docs/SDWebImage-5.0-Migration-guide.md @@ -62,21 +62,9 @@ By taking the advantage of the [Custom Loader](https://github.com/rs/SDWebImage/ #### Cache -`SDImageCache` in 5.x, use `~/Library/com.hackemist.SDImageCache.default` as default cache path. However, 4.x use `~/Library/com.hackemist.SDWebImageCache.default`. If you have images in cache, it may be lost during migration. +`SDImageCache` in 5.x, use `~/Library/Caches/com.hackemist.SDImageCache/default/` as default cache path. However, 4.x use `~/Library/Caches/default/com.hackemist.SDWebImageCache.default/`. And don't be worried, we will do the migration automatically once the shared cache initialized. -If you still want to keep the 4.x default cache path, you can custom the cache path prefix using `namespacePrefix` property. - -+ Objective-C - -```objective-c -SDImageCacheConfig.defaultCacheConfig.namespacePrefix = @"com.hackemist.SDWebImageCache."; -``` - -+ Swift - -```swift -SDImageCacheConfig.`default`.namespacePrefix = "com.hackemist.SDWebImageCache." -``` +However, if you have some other custom namespace cache instance, you should try to do migration by your own. But typically, since the cache is designed to be invalid at any time, you'd better not to bind some important logic related on that cache path changes. #### Prefetcher From 95dba7307d20708cc2745e43e7f172fff85a5f5c Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 10 Dec 2018 13:26:32 +0800 Subject: [PATCH 3/4] Update the migration guide for cache path about 5.0.0-beta version --- Docs/SDWebImage-5.0-Migration-guide.md | 2 ++ SDWebImage/SDImageCache.m | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Docs/SDWebImage-5.0-Migration-guide.md b/Docs/SDWebImage-5.0-Migration-guide.md index 96a6038c..dee4e681 100644 --- a/Docs/SDWebImage-5.0-Migration-guide.md +++ b/Docs/SDWebImage-5.0-Migration-guide.md @@ -66,6 +66,8 @@ By taking the advantage of the [Custom Loader](https://github.com/rs/SDWebImage/ However, if you have some other custom namespace cache instance, you should try to do migration by your own. But typically, since the cache is designed to be invalid at any time, you'd better not to bind some important logic related on that cache path changes. +And, if you're previously using any version from `5.0.0-beta` to `5.0.0-beta3`, please note that the cache folder is been temporarily moved to `~/Library/Caches/default/com.hackemist.SDImageCache.default/`, however, the final release version of 5.0.0 use the path above. If you upgrade from those beta version, you may need manually do migration, check `+[SDDiskCache moveCacheDirectoryFromPath:toPath:]` for detail information. + #### Prefetcher `SDWebImagePrefetcher` in 5.x, change the concept of fetching batch of URLs. Now, each time you call `prefetchURLs:`, you will get a token which represents the specified URLs list. It does not cancel the previous URLs which is prefetching, which make the shared prefetcher behaves more intuitively. diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index db5a02ea..4007ce1d 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -132,7 +132,7 @@ dispatch_once(&onceToken, ^{ // ~/Library/Caches/com.hackemist.SDImageCache/default/ NSString *newDefaultPath = [[[self userCacheDirectory] stringByAppendingPathComponent:@"com.hackemist.SDImageCache"] stringByAppendingPathComponent:@"default"]; - // ~/Library/Caches/default/com.hackemist.SDImageCache.default/ + // ~/Library/Caches/default/com.hackemist.SDWebImageCache.default/ NSString *oldDefaultPath = [[[self userCacheDirectory] stringByAppendingPathComponent:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"]; dispatch_async(self.ioQueue, ^{ [((SDDiskCache *)self.diskCache) moveCacheDirectoryFromPath:oldDefaultPath toPath:newDefaultPath]; From 1d540d3cb7c3f368bdde6db308914b3d19aff923 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 10 Dec 2018 20:34:10 +0800 Subject: [PATCH 4/4] Fix the typo in the 5.0 migration guide --- Docs/SDWebImage-5.0-Migration-guide.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Docs/SDWebImage-5.0-Migration-guide.md b/Docs/SDWebImage-5.0-Migration-guide.md index dee4e681..c9d07cdf 100644 --- a/Docs/SDWebImage-5.0-Migration-guide.md +++ b/Docs/SDWebImage-5.0-Migration-guide.md @@ -64,9 +64,9 @@ By taking the advantage of the [Custom Loader](https://github.com/rs/SDWebImage/ `SDImageCache` in 5.x, use `~/Library/Caches/com.hackemist.SDImageCache/default/` as default cache path. However, 4.x use `~/Library/Caches/default/com.hackemist.SDWebImageCache.default/`. And don't be worried, we will do the migration automatically once the shared cache initialized. -However, if you have some other custom namespace cache instance, you should try to do migration by your own. But typically, since the cache is designed to be invalid at any time, you'd better not to bind some important logic related on that cache path changes. +However, if you have some other custom namespace cache instance, you should try to do migration by yourself. But typically, since the cache is designed to be invalid at any time, you'd better not to bind some important logic related on that cache path changes. -And, if you're previously using any version from `5.0.0-beta` to `5.0.0-beta3`, please note that the cache folder is been temporarily moved to `~/Library/Caches/default/com.hackemist.SDImageCache.default/`, however, the final release version of 5.0.0 use the path above. If you upgrade from those beta version, you may need manually do migration, check `+[SDDiskCache moveCacheDirectoryFromPath:toPath:]` for detail information. +And, if you're previously using any version from `5.0.0-beta` to `5.0.0-beta3`, please note that the cache folder has been temporarily moved to `~/Library/Caches/default/com.hackemist.SDImageCache.default/`, however, the final release version of 5.0.0 use the path above. If you upgrade from those beta version, you may need manually do migration, check `+[SDDiskCache moveCacheDirectoryFromPath:toPath:]` for detail information. #### Prefetcher