From b5f17be108102e9835d155e72e05a7afe4252c9e Mon Sep 17 00:00:00 2001 From: Kevin Renskers Date: Wed, 30 Jan 2013 19:00:26 +0000 Subject: [PATCH] Example now uses rolling cache with GVCache --- Example/LastFmCache.m | 29 +++++++++++++++++++++++------ README.md | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Example/LastFmCache.m b/Example/LastFmCache.m index 3d2d424..d26dfa1 100644 --- a/Example/LastFmCache.m +++ b/Example/LastFmCache.m @@ -34,12 +34,19 @@ - (void)cacheArray:(NSArray *)array forKey:(NSString *)key maxAge:(NSTimeInterva /* - * Better example: uses memory cache but falls back to disk cache - * with the help of EGOCache. + * Better example: uses memory cache but falls back to rolling + * disk cache with the help of GVCache. + * + * Rolling cache: cache will never be purged from disk after + * its expire time is reached. Instead, the cached version is + * used AND a request to Last.fm is made, updating the cache + * in the process. This way, you will never hit the situation + * where you're offline and the cached is cleared because + * it's 24 hours old, leaving you with nothing. * #import "LastFmCache.h" -#import "EGOCache.h" +#import "GVCache.h" @interface LastFmCache () @property (strong, nonatomic) NSCache *cache; @@ -55,6 +62,16 @@ - (id)init { return self; } +- (BOOL)cacheExpiredForKey:(NSString *)key requestParams:(NSDictionary *)params { + NSTimeInterval age = [[GVCache globalCache] ageForKey:key]; + NSTimeInterval maxAge = 10*60;//24*60*60; + if (age > maxAge) { + return YES; + } + + return NO; +} + - (NSArray *)cachedArrayForKey:(NSString *)key { // Get from memory NSArray *result = [self.cache objectForKey:key]; @@ -63,7 +80,7 @@ - (NSArray *)cachedArrayForKey:(NSString *)key { } // Get from disk - NSData *data = [[EGOCache globalCache] dataForKey:key]; + NSData *data = [[GVCache globalCache] dataForKey:key]; if (data) { // Save in memory result = [NSKeyedUnarchiver unarchiveObjectWithData:data]; @@ -78,9 +95,9 @@ - (void)cacheArray:(NSArray *)array forKey:(NSString *)key maxAge:(NSTimeInterva // Save in memory [self.cache setObject:array forKey:key]; - // Also save to disk + // Also save to disk. Timeout is 10 years, never automatically remove stuff from cache. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array]; - [[EGOCache globalCache] setData:data forKey:key withTimeoutInterval:maxAge]; + [[GVCache globalCache] setData:data forKey:key withTimeoutInterval:60*60*24*365*10]; } @end diff --git a/README.md b/README.md index b13d194..ea7dea1 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Loosely based on LastFMService from the [old Last.fm iPhone app](https://github. - Block based for easier usage - Only one dependency ([KissXML](https://github.com/robbiehanson/KissXML)) - Returns values in the correct data type (NSDate, NSURL, NSNumber, etc) -- Hook in your own caching methods (NSCache, Core Data, SYCache, EGOCache, ...) +- Hook in your own caching methods (GVCache, NSCache, Core Data, SYCache, EGOCache, ...) - Cancelable operations, perfect for when cells are scrolled off screen and you don't need to make the API calls after all - Actively developed and maintained (it's used in the official Last.fm Scrobbler app!)