|
| 1 | +// |
| 2 | +// TQImageCache.m |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Tang Qiao on 12-5-9. |
| 6 | +// Copyright (c) 2012年 blog.devtang.com. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "TQImageCache.h" |
| 10 | + |
| 11 | +@interface TQImageCache() |
| 12 | + |
| 13 | +@property (nonatomic, retain) NSString * cachePath; |
| 14 | +@property (nonatomic, retain) NSFileManager * fileManager; |
| 15 | +@property (nonatomic, retain) NSMutableDictionary * memoryCache; |
| 16 | +@property (nonatomic, retain) NSMutableArray *memoryCacheKeys; |
| 17 | + |
| 18 | +@end |
| 19 | + |
| 20 | +@implementation TQImageCache; |
| 21 | + |
| 22 | +@synthesize maxMemoryCacheNumber; |
| 23 | +@synthesize cachePath; |
| 24 | +@synthesize fileManager; |
| 25 | +@synthesize memoryCache; |
| 26 | +@synthesize memoryCacheKeys; |
| 27 | + |
| 28 | +static BOOL debugMode = YES; |
| 29 | + |
| 30 | +#ifdef DEBUG |
| 31 | +#define debugLog(...) NSLog(__VA_ARGS__) |
| 32 | +#define debugMethod() NSLog(@"%s", __func__) |
| 33 | +#else |
| 34 | +#define debugLog(...) |
| 35 | +#define debugMethod() |
| 36 | +#endif |
| 37 | + |
| 38 | +- (id) init { |
| 39 | + return [self initWithCachePath:@"TQImageCache" andMaxMemoryCacheNumber:50]; |
| 40 | +} |
| 41 | + |
| 42 | +- (id) initWithCachePath:(NSString*)path andMaxMemoryCacheNumber:(NSInteger)maxNumber { |
| 43 | + if ([path hasPrefix:NSTemporaryDirectory()]) { |
| 44 | + self.cachePath = path; |
| 45 | + } else { |
| 46 | + if ([path length] != 0) { |
| 47 | + self.cachePath = [NSTemporaryDirectory() stringByAppendingPathComponent:path]; |
| 48 | + } else { |
| 49 | + return nil; |
| 50 | + } |
| 51 | + } |
| 52 | + self.maxMemoryCacheNumber = maxNumber; |
| 53 | + |
| 54 | + if (self = [super init]) { |
| 55 | + self.fileManager = [NSFileManager defaultManager]; |
| 56 | + if ([self.fileManager fileExistsAtPath:self.cachePath isDirectory:nil] == NO) { |
| 57 | + // create the directory |
| 58 | + BOOL res = [self.fileManager createDirectoryAtPath:self.cachePath withIntermediateDirectories:YES attributes:nil error:nil]; |
| 59 | + if (!res) { |
| 60 | + debugLog(@"file cache directory create failed! The path is %@", self.cachePath); |
| 61 | + return nil; |
| 62 | + } |
| 63 | + } |
| 64 | + self.memoryCache = [[[NSMutableDictionary alloc] initWithCapacity:maxMemoryCacheNumber] autorelease]; |
| 65 | + self.memoryCacheKeys = [[[NSMutableArray alloc] initWithCapacity:maxMemoryCacheNumber] autorelease]; |
| 66 | + return self; |
| 67 | + } |
| 68 | + return self; |
| 69 | +} |
| 70 | + |
| 71 | +- (void)clear { |
| 72 | + self.memoryCache = [[[NSMutableDictionary alloc] initWithCapacity:maxMemoryCacheNumber] autorelease]; |
| 73 | + self.memoryCacheKeys = [[[NSMutableArray alloc] initWithCapacity:maxMemoryCacheNumber] autorelease]; |
| 74 | + |
| 75 | + // remove all the file in temporary |
| 76 | + NSArray * files = [self.fileManager contentsOfDirectoryAtPath:self.cachePath error:nil]; |
| 77 | + for (NSString * file in files) { |
| 78 | + if (debugMode) { |
| 79 | + debugLog(@"remove cache file: %@", file); |
| 80 | + } |
| 81 | + [self.fileManager removeItemAtPath:file error:nil]; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +- (void) putImage:(NSData *) imageData withName:(NSString*)imageName { |
| 86 | + [self.memoryCache setObject:imageData forKey:imageName]; |
| 87 | + [self.memoryCacheKeys addObject:imageName]; |
| 88 | + if ([self.memoryCache count] > self.maxMemoryCacheNumber) { |
| 89 | + // Retain the `key` varible otherwise |
| 90 | + // it maybe dealloc after it is removed from memoryCahceKyes array |
| 91 | + NSString * key = [[self.memoryCacheKeys objectAtIndex:0] retain]; |
| 92 | + [self.memoryCache removeObjectForKey:key]; |
| 93 | + [self.memoryCacheKeys removeObjectAtIndex:0]; |
| 94 | + if (debugMode) { |
| 95 | + debugLog(@"remove oldest cache from memory: %@", key); |
| 96 | + } |
| 97 | + [key release]; |
| 98 | + } |
| 99 | + NSString * path = [self.cachePath stringByAppendingPathComponent:imageName]; |
| 100 | + [imageData writeToFile:path atomically:YES]; |
| 101 | + if (debugMode) { |
| 102 | + debugLog(@"TQImageCache put cache image to %@", path); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +- (NSData *) getImage:(NSString *)imageName { |
| 107 | + NSData * data = [self.memoryCache objectForKey:imageName]; |
| 108 | + if (data != nil) { |
| 109 | + if (debugMode) { |
| 110 | + debugLog(@"TQImageCache hit cache from memory: %@", imageName); |
| 111 | + } |
| 112 | + return data; |
| 113 | + } |
| 114 | + NSString * path = [self.cachePath stringByAppendingPathComponent:imageName]; |
| 115 | + if ([self.fileManager fileExistsAtPath:path]) { |
| 116 | + if (debugMode) { |
| 117 | + debugLog(@"TQImageCache hit cache from file %@", path); |
| 118 | + } |
| 119 | + return [NSData dataWithContentsOfFile:path]; |
| 120 | + } |
| 121 | + return nil; |
| 122 | +} |
| 123 | + |
| 124 | +@end |
0 commit comments