Supports the user to customize the default disk cache directory, which can be used to share cache for App && App Extension

This commit is contained in:
DreamPiggy 2020-08-10 15:41:07 +08:00
parent 40023d5668
commit 1873b7198f
2 changed files with 30 additions and 5 deletions

View File

@ -103,6 +103,15 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
*/
@property (nonatomic, class, readonly, nonnull) SDImageCache *sharedImageCache;
/**
* Control the default disk cache directory. This will effect all the SDImageCache instance created, even for shared image cache.
* This can be used to share the same disk cache with the App and App Extension (Today/Notification Widget) using `- [NSFileManager.containerURLForSecurityApplicationGroupIdentifier:]`.
* @note If you pass nil, the value will be reset to `~/Library/Caches/com.hackemist.SDImageCache`.
* @note We still preserve the `namespace` arg, which means, if you change this property into `/path/to/use`, the `SDImageCache.sharedImageCache.diskCachePath` should be `/path/to/use/default` because shared image cache use `default` as namespace.
* Defaults to nil.
*/
@property (nonatomic, class, readwrite, null_resettable) NSString *defaultDiskCacheDirectory;
/**
* Init a new cache store with a specific namespace
*

View File

@ -15,6 +15,8 @@
#import "UIImage+Metadata.h"
#import "UIImage+ExtendedCacheData.h"
static NSString * _defaultDiskCacheDirectory;
@interface SDImageCache ()
#pragma mark - Properties
@ -40,6 +42,17 @@
return instance;
}
+ (NSString *)defaultDiskCacheDirectory {
if (!_defaultDiskCacheDirectory) {
_defaultDiskCacheDirectory = [[self userCacheDirectory] stringByAppendingPathComponent:@"com.hackemist.SDImageCache"];
}
return _defaultDiskCacheDirectory;
}
+ (void)setDefaultDiskCacheDirectory:(NSString *)defaultDiskCacheDirectory {
_defaultDiskCacheDirectory = [defaultDiskCacheDirectory copy];
}
- (instancetype)init {
return [self initWithNamespace:@"default"];
}
@ -75,8 +88,11 @@
if (directory != nil) {
_diskCachePath = [directory stringByAppendingPathComponent:ns];
} else {
NSString *path = [[[self userCacheDirectory] stringByAppendingPathComponent:@"com.hackemist.SDImageCache"] stringByAppendingPathComponent:ns];
_diskCachePath = path;
if (!directory) {
// Use default disk cache directory
directory = [self.class defaultDiskCacheDirectory];
}
_diskCachePath = [directory stringByAppendingPathComponent:ns];
}
NSAssert([config.diskCacheClass conformsToProtocol:@protocol(SDDiskCache)], @"Custom disk cache class must conform to `SDDiskCache` protocol");
@ -121,7 +137,7 @@
return [self.diskCache cachePathForKey:key];
}
- (nullable NSString *)userCacheDirectory {
+ (nullable NSString *)userCacheDirectory {
NSArray<NSString *> *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return paths.firstObject;
}
@ -131,9 +147,9 @@
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// ~/Library/Caches/com.hackemist.SDImageCache/default/
NSString *newDefaultPath = [[[self userCacheDirectory] stringByAppendingPathComponent:@"com.hackemist.SDImageCache"] stringByAppendingPathComponent:@"default"];
NSString *newDefaultPath = [[[self.class 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"];
NSString *oldDefaultPath = [[[self.class userCacheDirectory] stringByAppendingPathComponent:@"default"] stringByAppendingPathComponent:@"com.hackemist.SDWebImageCache.default"];
dispatch_async(self.ioQueue, ^{
[((SDDiskCache *)self.diskCache) moveCacheDirectoryFromPath:oldDefaultPath toPath:newDefaultPath];
});