123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- #import "ReaderThumbCache.h"
- #import "ReaderThumbQueue.h"
- #import "ReaderThumbFetch.h"
- #import "ReaderThumbView.h"
- @implementation ReaderThumbCache
- {
- NSCache *thumbCache;
- }
- #pragma mark - Constants
- #define CACHE_SIZE 2097152
- #pragma mark - ReaderThumbCache class methods
- + (ReaderThumbCache *)sharedInstance
- {
- static dispatch_once_t predicate = 0;
- static ReaderThumbCache *object = nil;
- dispatch_once(&predicate, ^{ object = [self new]; });
- return object;
- }
- + (NSString *)appCachesPath
- {
- static dispatch_once_t predicate = 0;
- static NSString *theCachesPath = nil;
- dispatch_once(&predicate,
- ^{
- NSArray *cachesPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- theCachesPath = [[cachesPaths objectAtIndex:0] copy];
- });
- return theCachesPath;
- }
- + (NSString *)thumbCachePathForGUID:(NSString *)guid
- {
- NSString *cachesPath = [ReaderThumbCache appCachesPath];
- return [cachesPath stringByAppendingPathComponent:guid];
- }
- + (void)createThumbCacheWithGUID:(NSString *)guid
- {
- NSFileManager *fileManager = [NSFileManager new];
- NSString *cachePath = [ReaderThumbCache thumbCachePathForGUID:guid];
- [fileManager createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:NULL];
- }
- + (void)removeThumbCacheWithGUID:(NSString *)guid
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
- ^{
- NSFileManager *fileManager = [NSFileManager new];
- NSString *cachePath = [ReaderThumbCache thumbCachePathForGUID:guid];
- [fileManager removeItemAtPath:cachePath error:NULL];
- });
- }
- + (void)touchThumbCacheWithGUID:(NSString *)guid
- {
- NSFileManager *fileManager = [NSFileManager new];
- NSString *cachePath = [ReaderThumbCache thumbCachePathForGUID:guid];
- NSDictionary *attributes = [NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate];
- [fileManager setAttributes:attributes ofItemAtPath:cachePath error:NULL];
- }
- + (void)purgeThumbCachesOlderThan:(NSTimeInterval)age
- {
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
- ^{
- NSDate *now = [NSDate date];
- NSString *cachesPath = [ReaderThumbCache appCachesPath];
- NSFileManager *fileManager = [NSFileManager new];
- NSArray *cachesList = [fileManager contentsOfDirectoryAtPath:cachesPath error:NULL];
- if (cachesList != nil)
- {
- for (NSString *cacheName in cachesList)
- {
- if (cacheName.length == 36)
- {
- NSString *cachePath = [cachesPath stringByAppendingPathComponent:cacheName];
- NSDictionary *attributes = [fileManager attributesOfItemAtPath:cachePath error:NULL];
- NSDate *cacheDate = [attributes objectForKey:NSFileModificationDate];
- NSTimeInterval seconds = [now timeIntervalSinceDate:cacheDate];
- if (seconds > age)
- {
- [fileManager removeItemAtPath:cachePath error:NULL];
- #ifdef DEBUG
- NSLog(@"%s purged %@", __FUNCTION__, cacheName);
- #endif
- }
- }
- }
- }
- });
- }
- #pragma mark - ReaderThumbCache instance methods
- - (instancetype)init
- {
- if ((self = [super init]))
- {
- thumbCache = [NSCache new];
- [thumbCache setName:@"ReaderThumbCache"];
- [thumbCache setTotalCostLimit:CACHE_SIZE];
- }
- return self;
- }
- - (id)thumbRequest:(ReaderThumbRequest *)request priority:(BOOL)priority
- {
- @synchronized(thumbCache)
- {
- id object = [thumbCache objectForKey:request.cacheKey];
- if (object == nil)
- {
- object = [NSNull null];
- [thumbCache setObject:object forKey:request.cacheKey cost:2];
- ReaderThumbFetch *thumbFetch = [[ReaderThumbFetch alloc] initWithRequest:request];
- [thumbFetch setQueuePriority:(priority ? NSOperationQueuePriorityNormal : NSOperationQueuePriorityLow)];
- request.thumbView.operation = thumbFetch;
- [[ReaderThumbQueue sharedInstance] addLoadOperation:thumbFetch];
- }
- return object;
- }
- }
- - (void)setObject:(UIImage *)image forKey:(NSString *)key
- {
- @synchronized(thumbCache)
- {
- NSUInteger bytes = (image.size.width * image.size.height * 4.0f);
- [thumbCache setObject:image forKey:key cost:bytes];
- }
- }
- - (void)removeObjectForKey:(NSString *)key
- {
- @synchronized(thumbCache)
- {
- [thumbCache removeObjectForKey:key];
- }
- }
- - (void)removeNullForKey:(NSString *)key
- {
- @synchronized(thumbCache)
- {
- id object = [thumbCache objectForKey:key];
- if ([object isMemberOfClass:[NSNull class]])
- {
- [thumbCache removeObjectForKey:key];
- }
- }
- }
- - (void)removeAllObjects
- {
- @synchronized(thumbCache)
- {
- [thumbCache removeAllObjects];
- }
- }
- @end
|