Skip to content

Commit

Permalink
added option to disable memory cache
Browse files Browse the repository at this point in the history
use this:
```shouldDisableMemoryCache``` to toggle memory cache
  • Loading branch information
mythodeia committed Jul 15, 2015
1 parent a9ea132 commit df751e7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
5 changes: 5 additions & 0 deletions SDWebImage/SDImageCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot
*/
@property (assign, nonatomic) BOOL shouldDisableiCloud;

/**
* use memory cache [defaults to YES]
*/
@property (assign, nonatomic) BOOL shouldCacheImagesInMemory;

/**
* The maximum "total cost" of the in-memory image cache. The cost function is the number of pixels held in memory.
*/
Expand Down
24 changes: 16 additions & 8 deletions SDWebImage/SDImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ - (id)initWithNamespace:(NSString *)ns diskCacheDirectory:(NSString *)directory
// Set decompression to YES
_shouldDecompressImages = YES;

// memory cache enabled
_shouldCacheImagesInMemory = YES;

// Disable iCloud
_shouldDisableiCloud = YES;

Expand Down Expand Up @@ -196,9 +199,11 @@ - (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate image
if (!image || !key) {
return;
}

NSUInteger cost = SDCacheCostForImage(image);
[self.memCache setObject:image forKey:key cost:cost];
// if memory cache is enabled
if (self.shouldCacheImagesInMemory) {
NSUInteger cost = SDCacheCostForImage(image);
[self.memCache setObject:image forKey:key cost:cost];
}

if (toDisk) {
dispatch_async(self.ioQueue, ^{
Expand Down Expand Up @@ -290,6 +295,7 @@ - (UIImage *)imageFromMemoryCacheForKey:(NSString *)key {
}

- (UIImage *)imageFromDiskCacheForKey:(NSString *)key {

// First check the in-memory cache...
UIImage *image = [self imageFromMemoryCacheForKey:key];
if (image) {
Expand All @@ -298,7 +304,7 @@ - (UIImage *)imageFromDiskCacheForKey:(NSString *)key {

// Second check the disk cache...
UIImage *diskImage = [self diskImageForKey:key];
if (diskImage) {
if (diskImage && self.shouldCacheImagesInMemory) {
NSUInteger cost = SDCacheCostForImage(diskImage);
[self.memCache setObject:diskImage forKey:key cost:cost];
}
Expand Down Expand Up @@ -369,7 +375,7 @@ - (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompl

@autoreleasepool {
UIImage *diskImage = [self diskImageForKey:key];
if (diskImage) {
if (diskImage && self.shouldCacheImagesInMemory) {
NSUInteger cost = SDCacheCostForImage(diskImage);
[self.memCache setObject:diskImage forKey:key cost:cost];
}
Expand Down Expand Up @@ -400,9 +406,11 @@ - (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletion
if (key == nil) {
return;
}

[self.memCache removeObjectForKey:key];


if (self.shouldCacheImagesInMemory) {
[self.memCache removeObjectForKey:key];
}

if (fromDisk) {
dispatch_async(self.ioQueue, ^{
[_fileManager removeItemAtPath:[self defaultCachePathForKey:key] error:nil];
Expand Down

0 comments on commit df751e7

Please sign in to comment.