Merge pull request #2535 from dreampiggy/refactory_cache_path

Refactory cache path about namespace && final cache directory
This commit is contained in:
DreamPiggy 2018-12-17 22:28:03 +08:00 committed by GitHub
commit e6f7cb72cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 47 deletions

View File

@ -62,21 +62,11 @@ 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.
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.
+ Objective-C
```objective-c
SDImageCacheConfig.defaultCacheConfig.namespacePrefix = @"com.hackemist.SDWebImageCache.";
```
+ Swift
```swift
SDImageCacheConfig.`default`.namespacePrefix = "com.hackemist.SDWebImageCache."
```
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

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.SDWebImageCache.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