Skip to content

Commit

Permalink
standardizes on using NSURL for files throughout OGImageCache
Browse files Browse the repository at this point in the history
Leaving `filePathForKey:` for backwards-compatibility. (Re-implemented in terms of `fileURLForKey:`
  • Loading branch information
artgillespie committed Aug 22, 2013
1 parent 8b80953 commit a379186
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
6 changes: 2 additions & 4 deletions OGImage/OGImageCache.h
Expand Up @@ -19,6 +19,8 @@ typedef void (^OGImageCacheCompletionBlock)(__OGImage *image);

+ (NSString *)filePathForKey:(NSString *)key;

+ (NSURL *)fileURLForKey:(NSString *)key;

/**
* Check in-memory and on-disk caches for image corresponding to `key`. `block`
* called on main queue when check is complete. If `image` parameter is `nil`,
Expand Down Expand Up @@ -46,10 +48,6 @@ 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
Expand Down
33 changes: 12 additions & 21 deletions OGImage/OGImageCache.m
Expand Up @@ -12,18 +12,6 @@

static OGImageCache *OGImageCacheShared;

NSString *OGImageCachePath() {
// generate the cache path: <app>/Library/Application Support/<bundle identifier>/OGImageCache,
// creating the directories as needed
NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
if (nil == array || 0 == [array count]) {
return nil;
}
NSString *cachePath = [[array[0] stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]] stringByAppendingPathComponent:@"OGImageCache"];
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil];
return cachePath;
}

NSURL *OGImageCacheURL() {
// generate the cache path: <app>/Library/Application Support/<bundle identifier>/OGImageCache,
// creating the directories as needed
Expand Down Expand Up @@ -62,7 +50,11 @@ + (NSString *)MD5:(NSString *)string {
}

+ (NSString *)filePathForKey:(NSString *)key {
return [OGImageCachePath() stringByAppendingPathComponent:[OGImageCache MD5:key]];
return [[OGImageCache fileURLForKey:key] path];
}

+ (NSURL *)fileURLForKey:(NSString *)key {
return [OGImageCacheURL() URLByAppendingPathComponent:[OGImageCache MD5:key]];
}

- (id)init {
Expand Down Expand Up @@ -97,8 +89,8 @@ - (void)imageForKey:(NSString *)key block:(OGImageCacheCompletionBlock)block {
dispatch_suspend(_cacheFileTasksQueue);
dispatch_async(_cacheFileReadQueue, ^{
// Check to see if the image is cached locally
NSString *cachePath = [OGImageCache filePathForKey:(key)];
__OGImage *image = [[__OGImage alloc] initWithDataAtURL:[NSURL fileURLWithPath:cachePath]];
NSURL *cacheURL = [OGImageCache fileURLForKey:(key)];
__OGImage *image = [[__OGImage alloc] initWithDataAtURL:cacheURL];
// if we have the image in the on-disk cache, store it to the in-memory cache
if (nil != image) {
[_memoryCache setObject:image forKey:key];
Expand All @@ -116,7 +108,7 @@ - (void)setImage:(__OGImage *)image forKey:(NSString *)key {
NSParameterAssert(nil != key);
[_memoryCache setObject:image forKey:key];
dispatch_async(_cacheFileTasksQueue, ^{
NSURL *fileURL = [NSURL fileURLWithPath:[OGImageCache filePathForKey:key]];
NSURL *fileURL = [OGImageCache fileURLForKey:key];
NSError *error;
if(![image writeToURL:fileURL error:&error]) {
NSLog(@"[OGImageCache ERROR] failed to write image with error %@ %s %d", error, __FILE__, __LINE__);
Expand All @@ -131,9 +123,8 @@ - (void)setImage:(__OGImage *)image forKey:(NSString *)key {
- (void)purgeCache:(BOOL)wait {
[_memoryCache removeAllObjects];
void (^purgeFilesBlock)(void) = ^{
NSString *cachePath = OGImageCachePath();
for (NSString *file in [[NSFileManager defaultManager] enumeratorAtPath:cachePath]) {
[[NSFileManager defaultManager] removeItemAtPath:[cachePath stringByAppendingPathComponent:file] error:nil];
for (NSURL *url in [[NSFileManager defaultManager] enumeratorAtURL:OGImageCacheURL() includingPropertiesForKeys:nil options:0 errorHandler:nil]) {
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
}
};
if (YES == wait) {
Expand All @@ -148,10 +139,10 @@ - (void)purgeCacheForKey:(NSString *)key andWait:(BOOL)wait {

[self purgeMemoryCacheForKey:key andWait:wait];

NSString *cachedFilePath = [[self class] filePathForKey:key];
NSURL *cachedFileURL = [[self class] fileURLForKey:key];

void (^purgeFileBlock)(void) =^{
[[NSFileManager defaultManager] removeItemAtPath:cachedFilePath error:nil];
[[NSFileManager defaultManager] removeItemAtURL:cachedFileURL error:nil];
};

if (YES == wait) {
Expand Down

0 comments on commit a379186

Please sign in to comment.