Permalink
Browse files

add date-based purging to `OGImageCache`

  • Loading branch information...
1 parent ae3a343 commit 6647323dcd4ed9cd03fa2e4d1a881fbfc9a19ddc @artgillespie committed Aug 20, 2013
Showing with 47 additions and 1 deletion.
  1. +7 −0 OGImage/OGImageCache.h
  2. +33 −0 OGImage/OGImageCache.m
  3. +7 −1 OGImageDemo/OGImageDemo/OGViewController.m
@@ -46,5 +46,12 @@ typedef void (^OGImageCacheCompletionBlock)(__OGImage *image);
*/
- (void)purgeMemoryCacheForKey:(NSString *)key andWait:(BOOL)wait;
+/**
+ * Remove cached images from disk that haven't been accessed since `date`
+ * If `wait` is `YES` this will block the calling thread until the purge
+ * is complete.
+ */
+- (void)purgeDiskCacheWithDate:(NSDate *)date wait:(BOOL)wait;
+
@end
@@ -24,6 +24,18 @@
return cachePath;
}
+NSURL *OGImageCacheURL() {
+ // generate the cache path: <app>/Library/Application Support/<bundle identifier>/OGImageCache,
+ // creating the directories as needed
+ NSArray *array = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
+ if (nil == array || 0 == [array count]) {
+ return nil;
+ }
+ NSURL *cacheURL = [[array[0] URLByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]] URLByAppendingPathComponent:@"OGImageCache"];
+ [[NSFileManager defaultManager] createDirectoryAtURL:cacheURL withIntermediateDirectories:YES attributes:nil error:nil];
+ return cacheURL;
+}
+
@implementation OGImageCache {
NSCache *_memoryCache;
dispatch_queue_t _cacheFileTasksQueue;
@@ -142,4 +154,25 @@ - (void)purgeMemoryCacheForKey:(NSString *)key andWait:(BOOL)wait {
[_memoryCache removeObjectForKey:key];
}
+- (void)purgeDiskCacheWithDate:(NSDate *)date wait:(BOOL)wait {
+ void (^purgeFilesBlock)(void) = ^{
+ NSURL *cacheURL = OGImageCacheURL();
+ for (NSURL *fileURL in [[NSFileManager defaultManager] enumeratorAtURL:cacheURL includingPropertiesForKeys:@[NSURLContentAccessDateKey] options:NSDirectoryEnumerationSkipsHiddenFiles errorHandler:nil]) {
+ NSDate *accessDate;
+ if (NO == [fileURL getResourceValue:&accessDate forKey:NSURLContentAccessDateKey error:nil]) {
+ return;
+ }
+ if (NSOrderedDescending == [date compare:accessDate]) {
+ [[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil];
+ }
+ }
+ };
+
+ if (YES == wait) {
+ dispatch_sync(_cacheFileTasksQueue, purgeFilesBlock);
+ } else {
+ dispatch_async(_cacheFileTasksQueue, purgeFilesBlock);
+ }
+}
+
@end
@@ -8,6 +8,7 @@
#import "OGViewController.h"
#import "OGScaledImage.h"
+#import "OGImageCache.h"
#import "OGImageTableViewCell.h"
@interface OGViewController ()
@@ -20,8 +21,13 @@ @implementation OGViewController {
- (void)viewDidLoad {
[super viewDidLoad];
+ // purge the disk cache of any image that hasn't been
+ // accessed more recently than 2 minutes ago. This is pretty contrived;
+ // You'd probably do this in `application:didFinishLaunchingWithOptions:` or
+ // `applicationDidEnterBackground` or similar.
+ NSDate *since = [NSDate dateWithTimeIntervalSinceNow:-120.];
+ [[OGImageCache shared] purgeDiskCacheWithDate:since wait:YES];
[self loadJSON];
- // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {

0 comments on commit 6647323

Please sign in to comment.