Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a secondary weak memory cache to improve speed and decrease memory usage when reusing previous images #791

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 23 additions & 8 deletions SDWebImage/SDImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ BOOL ImageDataHasPNGPreffix(NSData *data) {
@interface SDImageCache ()

@property (strong, nonatomic) NSCache *memCache;
@property (strong, nonatomic) NSMapTable *weakMemCache;
@property (strong, nonatomic) NSString *diskCachePath;
@property (strong, nonatomic) NSMutableArray *customPaths;
@property (SDDispatchQueueSetterSementics, nonatomic) dispatch_queue_t ioQueue;
Expand Down Expand Up @@ -70,6 +71,9 @@ - (id)initWithNamespace:(NSString *)ns {
// Init the memory cache
_memCache = [[NSCache alloc] init];
_memCache.name = fullNamespace;

// Init the weak memory cache (to keep a weak reference to images cleared from our memory cache that are actually still alive)
_weakMemCache = [NSMapTable strongToWeakObjectsMapTable];

// Init the disk cache
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
Expand Down Expand Up @@ -146,8 +150,7 @@ - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate image
if (!image || !key) {
return;
}

[self.memCache setObject:image forKey:key cost:image.size.height * image.size.width * image.scale];
[self addImageToMemoryCache:image forKey:key];

if (toDisk) {
dispatch_async(self.ioQueue, ^{
Expand Down Expand Up @@ -221,7 +224,21 @@ - (void)diskImageExistsWithKey:(NSString *)key completion:(SDWebImageCheckCacheC
}

- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key {
return [self.memCache objectForKey:key];
UIImage *image = [self.memCache objectForKey:key];
// Check the weak memory cache to see if a previously cached image is still alive
if (!image) {
image = [self.weakMemCache objectForKey:key];
if (image) {
// re-add to memory cache
[self addImageToMemoryCache:image forKey:key];
}
}
return image;
}

- (void) addImageToMemoryCache:(UIImage *)image forKey:(NSString *)key {
[self.memCache setObject:image forKey:key cost:image.size.height * image.size.width * image.scale];
[self.weakMemCache setObject:image forKey:key];
}

- (UIImage *)imageFromDiskCacheForKey:(NSString *)key {
Expand All @@ -230,12 +247,11 @@ - (UIImage *)imageFromDiskCacheForKey:(NSString *)key {
if (image) {
return image;
}

// Second check the disk cache...
UIImage *diskImage = [self diskImageForKey:key];
if (diskImage) {
CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale;
[self.memCache setObject:diskImage forKey:key cost:cost];
[self addImageToMemoryCache:diskImage forKey:key];
}

return diskImage;
Expand Down Expand Up @@ -302,8 +318,7 @@ - (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompl
@autoreleasepool {
UIImage *diskImage = [self diskImageForKey:key];
if (diskImage) {
CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale;
[self.memCache setObject:diskImage forKey:key cost:cost];
[self addImageToMemoryCache:diskImage forKey:key];
}

dispatch_async(dispatch_get_main_queue(), ^{
Expand Down