Skip to content

Commit

Permalink
Merge pull request SDWebImage#1143 from rromanchuk/feature/auto-purge…
Browse files Browse the repository at this point in the history
…-on-memory-event

Purge nscache on system memory notifications
  • Loading branch information
rs committed May 13, 2015
2 parents a516881 + 78be42e commit 393d4f1
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion SDWebImage/SDImageCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@
#import "UIImage+MultiFormat.h"
#import <CommonCrypto/CommonDigest.h>

// See https://github.com/rs/SDWebImage/pull/1141 for discussion
@interface AutoPurgeCache : NSCache
@end

@implementation AutoPurgeCache

- (id)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeAllObjects) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
return self;
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil];

}

@end

static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
// PNG signature bytes and data (below)
static unsigned char kPNGSignatureBytes[8] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
Expand Down Expand Up @@ -74,7 +97,7 @@ - (id)initWithNamespace:(NSString *)ns {
_maxCacheAge = kDefaultCacheMaxCacheAge;

// Init the memory cache
_memCache = [[NSCache alloc] init];
_memCache = [[AutoPurgeCache alloc] init];
_memCache.name = fullNamespace;

// Init the disk cache
Expand Down

0 comments on commit 393d4f1

Please sign in to comment.