Skip to content

Commit

Permalink
Merge pull request #11 from eaigner/master
Browse files Browse the repository at this point in the history
Made class compile with ARC
  • Loading branch information
shnhrrsn committed Mar 28, 2012
2 parents 8b7c7ec + 8306e1c commit 8d0a067
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions EGOCache.m
Expand Up @@ -27,14 +27,18 @@
#import "EGOCache.h" #import "EGOCache.h"


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



#ifdef __has_feature
#define EGO_NO_ARC !__has_feature(objc_arc)
#else
#define EGO_NO_ARC 1
#endif


static NSString* _EGOCacheDirectory; static NSString* _EGOCacheDirectory;


Expand Down Expand Up @@ -88,9 +92,9 @@ - (id)init {
diskOperationQueue = [[NSOperationQueue alloc] init]; diskOperationQueue = [[NSOperationQueue alloc] init];


[[NSFileManager defaultManager] createDirectoryAtPath:EGOCacheDirectory() [[NSFileManager defaultManager] createDirectoryAtPath:EGOCacheDirectory()
withIntermediateDirectories:YES withIntermediateDirectories:YES
attributes:nil attributes:nil
error:NULL]; error:NULL];


for(NSString* key in cacheDictionary) { for(NSString* key in cacheDictionary) {
NSDate* date = [cacheDictionary objectForKey:key]; NSDate* date = [cacheDictionary objectForKey:key];
Expand All @@ -113,7 +117,7 @@ - (void)clearCache {


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

[self removeItemFromCache:key]; [self removeItemFromCache:key];
[self saveCacheDictionary]; [self saveCacheDictionary];
} }
Expand Down Expand Up @@ -204,7 +208,11 @@ - (void)saveCacheDictionary {
#pragma mark String methods #pragma mark String methods


- (NSString*)stringForKey:(NSString*)key { - (NSString*)stringForKey:(NSString*)key {
return [[[NSString alloc] initWithData:[self dataForKey:key] encoding:NSUTF8StringEncoding] autorelease]; NSString *string = [[NSString alloc] initWithData:[self dataForKey:key] encoding:NSUTF8StringEncoding];
#if EGO_NO_ARC
return [string autorelease];
#endif
return string;
} }


- (void)setString:(NSString*)aString forKey:(NSString*)key { - (void)setString:(NSString*)aString forKey:(NSString*)key {
Expand Down Expand Up @@ -245,7 +253,7 @@ - (void)setImage:(NSImage*)anImage forKey:(NSString*)key {


- (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { - (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval {
[self setData:[[[anImage representations] objectAtIndex:0] representationUsingType:NSPNGFileType properties:nil] [self setData:[[[anImage representations] objectAtIndex:0] representationUsingType:NSPNGFileType properties:nil]
forKey:key withTimeoutInterval:timeoutInterval]; forKey:key withTimeoutInterval:timeoutInterval];
} }


#endif #endif
Expand All @@ -257,9 +265,9 @@ - (NSData*)plistForKey:(NSString*)key; {
NSData* plistData = [self dataForKey:key]; NSData* plistData = [self dataForKey:key];


return [NSPropertyListSerialization propertyListFromData:plistData return [NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListImmutable mutabilityOption:NSPropertyListImmutable
format:nil format:nil
errorDescription:nil]; errorDescription:nil];
} }


- (void)setPlist:(id)plistObject forKey:(NSString*)key; { - (void)setPlist:(id)plistObject forKey:(NSString*)key; {
Expand All @@ -269,8 +277,8 @@ - (void)setPlist:(id)plistObject forKey:(NSString*)key; {
- (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; { - (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; {
// Binary plists are used over XML for better performance // Binary plists are used over XML for better performance
NSData* plistData = [NSPropertyListSerialization dataFromPropertyList:plistObject NSData* plistData = [NSPropertyListSerialization dataFromPropertyList:plistObject
format:NSPropertyListBinaryFormat_v1_0 format:NSPropertyListBinaryFormat_v1_0
errorDescription:NULL]; errorDescription:NULL];


[self setData:plistData forKey:key withTimeoutInterval:timeoutInterval]; [self setData:plistData forKey:key withTimeoutInterval:timeoutInterval];
} }
Expand All @@ -281,15 +289,19 @@ - (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTi
- (void)performDiskWriteOperation:(NSInvocation *)invoction { - (void)performDiskWriteOperation:(NSInvocation *)invoction {
NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invoction]; NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithInvocation:invoction];
[diskOperationQueue addOperation:operation]; [diskOperationQueue addOperation:operation];
#if EGO_NO_ARC
[operation release]; [operation release];
#endif
} }


#pragma mark - #pragma mark -


- (void)dealloc { - (void)dealloc {
#if EGO_NO_ARC
[diskOperationQueue release]; [diskOperationQueue release];
[cacheDictionary release]; [cacheDictionary release];
[super dealloc]; [super dealloc];
#endif
} }


@end @end

0 comments on commit 8d0a067

Please sign in to comment.