Skip to content

Instantly share code, notes, and snippets.

@andrei512
Created September 7, 2012 16:46
Show Gist options
  • Save andrei512/3667691 to your computer and use it in GitHub Desktop.
Save andrei512/3667691 to your computer and use it in GitHub Desktop.
JewelBox
//
// JewelBox.h
// Clomp
//
// Created by Andrei on 8/12/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface JewelBox : NSObject
@property (nonatomic, retain) NSFileManager *fileManager;
+ (JewelBox *)box;
- (BOOL)hasJewelFromURL:(NSString *)url;
- (id)jewelForURL:(NSString *)url;
- (UIImage *)imageForURL:(NSString *)url;
- (void)storeJewel:(NSData *)jewel forURL:(NSString *)url;
- (void)storeImage:(UIImage *)image forURL:(NSString *)url;
- (void)throwAwayOldJewels;
- (void)throwAwayAllJewels;
// JewelBox helpers
- (NSString *)cachesPath;
- (NSString *)pathForURL:(NSString *)url;
@end
//
// JewelBox.m
// Clomp
//
// Created by Andrei on 8/12/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "JewelBox.h"
#import "NGUtils.h"
#define TenDaysInSeconds (60 * 60 * 24 * 10)
@implementation JewelBox
@synthesize fileManager;
+ (JewelBox *)box {
static JewelBox *box = nil;
if (box == nil) {
box = [JewelBox new];
}
return box;
}
- (id)init {
if (self = [super init]) {
self.fileManager = [[[NSFileManager alloc] init] autorelease];
}
return self;
}
- (NSString *)cachesPath {
static NSString *cachesPath = nil;
if (cachesPath == nil) {
NSArray *urls = [self.fileManager URLsForDirectory:NSCachesDirectory
inDomains:NSUserDomainMask];
cachesPath = [[(NSURL *)[urls lastObject] path] retain];
}
return cachesPath;
}
- (NSString *)pathForURL:(NSString *)url {
if (url) {
return [[self cachesPath] stringByAppendingPathComponent:[url simpleHash]];
}
return nil;
}
- (BOOL)hasJewelFromURL:(NSString *)url {
return [self.fileManager fileExistsAtPath:[self pathForURL:url]];
}
- (id)jewelForURL:(NSString *)url {
return [NSData dataWithContentsOfFile:[self pathForURL:url]];
}
- (UIImage *)imageForURL:(NSString *)url {
return [UIImage imageWithContentsOfFile:[self pathForURL:url]];
}
- (void)storeJewel:(NSData *)jewel forURL:(NSString *)url {
NSLog(@"saving file ...");
[jewel writeToFile:[self pathForURL:url]
atomically:YES];
}
- (void)storeImage:(UIImage *)image forURL:(NSString *)url {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSData *jewel = UIImagePNGRepresentation(image);
[self storeJewel:jewel forURL:url];
});
}
- (void)throwAwayJewlsOlderThan:(double)seconds {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// NSFileManager is not thread safe
NSFileManager *fileManeger = [[NSFileManager alloc] init];
NSMutableArray *oldJewels = [NSMutableArray array];
NSDirectoryEnumerator *dirEnum = [fileManeger enumeratorAtPath:[self cachesPath]];
NSURL *url = nil;
NSDate *currentDate = [NSDate date];
while ((url = [dirEnum nextObject])) {
NSDate *modDate = [[dirEnum fileAttributes] objectForKey:NSFileModificationDate];
if (modDate &&
[currentDate compare:[modDate dateByAddingTimeInterval:seconds]] == NSOrderedDescending) {
[oldJewels addObject:url];
}
}
for (NSString *path in oldJewels) {
[fileManeger removeItemAtPath:[[self cachesPath] stringByAppendingPathComponent:path]
error:NULL];
}
[fileManeger release];
});
}
- (void)throwAwayOldJewels {
[self throwAwayJewlsOlderThan:TenDaysInSeconds];
}
- (void)throwAwayAllJewels {
[self throwAwayJewlsOlderThan:0];
}
- (void)dealloc {
self.fileManager = nil;
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment