Merge pull request #2779 from dreampiggy/expose_memory_disk_cache_object
Expose the memoryCache and diskCache object on `SDImageCache`, Make it useful for user who have custom property beyond `SDImageCacheConfig`
This commit is contained in:
commit
9f2db8701a
|
@ -11,6 +11,8 @@
|
|||
#import "SDWebImageDefine.h"
|
||||
#import "SDImageCacheConfig.h"
|
||||
#import "SDImageCacheDefine.h"
|
||||
#import "SDMemoryCache.h"
|
||||
#import "SDDiskCache.h"
|
||||
|
||||
/// Image Cache Options
|
||||
typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
|
||||
|
@ -61,6 +63,21 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
|
|||
*/
|
||||
@property (nonatomic, copy, nonnull, readonly) SDImageCacheConfig *config;
|
||||
|
||||
/**
|
||||
* The memory cache implementation object used for current image cache.
|
||||
* By default we use `SDMemoryCache` class, you can also use this to call your own implementation class method.
|
||||
* @note To customize this class, check `SDImageCacheConfig.memoryCacheClass` property.
|
||||
*/
|
||||
@property (nonatomic, strong, readonly, nonnull) id<SDMemoryCache> memoryCache;
|
||||
|
||||
/**
|
||||
* The disk cache implementation object used for current image cache.
|
||||
* By default we use `SDMemoryCache` class, you can also use this to call your own implementation class method.
|
||||
* @note To customize this class, check `SDImageCacheConfig.diskCacheClass` property.
|
||||
* @warning When calling method about read/write in disk cache, be sure to either make your disk cache implementation IO-safe or using the same access queue to avoid issues.
|
||||
*/
|
||||
@property (nonatomic, strong, readonly, nonnull) id<SDDiskCache> diskCache;
|
||||
|
||||
/**
|
||||
* The disk cache's root path
|
||||
*/
|
||||
|
|
|
@ -7,8 +7,6 @@
|
|||
*/
|
||||
|
||||
#import "SDImageCache.h"
|
||||
#import "SDMemoryCache.h"
|
||||
#import "SDDiskCache.h"
|
||||
#import "NSImage+Compatibility.h"
|
||||
#import "SDImageCodersManager.h"
|
||||
#import "SDImageTransformer.h"
|
||||
|
@ -20,8 +18,8 @@
|
|||
@interface SDImageCache ()
|
||||
|
||||
#pragma mark - Properties
|
||||
@property (nonatomic, strong, nonnull) id<SDMemoryCache> memCache;
|
||||
@property (nonatomic, strong, nonnull) id<SDDiskCache> diskCache;
|
||||
@property (nonatomic, strong, readwrite, nonnull) id<SDMemoryCache> memoryCache;
|
||||
@property (nonatomic, strong, readwrite, nonnull) id<SDDiskCache> diskCache;
|
||||
@property (nonatomic, copy, readwrite, nonnull) SDImageCacheConfig *config;
|
||||
@property (nonatomic, copy, readwrite, nonnull) NSString *diskCachePath;
|
||||
@property (nonatomic, strong, nullable) dispatch_queue_t ioQueue;
|
||||
|
@ -71,7 +69,7 @@
|
|||
|
||||
// Init the memory cache
|
||||
NSAssert([config.memoryCacheClass conformsToProtocol:@protocol(SDMemoryCache)], @"Custom memory cache class must conform to `SDMemoryCache` protocol");
|
||||
_memCache = [[config.memoryCacheClass alloc] initWithConfig:_config];
|
||||
_memoryCache = [[config.memoryCacheClass alloc] initWithConfig:_config];
|
||||
|
||||
// Init the disk cache
|
||||
if (directory != nil) {
|
||||
|
@ -181,7 +179,7 @@
|
|||
// if memory cache is enabled
|
||||
if (toMemory && self.config.shouldCacheImagesInMemory) {
|
||||
NSUInteger cost = image.sd_memoryCost;
|
||||
[self.memCache setObject:image forKey:key cost:cost];
|
||||
[self.memoryCache setObject:image forKey:key cost:cost];
|
||||
}
|
||||
|
||||
if (toDisk) {
|
||||
|
@ -219,7 +217,7 @@
|
|||
return;
|
||||
}
|
||||
NSUInteger cost = image.sd_memoryCost;
|
||||
[self.memCache setObject:image forKey:key cost:cost];
|
||||
[self.memoryCache setObject:image forKey:key cost:cost];
|
||||
}
|
||||
|
||||
- (void)storeImageDataToDisk:(nullable NSData *)imageData
|
||||
|
@ -290,14 +288,14 @@
|
|||
}
|
||||
|
||||
- (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key {
|
||||
return [self.memCache objectForKey:key];
|
||||
return [self.memoryCache objectForKey:key];
|
||||
}
|
||||
|
||||
- (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key {
|
||||
UIImage *diskImage = [self diskImageForKey:key];
|
||||
if (diskImage && self.config.shouldCacheImagesInMemory) {
|
||||
NSUInteger cost = diskImage.sd_memoryCost;
|
||||
[self.memCache setObject:diskImage forKey:key cost:cost];
|
||||
[self.memoryCache setObject:diskImage forKey:key cost:cost];
|
||||
}
|
||||
|
||||
return diskImage;
|
||||
|
@ -423,7 +421,7 @@
|
|||
diskImage = [self diskImageForKey:key data:diskData options:options context:context];
|
||||
if (diskImage && self.config.shouldCacheImagesInMemory) {
|
||||
NSUInteger cost = diskImage.sd_memoryCost;
|
||||
[self.memCache setObject:diskImage forKey:key cost:cost];
|
||||
[self.memoryCache setObject:diskImage forKey:key cost:cost];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -465,7 +463,7 @@
|
|||
}
|
||||
|
||||
if (fromMemory && self.config.shouldCacheImagesInMemory) {
|
||||
[self.memCache removeObjectForKey:key];
|
||||
[self.memoryCache removeObjectForKey:key];
|
||||
}
|
||||
|
||||
if (fromDisk) {
|
||||
|
@ -488,7 +486,7 @@
|
|||
return;
|
||||
}
|
||||
|
||||
[self.memCache removeObjectForKey:key];
|
||||
[self.memoryCache removeObjectForKey:key];
|
||||
}
|
||||
|
||||
- (void)removeImageFromDiskForKey:(NSString *)key {
|
||||
|
@ -512,7 +510,7 @@
|
|||
#pragma mark - Cache clean Ops
|
||||
|
||||
- (void)clearMemory {
|
||||
[self.memCache removeAllObjects];
|
||||
[self.memoryCache removeAllObjects];
|
||||
}
|
||||
|
||||
- (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion {
|
||||
|
|
|
@ -14,13 +14,6 @@
|
|||
static NSString *kTestImageKeyJPEG = @"TestImageKey.jpg";
|
||||
static NSString *kTestImageKeyPNG = @"TestImageKey.png";
|
||||
|
||||
@interface SDImageCache ()
|
||||
|
||||
@property (nonatomic, strong, nonnull) id<SDMemoryCache> memCache;
|
||||
@property (nonatomic, strong, nonnull) id<SDDiskCache> diskCache;
|
||||
|
||||
@end
|
||||
|
||||
@interface SDImageCacheTests : SDTestCase <NSFileManagerDelegate>
|
||||
|
||||
@end
|
||||
|
@ -380,7 +373,7 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
|
|||
config.memoryCacheClass = [SDWebImageTestMemoryCache class];
|
||||
NSString *nameSpace = @"SDWebImageTestMemoryCache";
|
||||
SDImageCache *cache = [[SDImageCache alloc] initWithNamespace:nameSpace diskCacheDirectory:nil config:config];
|
||||
SDWebImageTestMemoryCache *memCache = cache.memCache;
|
||||
SDWebImageTestMemoryCache *memCache = cache.memoryCache;
|
||||
expect([memCache isKindOfClass:[SDWebImageTestMemoryCache class]]).to.beTruthy();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue