From 1bf99638d21de95662d9376fc639702433fcd057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Arratia=20Garc=C3=ADa?= Date: Wed, 20 Aug 2014 15:09:35 +0200 Subject: [PATCH 01/11] - Removed dealloc. it was an empty function - Added test of key for plist in a function where it wasn't present - Added possibility of get path to cached file. Very useful por previewcontroller and some other classes which need an url and not an NSData. --- EGOCache.h | 2 ++ EGOCache.m | 22 ++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/EGOCache.h b/EGOCache.h index 05158cf..7f4fc0a 100755 --- a/EGOCache.h +++ b/EGOCache.h @@ -45,6 +45,8 @@ - (BOOL)hasCacheForKey:(NSString*)key; +- (NSString*)pathForKey:(NSString*)key; + - (NSData*)dataForKey:(NSString*)key; - (void)setData:(NSData*)data forKey:(NSString*)key; - (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; diff --git a/EGOCache.m b/EGOCache.m index 9a03a17..9205e40 100755 --- a/EGOCache.m +++ b/EGOCache.m @@ -220,6 +220,8 @@ - (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key { } - (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { + CHECK_FOR_EGOCACHE_PLIST(); + dispatch_async(_diskQueue, ^{ [[NSFileManager defaultManager] copyItemAtPath:filePath toPath:cachePathForKey(_directory, key) error:NULL]; }); @@ -227,6 +229,16 @@ - (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key withTimeoutInterva [self setCacheTimeoutInterval:timeoutInterval forKey:key]; } +#pragma mark Path methods + +- (NSString*)pathForKey:(NSString*)key { + if([self hasCacheForKey:key]) { + return cachePathForKey(_directory, key); + } else { + return nil; + } +} + #pragma mark - #pragma mark Data methods @@ -306,7 +318,7 @@ - (void)setImage:(UIImage*)anImage forKey:(NSString*)key { } - (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { - @try { + @try { // Using NSKeyedArchiver preserves all information such as scale, orientation, and the proper image format instead of saving everything as pngs [self setData:[NSKeyedArchiver archivedDataWithRootObject:anImage] forKey:key withTimeoutInterval:timeoutInterval]; } @catch (NSException* e) { @@ -362,7 +374,7 @@ - (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTi - (id)objectForKey:(NSString*)key { if([self hasCacheForKey:key]) { - return [NSKeyedUnarchiver unarchiveObjectWithData:[self dataForKey:key]]; + return [NSKeyedUnarchiver unarchiveObjectWithFile:cachePathForKey(_directory, key)]; } else { return nil; } @@ -376,10 +388,4 @@ - (void)setObject:(id)anObject forKey:(NSString*)key withTimeoutInterv [self setData:[NSKeyedArchiver archivedDataWithRootObject:anObject] forKey:key withTimeoutInterval:timeoutInterval]; } -#pragma mark - - -- (void)dealloc { - -} - @end From a12f0429e9fbb3b424c3ca73c921d3f5320909f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Arratia=20Garc=C3=ADa?= Date: Thu, 21 Aug 2014 08:49:29 +0200 Subject: [PATCH 02/11] - Changed / for _ in key name. It allows urls as keys - Added JSON Methods. It allows EGOCache to store JSONs easily --- EGOCache.h | 5 +++++ EGOCache.m | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/EGOCache.h b/EGOCache.h index 7f4fc0a..86d7415 100755 --- a/EGOCache.h +++ b/EGOCache.h @@ -72,6 +72,11 @@ - (void)setPlist:(id)plistObject forKey:(NSString*)key; - (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (id)jsonObjectForKey:(NSString*)key; +- (id)mutableJsonObjectForKey:(NSString*)key; +- (void)setJson:(id)jsonObject forKey:(NSString*)key; +- (void)setJson:(id)jsonObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; + - (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key; - (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; diff --git a/EGOCache.m b/EGOCache.m index 9205e40..0caea2a 100755 --- a/EGOCache.m +++ b/EGOCache.m @@ -35,7 +35,8 @@ #endif static inline NSString* cachePathForKey(NSString* directory, NSString* key) { - return [directory stringByAppendingPathComponent:key]; + key = [key stringByReplacingOccurrencesOfString:@"/" withString:@"_"]; + return [directory stringByAppendingPathComponent:key]; } #pragma mark - @@ -369,6 +370,24 @@ - (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTi [self setData:plistData forKey:key withTimeoutInterval:timeoutInterval]; } +#pragma mark - +#pragma mark JSON Object methods +- (id)jsonObjectForKey:(NSString*)key { +NSData* jsonData = [self dataForKey:key]; +return [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil]; +} +- (id)mutableJsonObjectForKey:(NSString*)key { +NSData* jsonData = [self dataForKey:key]; +return [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; +} +- (void)setJson:(id)jsonObject forKey:(NSString*)key { +[self setJson:jsonObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval]; +} +- (void)setJson:(id)jsonObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { +NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject options:0 error:nil]; +[self setData:jsonData forKey:key withTimeoutInterval:timeoutInterval]; +} + #pragma mark - #pragma mark Object methods From 280fff44819d9107cdc5df9d7f106fcbdbdad86e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Arratia=20Garc=C3=ADa?= Date: Wed, 27 Aug 2014 10:19:29 +0200 Subject: [PATCH 03/11] - Added Sync Methods to store in cache. Useful when you use the cache as a temporal disk space for previewControllers for example. Perfect to use with methods to obtain path for cache object. - Added json methods --- EGOCache.h | 16 ++++++ EGOCache.m | 149 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 129 insertions(+), 36 deletions(-) diff --git a/EGOCache.h b/EGOCache.h index 86d7415..535dabe 100755 --- a/EGOCache.h +++ b/EGOCache.h @@ -49,11 +49,15 @@ - (NSData*)dataForKey:(NSString*)key; - (void)setData:(NSData*)data forKey:(NSString*)key; +- (void)setData:(NSData*)data forKey:(NSString*)key synchronous:(BOOL)sync; - (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; - (NSString*)stringForKey:(NSString*)key; - (void)setString:(NSString*)aString forKey:(NSString*)key; +- (void)setString:(NSString*)aString forKey:(NSString*)key synchronous:(BOOL)sync; - (void)setString:(NSString*)aString forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setString:(NSString*)aString forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; - (NSDate*)dateForKey:(NSString*)key; - (NSArray*)allKeys; @@ -61,28 +65,40 @@ #if TARGET_OS_IPHONE - (UIImage*)imageForKey:(NSString*)key; - (void)setImage:(UIImage*)anImage forKey:(NSString*)key; +- (void)setImage:(UIImage*)anImage forKey:(NSString*)key synchronous:(BOOL)sync; - (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; #else - (NSImage*)imageForKey:(NSString*)key; - (void)setImage:(NSImage*)anImage forKey:(NSString*)key; +- (void)setImage:(UIImage*)anImage forKey:(NSString*)key synchronous:(BOOL)sync; - (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; #endif - (NSData*)plistForKey:(NSString*)key; - (void)setPlist:(id)plistObject forKey:(NSString*)key; +- (void)setPlist:(id)plistObject forKey:(NSString*)key synchronous:(BOOL)sync; - (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; - (id)jsonObjectForKey:(NSString*)key; - (id)mutableJsonObjectForKey:(NSString*)key; - (void)setJson:(id)jsonObject forKey:(NSString*)key; +- (void)setJson:(id)jsonObject forKey:(NSString*)key synchronous:(BOOL)sync; - (void)setJson:(id)jsonObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setJson:(id)jsonObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; - (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key; +- (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key synchronous:(BOOL)sync; - (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; - (id)objectForKey:(NSString*)key; - (void)setObject:(id)anObject forKey:(NSString*)key; +- (void)setObject:(id)anObject forKey:(NSString*)key synchronous:(BOOL)sync; - (void)setObject:(id)anObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setObject:(id)anObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; @property(nonatomic,assign) NSTimeInterval defaultTimeoutInterval; // Default is 1 day @end \ No newline at end of file diff --git a/EGOCache.m b/EGOCache.m index 0caea2a..f2fa504 100755 --- a/EGOCache.m +++ b/EGOCache.m @@ -217,16 +217,30 @@ - (void)setCacheTimeoutInterval:(NSTimeInterval)timeoutInterval forKey:(NSString #pragma mark Copy file methods - (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key { - [self copyFilePath:filePath asKey:key withTimeoutInterval:self.defaultTimeoutInterval]; + [self copyFilePath:filePath asKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; +} + +- (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key synchronous:(BOOL)sync{ + [self copyFilePath:filePath asKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:sync]; } - (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { + [self copyFilePath:filePath asKey:key withTimeoutInterval:timeoutInterval synchronous:NO]; +} + +- (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ CHECK_FOR_EGOCACHE_PLIST(); - dispatch_async(_diskQueue, ^{ - [[NSFileManager defaultManager] copyItemAtPath:filePath toPath:cachePathForKey(_directory, key) error:NULL]; - }); - + if (sync){ + dispatch_sync(_diskQueue, ^{ + [[NSFileManager defaultManager] copyItemAtPath:filePath toPath:cachePathForKey(_directory, key) error:NULL]; + }); + }else{ + dispatch_async(_diskQueue, ^{ + [[NSFileManager defaultManager] copyItemAtPath:filePath toPath:cachePathForKey(_directory, key) error:NULL]; + }); + } + [self setCacheTimeoutInterval:timeoutInterval forKey:key]; } @@ -244,17 +258,31 @@ - (NSString*)pathForKey:(NSString*)key { #pragma mark Data methods - (void)setData:(NSData*)data forKey:(NSString*)key { - [self setData:data forKey:key withTimeoutInterval:self.defaultTimeoutInterval]; + [self setData:data forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } - (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { + [self setData:data forKey:key withTimeoutInterval:timeoutInterval synchronous:NO]; +} + +- (void)setData:(NSData*)data forKey:(NSString*)key synchronous:(BOOL)sync{ + [self setData:data forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:sync]; +} + +- (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ CHECK_FOR_EGOCACHE_PLIST(); NSString* cachePath = cachePathForKey(_directory, key); - dispatch_async(_diskQueue, ^{ - [data writeToFile:cachePath atomically:YES]; - }); + if (sync){ + dispatch_sync(_diskQueue, ^{ + [data writeToFile:cachePath atomically:YES]; + }); + }else{ + dispatch_async(_diskQueue, ^{ + [data writeToFile:cachePath atomically:YES]; + }); + } [self setCacheTimeoutInterval:timeoutInterval forKey:key]; } @@ -290,11 +318,19 @@ - (NSString*)stringForKey:(NSString*)key { } - (void)setString:(NSString*)aString forKey:(NSString*)key { - [self setString:aString forKey:key withTimeoutInterval:self.defaultTimeoutInterval]; + [self setData:[aString dataUsingEncoding:NSUTF8StringEncoding] forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; +} + +- (void)setString:(NSString*)aString forKey:(NSString*)key synchronous:(BOOL)sync{ + [self setData:[aString dataUsingEncoding:NSUTF8StringEncoding] forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:sync]; } - (void)setString:(NSString*)aString forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { - [self setData:[aString dataUsingEncoding:NSUTF8StringEncoding] forKey:key withTimeoutInterval:timeoutInterval]; + [self setData:[aString dataUsingEncoding:NSUTF8StringEncoding] forKey:key withTimeoutInterval:timeoutInterval synchronous:NO]; +} + +- (void)setString:(NSString*)aString forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ + [self setData:[aString dataUsingEncoding:NSUTF8StringEncoding] forKey:key withTimeoutInterval:timeoutInterval synchronous:sync]; } #pragma mark - @@ -314,14 +350,22 @@ - (UIImage*)imageForKey:(NSString*)key { return image; } -- (void)setImage:(UIImage*)anImage forKey:(NSString*)key { - [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval]; +- (void)setImage:(UIImage*)anImage forKey:(NSString*)key{ + [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; +} + +- (void)setImage:(UIImage*)anImage forKey:(NSString*)key synchronous:(BOOL)sync{ + [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:sync]; +} + +- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval{ + [self setImage:anImage forKey:key withTimeoutInterval:timeoutInterval synchronous:NO]; } -- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { +- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ @try { // Using NSKeyedArchiver preserves all information such as scale, orientation, and the proper image format instead of saving everything as pngs - [self setData:[NSKeyedArchiver archivedDataWithRootObject:anImage] forKey:key withTimeoutInterval:timeoutInterval]; + [self setData:[NSKeyedArchiver archivedDataWithRootObject:anImage] forKey:key withTimeoutInterval:timeoutInterval synchronous:sync]; } @catch (NSException* e) { // Something went wrong, but we'll fail silently. } @@ -337,10 +381,16 @@ - (NSImage*)imageForKey:(NSString*)key { - (void)setImage:(NSImage*)anImage forKey:(NSString*)key { [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval]; } - +- (void)setImage:(UIImage*)anImage forKey:(NSString*)key synchronous:(BOOL)sync{ + [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:sync]; +} - (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { [self setData:[[[anImage representations] objectAtIndex:0] representationUsingType:NSPNGFileType properties:nil] - forKey:key withTimeoutInterval:timeoutInterval]; + forKey:key withTimeoutInterval:timeoutInterval synchronous:NO]; +} +- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ + [self setData:[[[anImage representations] objectAtIndex:0] representationUsingType:NSPNGFileType properties:nil] + forKey:key withTimeoutInterval:timeoutInterval synchronous:sync]; } #endif @@ -357,35 +407,54 @@ - (NSData*)plistForKey:(NSString*)key; { errorDescription:nil]; } -- (void)setPlist:(id)plistObject forKey:(NSString*)key; { - [self setPlist:plistObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval]; +- (void)setPlist:(id)plistObject forKey:(NSString*)key{ + [self setPlist:plistObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } -- (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; { +- (void)setPlist:(id)plistObject forKey:(NSString*)key synchronous:(BOOL)sync{ + [self setPlist:plistObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:sync]; +} + +- (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval{ + [self setPlist:plistObject forKey:key withTimeoutInterval:timeoutInterval synchronous:NO]; +} + +- (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ // Binary plists are used over XML for better performance NSData* plistData = [NSPropertyListSerialization dataFromPropertyList:plistObject format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]; - [self setData:plistData forKey:key withTimeoutInterval:timeoutInterval]; + [self setData:plistData forKey:key withTimeoutInterval:timeoutInterval synchronous:sync]; } #pragma mark - #pragma mark JSON Object methods -- (id)jsonObjectForKey:(NSString*)key { -NSData* jsonData = [self dataForKey:key]; -return [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil]; +- (id)jsonObjectForKey:(NSString*)key{ + NSData* jsonData = [self dataForKey:key]; + return [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil]; } -- (id)mutableJsonObjectForKey:(NSString*)key { -NSData* jsonData = [self dataForKey:key]; -return [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; + +- (id)mutableJsonObjectForKey:(NSString*)key{ + NSData* jsonData = [self dataForKey:key]; + return [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; } -- (void)setJson:(id)jsonObject forKey:(NSString*)key { -[self setJson:jsonObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval]; + +- (void)setJson:(id)jsonObject forKey:(NSString*)key{ + [self setJson:jsonObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; +} + +- (void)setJson:(id)jsonObject forKey:(NSString*)key synchronous:(BOOL)sync{ + [self setJson:jsonObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:sync]; +} + +- (void)setJson:(id)jsonObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval{ + [self setJson:jsonObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } -- (void)setJson:(id)jsonObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { -NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject options:0 error:nil]; -[self setData:jsonData forKey:key withTimeoutInterval:timeoutInterval]; + +- (void)setJson:(id)jsonObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonObject options:0 error:nil]; + [self setData:jsonData forKey:key withTimeoutInterval:timeoutInterval synchronous:sync]; } #pragma mark - @@ -399,12 +468,20 @@ - (void)setJson:(id)jsonObject forKey:(NSString*)key withTimeoutInterval:(NSTime } } -- (void)setObject:(id)anObject forKey:(NSString*)key { - [self setObject:anObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval]; +- (void)setObject:(id)anObject forKey:(NSString*)key{ + [self setObject:anObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; +} + +- (void)setObject:(id)anObject forKey:(NSString*)key synchronous:(BOOL)sync{ + [self setObject:anObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:sync]; +} + +- (void)setObject:(id)anObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval{ + [self setObject:anObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } -- (void)setObject:(id)anObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { - [self setData:[NSKeyedArchiver archivedDataWithRootObject:anObject] forKey:key withTimeoutInterval:timeoutInterval]; +- (void)setObject:(id)anObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ + [self setData:[NSKeyedArchiver archivedDataWithRootObject:anObject] forKey:key withTimeoutInterval:timeoutInterval synchronous:sync]; } @end From 5adab63f4e65f53a430ae2581809364e65b83a4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Arratia=20Garc=C3=ADa?= Date: Thu, 9 Apr 2015 11:26:16 +0200 Subject: [PATCH 04/11] - Deprecation with plists corrected --- EGOCache.m | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/EGOCache.m b/EGOCache.m index f2fa504..f5e2528 100755 --- a/EGOCache.m +++ b/EGOCache.m @@ -400,11 +400,11 @@ - (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NS - (NSData*)plistForKey:(NSString*)key; { NSData* plistData = [self dataForKey:key]; - - return [NSPropertyListSerialization propertyListFromData:plistData - mutabilityOption:NSPropertyListImmutable - format:nil - errorDescription:nil]; + + return [NSPropertyListSerialization propertyListWithData:plistData + options:NSPropertyListImmutable + format:nil + error:nil]; } - (void)setPlist:(id)plistObject forKey:(NSString*)key{ @@ -421,9 +421,10 @@ - (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTi - (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ // Binary plists are used over XML for better performance - NSData* plistData = [NSPropertyListSerialization dataFromPropertyList:plistObject - format:NSPropertyListBinaryFormat_v1_0 - errorDescription:NULL]; + NSData* plistData = [NSPropertyListSerialization dataWithPropertyList:plistObject + format:NSPropertyListBinaryFormat_v1_0 + options:0 + error:nil]; [self setData:plistData forKey:key withTimeoutInterval:timeoutInterval synchronous:sync]; } From 5b238c4679a1f0b085e88dd89d14dea8d6574fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Arratia=20Garc=C3=ADa?= Date: Thu, 9 Apr 2015 11:55:39 +0200 Subject: [PATCH 05/11] - using clang extensions for nullability xcode >6.3 previous uses header definition to simulate --- EGOCache.h | 110 ++++++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 51 deletions(-) diff --git a/EGOCache.h b/EGOCache.h index 535dabe..a7a7388 100755 --- a/EGOCache.h +++ b/EGOCache.h @@ -30,75 +30,83 @@ #import #endif +#if !__has_feature(nullability) +# define nullable +# define nonnull +# define __nullable +# define __nonnull +#endif + @interface EGOCache : NSObject -+ (instancetype)currentCache __deprecated; // Renamed to globalCache ++ (nonnull instancetype)currentCache __deprecated; // Renamed to globalCache // Global cache for easy use -+ (instancetype)globalCache; ++ (nonnull instancetype)globalCache; // Opitionally create a different EGOCache instance with it's own cache directory -- (id)initWithCacheDirectory:(NSString*)cacheDirectory; +- (nonnull id)initWithCacheDirectory:(NSString* __nonnull)cacheDirectory; - (void)clearCache; -- (void)removeCacheForKey:(NSString*)key; +- (void)removeCacheForKey:(NSString* __nonnull)key; -- (BOOL)hasCacheForKey:(NSString*)key; +- (BOOL)hasCacheForKey:(NSString* __nonnull)key; -- (NSString*)pathForKey:(NSString*)key; +- (NSString* __nullable)pathForKey:(NSString* __nonnull)key; -- (NSData*)dataForKey:(NSString*)key; -- (void)setData:(NSData*)data forKey:(NSString*)key; -- (void)setData:(NSData*)data forKey:(NSString*)key synchronous:(BOOL)sync; -- (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; -- (void)setData:(NSData*)data forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; +- (NSData* __nullable)dataForKey:(NSString* __nonnull)key; +- (void)setData:(NSData* __nonnull)data forKey:(NSString* __nonnull)key; +- (void)setData:(NSData* __nonnull)data forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; +- (void)setData:(NSData* __nonnull)data forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setData:(NSData* __nonnull)data forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; -- (NSString*)stringForKey:(NSString*)key; -- (void)setString:(NSString*)aString forKey:(NSString*)key; -- (void)setString:(NSString*)aString forKey:(NSString*)key synchronous:(BOOL)sync; -- (void)setString:(NSString*)aString forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; -- (void)setString:(NSString*)aString forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; +- (NSString* __nullable)stringForKey:(NSString* __nonnull)key; +- (void)setString:(NSString* __nonnull)aString forKey:(NSString* __nonnull)key; +- (void)setString:(NSString* __nonnull)aString forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; +- (void)setString:(NSString* __nonnull)aString forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setString:(NSString* __nonnull)aString forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; -- (NSDate*)dateForKey:(NSString*)key; -- (NSArray*)allKeys; +- (NSDate* __nullable)dateForKey:(NSString* __nonnull)key; +- (NSArray* __nonnull)allKeys; #if TARGET_OS_IPHONE -- (UIImage*)imageForKey:(NSString*)key; -- (void)setImage:(UIImage*)anImage forKey:(NSString*)key; -- (void)setImage:(UIImage*)anImage forKey:(NSString*)key synchronous:(BOOL)sync; -- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; -- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; +- (UIImage* __nullable)imageForKey:(NSString* __nonnull)key; +- (void)setImage:(UIImage* __nonnull)anImage forKey:(NSString* __nonnull)key; +- (void)setImage:(UIImage* __nonnull)anImage forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; +- (void)setImage:(UIImage* __nonnull)anImage forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setImage:(UIImage* __nonnull)anImage forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; #else -- (NSImage*)imageForKey:(NSString*)key; -- (void)setImage:(NSImage*)anImage forKey:(NSString*)key; -- (void)setImage:(UIImage*)anImage forKey:(NSString*)key synchronous:(BOOL)sync; -- (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; -- (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; +- (NSImage* __nullable)imageForKey:(NSString* __nonnull)key; +- (void)setImage:(NSImage* __nonnull)anImage forKey:(NSString* __nonnull)key; +- (void)setImage:(UIImage* __nonnull)anImage forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; +- (void)setImage:(NSImage* __nonnull)anImage forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setImage:(UIImage* __nonnull)anImage forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; #endif -- (NSData*)plistForKey:(NSString*)key; -- (void)setPlist:(id)plistObject forKey:(NSString*)key; -- (void)setPlist:(id)plistObject forKey:(NSString*)key synchronous:(BOOL)sync; -- (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; -- (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; - -- (id)jsonObjectForKey:(NSString*)key; -- (id)mutableJsonObjectForKey:(NSString*)key; -- (void)setJson:(id)jsonObject forKey:(NSString*)key; -- (void)setJson:(id)jsonObject forKey:(NSString*)key synchronous:(BOOL)sync; -- (void)setJson:(id)jsonObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; -- (void)setJson:(id)jsonObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; - -- (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key; -- (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key synchronous:(BOOL)sync; -- (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; -- (void)copyFilePath:(NSString*)filePath asKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; - -- (id)objectForKey:(NSString*)key; -- (void)setObject:(id)anObject forKey:(NSString*)key; -- (void)setObject:(id)anObject forKey:(NSString*)key synchronous:(BOOL)sync; -- (void)setObject:(id)anObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; -- (void)setObject:(id)anObject forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; +- (NSData* __nullable)plistForKey:(NSString* __nonnull)key; +- (void)setPlist:(nonnull id)plistObject forKey:(NSString* __nonnull)key; +- (void)setPlist:(nonnull id)plistObject forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; +- (void)setPlist:(nonnull id)plistObject forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setPlist:(nonnull id)plistObject forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; + +- (id __nullable)jsonObjectForKey:(NSString* __nonnull)key; +- (id __nullable)mutableJsonObjectForKey:(NSString* __nonnull)key; +- (void)setJson:(nonnull id)jsonObject forKey:(NSString* __nonnull)key; +- (void)setJson:(nonnull id)jsonObject forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; +- (void)setJson:(nonnull id)jsonObject forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setJson:(nonnull id)jsonObject forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; + +- (void)copyFilePath:(NSString* __nonnull)filePath asKey:(NSString* __nonnull)key; +- (void)copyFilePath:(NSString* __nonnull)filePath asKey:(NSString* __nonnull)key synchronous:(BOOL)sync; +- (void)copyFilePath:(NSString* __nonnull)filePath asKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)copyFilePath:(NSString* __nonnull)filePath asKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; + + +- (nullable id)objectForKey:(NSString* __nonnull)key; +- (void)setObject:(nonnull id)anObject forKey:(NSString* __nonnull)key; +- (void)setObject:(nonnull id)anObject forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; +- (void)setObject:(nonnull id)anObject forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; +- (void)setObject:(nonnull id)anObject forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; @property(nonatomic,assign) NSTimeInterval defaultTimeoutInterval; // Default is 1 day @end \ No newline at end of file From ff02c621e6f96247d6c39981421e86265bdc946a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Arratia=20Garc=C3=ADa?= Date: Thu, 13 Aug 2015 12:01:18 +0200 Subject: [PATCH 06/11] - Bugfix parameters order: dispatch_set_target_queue(priority, _cacheInfoQueue); --- EGOCache.m | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/EGOCache.m b/EGOCache.m index f5e2528..3443916 100755 --- a/EGOCache.m +++ b/EGOCache.m @@ -85,18 +85,18 @@ - (id)init { - (id)initWithCacheDirectory:(NSString*)cacheDirectory { if((self = [super init])) { - _cacheInfoQueue = dispatch_queue_create("com.enormego.egocache.info", DISPATCH_QUEUE_SERIAL); - dispatch_queue_t priority = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); - dispatch_set_target_queue(priority, _cacheInfoQueue); - - _frozenCacheInfoQueue = dispatch_queue_create("com.enormego.egocache.info.frozen", DISPATCH_QUEUE_SERIAL); - priority = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); - dispatch_set_target_queue(priority, _frozenCacheInfoQueue); - - _diskQueue = dispatch_queue_create("com.enormego.egocache.disk", DISPATCH_QUEUE_CONCURRENT); - priority = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); - dispatch_set_target_queue(priority, _diskQueue); - + _cacheInfoQueue = dispatch_queue_create("com.enormego.egocache.info", DISPATCH_QUEUE_SERIAL); + dispatch_queue_t priority = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); + dispatch_set_target_queue(_cacheInfoQueue, priority); + + _frozenCacheInfoQueue = dispatch_queue_create("com.enormego.egocache.info.frozen", DISPATCH_QUEUE_SERIAL); + priority = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); + dispatch_set_target_queue(_frozenCacheInfoQueue, priority); + + _diskQueue = dispatch_queue_create("com.enormego.egocache.disk", DISPATCH_QUEUE_CONCURRENT); + priority = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); + dispatch_set_target_queue(_diskQueue, priority ); + _directory = cacheDirectory; From b9e7537ef2ca3a8d046363db9c8e85a1c7ece292 Mon Sep 17 00:00:00 2001 From: Cory Smith Date: Tue, 15 Dec 2015 17:11:45 -0700 Subject: [PATCH 07/11] Excluding cache from iCloud backups --- EGOCache.m | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/EGOCache.m b/EGOCache.m index fb4f602..6b2828c 100755 --- a/EGOCache.m +++ b/EGOCache.m @@ -106,7 +106,14 @@ - (instancetype)initWithCacheDirectory:(NSString*)cacheDirectory { } [[NSFileManager defaultManager] createDirectoryAtPath:_directory withIntermediateDirectories:YES attributes:nil error:NULL]; - + NSURL *url = [NSURL fileURLWithPath:_directory]; + NSError *error = nil; + BOOL success = [url setResourceValue:[NSNumber numberWithBool: YES] + forKey: NSURLIsExcludedFromBackupKey error: &error]; + if(!success){ + NSLog(@"Error excluding %@ from backup %@", [url lastPathComponent], error); + } + NSTimeInterval now = [[NSDate date] timeIntervalSinceReferenceDate]; NSMutableArray* removedKeys = [[NSMutableArray alloc] init]; From 965b44b486b8529b5ee1a2dc0e1edd957ecda89d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Arratia=20Garc=C3=ADa?= Date: Wed, 8 Jun 2016 13:18:08 +0200 Subject: [PATCH 08/11] - Added methods to allow refresh of cache expiry time when reading variable --- EGOCache.h | 7 +++++ EGOCache.m | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/EGOCache.h b/EGOCache.h index a7a7388..ad9137d 100755 --- a/EGOCache.h +++ b/EGOCache.h @@ -55,12 +55,14 @@ - (NSString* __nullable)pathForKey:(NSString* __nonnull)key; - (NSData* __nullable)dataForKey:(NSString* __nonnull)key; +- (NSData* __nullable)dataForKey:(NSString* __nonnull)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut; - (void)setData:(NSData* __nonnull)data forKey:(NSString* __nonnull)key; - (void)setData:(NSData* __nonnull)data forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; - (void)setData:(NSData* __nonnull)data forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; - (void)setData:(NSData* __nonnull)data forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; - (NSString* __nullable)stringForKey:(NSString* __nonnull)key; +- (NSString* __nullable)stringForKey:(NSString* __nonnull)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut; - (void)setString:(NSString* __nonnull)aString forKey:(NSString* __nonnull)key; - (void)setString:(NSString* __nonnull)aString forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; - (void)setString:(NSString* __nonnull)aString forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; @@ -71,12 +73,14 @@ #if TARGET_OS_IPHONE - (UIImage* __nullable)imageForKey:(NSString* __nonnull)key; +- (UIImage* __nullable)imageForKey:(NSString* __nonnull)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut; - (void)setImage:(UIImage* __nonnull)anImage forKey:(NSString* __nonnull)key; - (void)setImage:(UIImage* __nonnull)anImage forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; - (void)setImage:(UIImage* __nonnull)anImage forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; - (void)setImage:(UIImage* __nonnull)anImage forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; #else - (NSImage* __nullable)imageForKey:(NSString* __nonnull)key; +- (NSImage* __nullable)imageForKey:(NSString* __nonnull)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut; - (void)setImage:(NSImage* __nonnull)anImage forKey:(NSString* __nonnull)key; - (void)setImage:(UIImage* __nonnull)anImage forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; - (void)setImage:(NSImage* __nonnull)anImage forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; @@ -84,13 +88,16 @@ #endif - (NSData* __nullable)plistForKey:(NSString* __nonnull)key; +- (NSData* __nullable)plistForKey:(NSString* __nonnull)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut; - (void)setPlist:(nonnull id)plistObject forKey:(NSString* __nonnull)key; - (void)setPlist:(nonnull id)plistObject forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; - (void)setPlist:(nonnull id)plistObject forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; - (void)setPlist:(nonnull id)plistObject forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync; - (id __nullable)jsonObjectForKey:(NSString* __nonnull)key; +- (id __nullable)jsonObjectForKey:(NSString* __nonnull)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut; - (id __nullable)mutableJsonObjectForKey:(NSString* __nonnull)key; +- (id __nullable)mutableJsonObjectForKey:(NSString* __nonnull)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut; - (void)setJson:(nonnull id)jsonObject forKey:(NSString* __nonnull)key; - (void)setJson:(nonnull id)jsonObject forKey:(NSString* __nonnull)key synchronous:(BOOL)sync; - (void)setJson:(nonnull id)jsonObject forKey:(NSString* __nonnull)key withTimeoutInterval:(NSTimeInterval)timeoutInterval; diff --git a/EGOCache.m b/EGOCache.m index 3443916..c8e9e05 100755 --- a/EGOCache.m +++ b/EGOCache.m @@ -310,6 +310,17 @@ - (NSData*)dataForKey:(NSString*)key { } } +- (NSData*)dataForKey:(NSString*)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut{ + NSData* tmpObject = [self dataForKey:key]; + + if (tmpObject){ + //If object exists we reset the timeout to the established value + [self setCacheTimeoutInterval:newTimeOut forKey:key]; + } + + return tmpObject; +} + #pragma mark - #pragma mark String methods @@ -317,6 +328,10 @@ - (NSString*)stringForKey:(NSString*)key { return [[NSString alloc] initWithData:[self dataForKey:key] encoding:NSUTF8StringEncoding]; } +- (NSString*)stringForKey:(NSString*)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut{ + return [[NSString alloc] initWithData:[self dataForKey:key RestartTimeOutInterval:newTimeOut] encoding:NSUTF8StringEncoding]; +} + - (void)setString:(NSString*)aString forKey:(NSString*)key { [self setData:[aString dataUsingEncoding:NSUTF8StringEncoding] forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } @@ -334,7 +349,7 @@ - (void)setString:(NSString*)aString forKey:(NSString*)key withTimeoutInterval:( } #pragma mark - -#pragma mark Image methds +#pragma mark Image methods #if TARGET_OS_IPHONE @@ -344,12 +359,23 @@ - (UIImage*)imageForKey:(NSString*)key { @try { image = [NSKeyedUnarchiver unarchiveObjectWithFile:cachePathForKey(_directory, key)]; } @catch (NSException* e) { - // Surpress any unarchiving exceptions and continue with nil + // Supress any unarchiving exceptions and continue with nil } return image; } +- (UIImage*)imageForKey:(NSString*)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut{ + UIImage* tmpObject = [self imageForKey:key]; + + if (tmpObject){ + //If object exists we reset the timeout to the established value + [self setCacheTimeoutInterval:newTimeOut forKey:key]; + } + + return tmpObject; +} + - (void)setImage:(UIImage*)anImage forKey:(NSString*)key{ [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } @@ -378,6 +404,17 @@ - (NSImage*)imageForKey:(NSString*)key { return [[NSImage alloc] initWithData:[self dataForKey:key]]; } +- (NSImage*)imageForKey:(NSString*)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut{ + NSImage* tmpObject = [self imageForKey:key]; + + if (tmpObject){ + //If object exists we reset the timeout to the established value + [self setCacheTimeoutInterval:newTimeOut forKey:key]; + } + + return tmpObject; +} + - (void)setImage:(NSImage*)anImage forKey:(NSString*)key { [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval]; } @@ -407,6 +444,17 @@ - (NSData*)plistForKey:(NSString*)key; { error:nil]; } +- (NSData*)plistForKey:(NSString*)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut{ + NSData* tmpObject = [self plistForKey:key]; + + if (tmpObject){ + //If object exists we reset the timeout to the established value + [self setCacheTimeoutInterval:newTimeOut forKey:key]; + } + + return tmpObject; +} + - (void)setPlist:(id)plistObject forKey:(NSString*)key{ [self setPlist:plistObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } @@ -431,16 +479,39 @@ - (void)setPlist:(id)plistObject forKey:(NSString*)key withTimeoutInterval:(NSTi #pragma mark - #pragma mark JSON Object methods + - (id)jsonObjectForKey:(NSString*)key{ NSData* jsonData = [self dataForKey:key]; return [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil]; } +- (id)jsonObjectForKey:(NSString*)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut{ + id tmpObject = [self jsonObjectForKey:key]; + + if (tmpObject){ + //If object exists we reset the timeout to the established value + [self setCacheTimeoutInterval:newTimeOut forKey:key]; + } + + return tmpObject; +} + - (id)mutableJsonObjectForKey:(NSString*)key{ NSData* jsonData = [self dataForKey:key]; return [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil]; } +- (id)mutableJsonObjectForKey:(NSString*)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut{ + id tmpObject = [self mutableJsonObjectForKey:key]; + + if (tmpObject){ + //If object exists we reset the timeout to the established value + [self setCacheTimeoutInterval:newTimeOut forKey:key]; + } + + return tmpObject; +} + - (void)setJson:(id)jsonObject forKey:(NSString*)key{ [self setJson:jsonObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } @@ -469,6 +540,17 @@ - (void)setJson:(id)jsonObject forKey:(NSString*)key withTimeoutInterval:(NSTime } } +- (id)objectForKey:(NSString*)key RestartTimeOutInterval:(NSTimeInterval)newTimeOut{ + id tmpObject = [self objectForKey:key]; + + if (tmpObject){ + //If object exists we reset the timeout to the established value + [self setCacheTimeoutInterval:newTimeOut forKey:key]; + } + + return tmpObject; +} + - (void)setObject:(id)anObject forKey:(NSString*)key{ [self setObject:anObject forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } From 4b7404a8402cbf313de01a5d8713639e8cc83887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Arratia=20Garc=C3=ADa?= Date: Wed, 28 Sep 2016 12:24:45 +0200 Subject: [PATCH 09/11] Simplified method setImage --- EGOCache.m | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/EGOCache.m b/EGOCache.m index c8e9e05..91b5bc6 100755 --- a/EGOCache.m +++ b/EGOCache.m @@ -422,8 +422,7 @@ - (void)setImage:(UIImage*)anImage forKey:(NSString*)key synchronous:(BOOL)sync{ [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:sync]; } - (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval { - [self setData:[[[anImage representations] objectAtIndex:0] representationUsingType:NSPNGFileType properties:nil] - forKey:key withTimeoutInterval:timeoutInterval synchronous:NO]; + [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } - (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ [self setData:[[[anImage representations] objectAtIndex:0] representationUsingType:NSPNGFileType properties:nil] From 4a976133dfd79c069a0195d557244687cfd2cf96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Arratia=20Garc=C3=ADa?= Date: Fri, 15 Sep 2017 14:40:04 +0200 Subject: [PATCH 10/11] - Fix some nullability issues in XCode --- EGOCache.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EGOCache.m b/EGOCache.m index c7e7c0d..19a5e71 100755 --- a/EGOCache.m +++ b/EGOCache.m @@ -430,8 +430,8 @@ - (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NS [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } - (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ - [self setData:[[[anImage representations] objectAtIndex:0] representationUsingType:NSPNGFileType properties:nil] - forKey:key withTimeoutInterval:timeoutInterval synchronous:sync]; + NSBitmapImageRep *bitmapRep = (id)anImage.representations.firstObject; + [self setData:[bitmapRep representationUsingType:NSPNGFileType properties:@{}] forKey:key withTimeoutInterval:timeoutInterval]; } #endif From 88195aa72a09df448914b3ed14307bb06f70698d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Arratia=20Garc=C3=ADa?= Date: Fri, 1 Dec 2017 14:56:49 +0100 Subject: [PATCH 11/11] -update from enormego repo --- EGOCache.h | 6 +++++- EGOCache.m | 3 +-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/EGOCache.h b/EGOCache.h index 3d81765..96f48f2 100755 --- a/EGOCache.h +++ b/EGOCache.h @@ -27,7 +27,11 @@ #import #if TARGET_OS_IPHONE -#import + #import +#endif + +#if TARGET_OS_OSX + #import #endif #if !__has_feature(nullability) diff --git a/EGOCache.m b/EGOCache.m index 19a5e71..0080e09 100755 --- a/EGOCache.m +++ b/EGOCache.m @@ -430,8 +430,7 @@ - (void)setImage:(NSImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NS [self setImage:anImage forKey:key withTimeoutInterval:self.defaultTimeoutInterval synchronous:NO]; } - (void)setImage:(UIImage*)anImage forKey:(NSString*)key withTimeoutInterval:(NSTimeInterval)timeoutInterval synchronous:(BOOL)sync{ - NSBitmapImageRep *bitmapRep = (id)anImage.representations.firstObject; - [self setData:[bitmapRep representationUsingType:NSPNGFileType properties:@{}] forKey:key withTimeoutInterval:timeoutInterval]; + [self setData:[[NSBitmapImageRep imageRepWithData:anImage.TIFFRepresentation] representationUsingType:NSPNGFileType properties:@{ }] forKey:key withTimeoutInterval:timeoutInterval]; } #endif