Skip to content

Commit

Permalink
Added defaultTimeoutInterval property, set to 1 day. Refactored clear…
Browse files Browse the repository at this point in the history
…Cache: to removeCacheForKey:. Moved cacheDictionary to EGOCache.plist stored in the cache directory, instead of storing in NSUserDefaults
  • Loading branch information
shnhrrsn committed Nov 26, 2009
1 parent cc3bbc7 commit 243109a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
14 changes: 8 additions & 6 deletions EGOCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,33 @@
@private
NSMutableDictionary* cacheDictionary;
NSOperationQueue* diskOperationQueue;
NSTimeInterval defaultTimeoutInterval;
}

+ (EGOCache*)currentCache;

- (void)clearCache; // This is blocking, and may take a while.
- (void)clearCache:(NSString*)key;
- (void)clearCache;
- (void)removeCacheForKey:(NSString*)key;

- (BOOL)hasCacheForKey:(NSString*)key;

- (NSData*)dataForKey:(NSString*)key;
- (void)setData:(NSData*)data forKey:(NSString*)key; // withTimeoutInterval: 1 day
- (void)setData:(NSData*)data forKey:(NSString*)key; // withTimeoutInterval:defaultTimeoutInterval
- (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;

- (NSString*)stringForKey:(NSString*)key;
- (void)setString:(NSString*)aString forKey:(NSString*)key; // withTimeoutInterval: 1 day
- (void)setString:(NSString*)aString forKey:(NSString*)key; // withTimeoutInterval:defaultTimeoutInterval
- (void)setString:(NSString*)aString forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;

#if TARGET_OS_IPHONE
- (UIImage*)imageForKey:(NSString*)key;
- (void)setImage:(UIImage*)anImage forKey:(NSString*)key; // withTimeoutInterval: 1 day
- (void)setImage:(UIImage*)anImage forKey:(NSString*)key; // withTimeoutInterval:defaultTimeoutInterval
- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
#else
- (NSImage*)imageForKey:(NSString*)key;
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key; // withTimeoutInterval: 1 day
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key; // withTimeoutInterval:defaultTimeoutInterval
- (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval;
#endif

@property(nonatomic,assign) NSTimeInterval defaultTimeoutInterval; // Default is 1 day
@end
47 changes: 31 additions & 16 deletions EGOCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@

#import "EGOCache.h"

#if DEBUG
#define CHECK_FOR_EGOCACHE_PLIST() if([key isEqualToString:@"EGOCache.plist"]) { \
NSLog(@"EGOCache.plist is a reserved key and can not be modified."); \
return; }
#else
#define CHECK_FOR_EGOCACHE_PLIST() if([key isEqualToString:@"EGOCache.plist"]) return;
#endif



static NSString* _EGOCacheDirectory;

static inline NSString* EGOCacheDirectory() {
if(!_EGOCacheDirectory) {
#ifdef TARGET_OS_IPHONE
_EGOCacheDirectory = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/EGOCache"] retain];
_EGOCacheDirectory = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/EGOCache"] copy];
#else
NSString* appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
_EGOCacheDirectory = [[[appSupportDir stringByAppendingPathComponent:[[NSProcessInfo processInfo] processName]] stringByAppendingPathComponent:@"EGOCache"] retain];
_EGOCacheDirectory = [[[appSupportDir stringByAppendingPathComponent:[[NSProcessInfo processInfo] processName]] stringByAppendingPathComponent:@"EGOCache"] copy];
#endif
}

Expand All @@ -31,7 +41,7 @@
return [EGOCacheDirectory() stringByAppendingPathComponent:key];
}

static id __instance;
static EGOCache* __instance;

@interface EGOCache ()
- (void)removeItemFromCache:(NSString*)key;
Expand All @@ -42,11 +52,13 @@ - (void)saveCacheDictionary;
#pragma mark -

@implementation EGOCache
@synthesize defaultTimeoutInterval;

+ (EGOCache*)currentCache {
@synchronized(self) {
if(!__instance) {
__instance = [[EGOCache alloc] init];
__instance.defaultTimeoutInterval = 86400;
}
}

Expand All @@ -55,7 +67,7 @@ + (EGOCache*)currentCache {

- (id)init {
if((self = [super init])) {
NSDictionary* dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"EGOCache"];
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:cachePathForKey(@"EGOCache.plist")];

if([dict isKindOfClass:[NSDictionary class]]) {
cacheDictionary = [dict mutableCopy];
Expand Down Expand Up @@ -89,15 +101,17 @@ - (void)clearCache {
[self saveCacheDictionary];
}

- (void)clearCache:(NSString*)key {
- (void)removeCacheForKey:(NSString*)key {
CHECK_FOR_EGOCACHE_PLIST();

[self removeItemFromCache:key];
[self saveCacheDictionary];
}

- (void)removeItemFromCache:(NSString*)key {
NSString *cachePath = cachePathForKey(key);
NSString* cachePath = cachePathForKey(key);

NSInvocation *deleteInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(deleteDataAtPath:)]];
NSInvocation* deleteInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(deleteDataAtPath:)]];
[deleteInvocation setTarget:self];
[deleteInvocation setSelector:@selector(deleteDataAtPath:)];
[deleteInvocation setArgument:&cachePath atIndex:2];
Expand All @@ -117,12 +131,14 @@ - (BOOL)hasCacheForKey:(NSString*)key {
#pragma mark Data methods

- (void)setData:(NSData*)data forKey:(NSString*)key {
[self setData:data forKey:key withTimeoutInterval:60 * 60 * 24];
[self setData:data forKey:key withTimeoutInterval:self.defaultTimeoutInterval];
}

- (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
NSString *cachePath = cachePathForKey(key);
NSInvocation *writeInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(writeData:toPath:)]];
CHECK_FOR_EGOCACHE_PLIST();

NSString* cachePath = cachePathForKey(key);
NSInvocation* writeInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(writeData:toPath:)]];
[writeInvocation setTarget:self];
[writeInvocation setSelector:@selector(writeData:toPath:)];
[writeInvocation setArgument:&data atIndex:2];
Expand All @@ -134,7 +150,7 @@ - (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeI
[self performSelectorOnMainThread:@selector(saveAfterDelay) withObject:nil waitUntilDone:YES]; // Need to make sure the save delay get scheduled in the main runloop, not the current threads
}

