Permalink
Browse files

standardizes on using `NSURL` for files throughout `OGImageCache`

Leaving `filePathForKey:` for backwards-compatibility. (Re-implemented in terms of `fileURLForKey:`
  • Loading branch information...
1 parent 8b80953 commit 736fed87b5a16d6248e6e0d3ced4ec1659720a5c @artgillespie committed Aug 22, 2013
Showing with 14 additions and 21 deletions.
  1. +2 −0 OGImage/OGImageCache.h
  2. +12 −21 OGImage/OGImageCache.m
View
2 OGImage/OGImageCache.h
@@ -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`,
View
33 OGImage/OGImageCache.m
@@ -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
@@ -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 {
@@ -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];
@@ -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__);
@@ -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) {
@@ -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) {

0 comments on commit 736fed8

Please sign in to comment.