Skip to content

Commit

Permalink
Example now uses rolling cache with GVCache
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Renskers committed Jan 30, 2013
1 parent 60bd90f commit b5f17be
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
29 changes: 23 additions & 6 deletions Example/LastFmCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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];
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!)

Expand Down

0 comments on commit b5f17be

Please sign in to comment.