- (void)saveAfterDelay { // Prevents multiple-rapid user defaults saves from happening, which will slow down your app
- (void)saveAfterDelay { // Prevents multiple-rapid saves from happening, which will slow down your app
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(saveCacheDictionary) object:nil];
[self performSelector:@selector(saveCacheDictionary) withObject:nil afterDelay:0.3];
}
Expand All @@ -157,8 +173,7 @@ - (void)deleteDataAtPath:(NSString *)path {

- (void)saveCacheDictionary {
@synchronized(self) {
[[NSUserDefaults standardUserDefaults] setObject:cacheDictionary forKey:@"EGOCache"];
[[NSUserDefaults standardUserDefaults] synchronize];
[cacheDictionary writeToFile:cachePathForKey(@"EGOCache.plist") atomically:YES];
}
}

Expand All @@ -170,7 +185,7 @@ - (NSString*)stringForKey:(NSString*)key {
}

- (void)setString:(NSString*)aString forKey:(NSString*)key {
[self setString:aString forKey:key withTimeoutInterval:60 * 60 * 24];
[self setString:aString forKey:key withTimeoutInterval:self.defaultTimeoutInterval];
}

- (void)setString:(NSString*)aString forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
Expand All @@ -187,7 +202,7 @@ - (UIImage*)imageForKey:(NSString*)key {
}

- (void)setImage:(UIImage*)anImage forKey:(NSString*)key {
[self setImage:anImage forKey:key withTimeoutInterval:60 * 60 * 24];
[self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval];
}

- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
Expand All @@ -202,7 +217,7 @@ - (NSImage*)imageForKey:(NSString*)key {
}

- (void)setImage:(NSImage*)anImage forKey:(NSString*)key {
[self setImage:anImage forKey:key withTimeoutInterval:60 * 60 * 24];
[self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeInterval];
}

- (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
Expand Down

0 comments on commit 243109a

Please sign in to comment.