| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257 |
- // PINCache is a modified version of TMCache
- // Modifications by Garrett Moon
- // Copyright (c) 2015 Pinterest. All rights reserved.
- #import "PINDiskCache.h"
- #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0
- #import <UIKit/UIKit.h>
- #endif
- #import <pthread.h>
- #import "PINOperationQueue.h"
- #define PINDiskCacheError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \
- [[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
- __LINE__, [error localizedDescription]); }
- static NSString * const PINDiskCachePrefix = @"com.pinterest.PINDiskCache";
- static NSString * const PINDiskCacheSharedName = @"PINDiskCacheShared";
- typedef NS_ENUM(NSUInteger, PINDiskCacheCondition) {
- PINDiskCacheConditionNotReady = 0,
- PINDiskCacheConditionReady = 1,
- };
- @interface PINDiskCache () {
- NSConditionLock *_instanceLock;
-
- PINDiskCacheSerializerBlock _serializer;
- PINDiskCacheDeserializerBlock _deserializer;
- }
- @property (assign) NSUInteger byteCount;
- @property (strong, nonatomic) NSURL *cacheURL;
- @property (strong, nonatomic) PINOperationQueue *operationQueue;
- @property (strong, nonatomic) NSMutableDictionary *dates;
- @property (strong, nonatomic) NSMutableDictionary *sizes;
- @end
- @implementation PINDiskCache
- @synthesize willAddObjectBlock = _willAddObjectBlock;
- @synthesize willRemoveObjectBlock = _willRemoveObjectBlock;
- @synthesize willRemoveAllObjectsBlock = _willRemoveAllObjectsBlock;
- @synthesize didAddObjectBlock = _didAddObjectBlock;
- @synthesize didRemoveObjectBlock = _didRemoveObjectBlock;
- @synthesize didRemoveAllObjectsBlock = _didRemoveAllObjectsBlock;
- @synthesize byteLimit = _byteLimit;
- @synthesize ageLimit = _ageLimit;
- @synthesize ttlCache = _ttlCache;
- #if TARGET_OS_IPHONE
- @synthesize writingProtectionOption = _writingProtectionOption;
- #endif
- #pragma mark - Initialization -
- - (instancetype)init
- {
- @throw [NSException exceptionWithName:@"Must initialize with a name" reason:@"PINDiskCache must be initialized with a name. Call initWithName: instead." userInfo:nil];
- return [self initWithName:@""];
- }
- - (instancetype)initWithName:(NSString *)name
- {
- return [self initWithName:name fileExtension:nil];
- }
- - (instancetype)initWithName:(NSString *)name fileExtension:(NSString *)fileExtension
- {
- return [self initWithName:name rootPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] fileExtension:fileExtension];
- }
- - (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath fileExtension:(NSString *)fileExtension
- {
- return [self initWithName:name rootPath:rootPath serializer:nil deserializer:nil fileExtension:fileExtension];
- }
- - (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath serializer:(PINDiskCacheSerializerBlock)serializer deserializer:(PINDiskCacheDeserializerBlock)deserializer fileExtension:(NSString *)fileExtension
- {
- return [self initWithName:name rootPath:rootPath serializer:serializer deserializer:deserializer fileExtension:fileExtension operationQueue:[PINOperationQueue sharedOperationQueue]];
- }
- - (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath serializer:(PINDiskCacheSerializerBlock)serializer deserializer:(PINDiskCacheDeserializerBlock)deserializer fileExtension:(NSString *)fileExtension operationQueue:(PINOperationQueue *)operationQueue
- {
- if (!name)
- return nil;
-
- if ((serializer && !deserializer) ||
- (!serializer && deserializer)){
- @throw [NSException exceptionWithName:@"Must initialize with a both serializer and deserializer" reason:@"PINDiskCache must be initialized with a serializer and deserializer." userInfo:nil];
- return nil;
- }
-
- if (self = [super init]) {
- _name = [name copy];
- _fileExtension = [fileExtension copy];
- _operationQueue = operationQueue;
- _instanceLock = [[NSConditionLock alloc] initWithCondition:PINDiskCacheConditionNotReady];
- _willAddObjectBlock = nil;
- _willRemoveObjectBlock = nil;
- _willRemoveAllObjectsBlock = nil;
- _didAddObjectBlock = nil;
- _didRemoveObjectBlock = nil;
- _didRemoveAllObjectsBlock = nil;
-
- _byteCount = 0;
- _byteLimit = 0;
- _ageLimit = 0.0;
-
- #if TARGET_OS_IPHONE
- _writingProtectionOption = NSDataWritingFileProtectionNone;
- #endif
-
- _dates = [[NSMutableDictionary alloc] init];
- _sizes = [[NSMutableDictionary alloc] init];
-
- NSString *pathComponent = [[NSString alloc] initWithFormat:@"%@.%@", PINDiskCachePrefix, _name];
- _cacheURL = [NSURL fileURLWithPathComponents:@[ rootPath, pathComponent ]];
-
- //setup serializers
- if(serializer) {
- _serializer = [serializer copy];
- } else {
- _serializer = self.defaultSerializer;
- }
- if(deserializer) {
- _deserializer = [deserializer copy];
- } else {
- _deserializer = self.defaultDeserializer;
- }
- //we don't want to do anything without setting up the disk cache, but we also don't want to block init, it can take a while to initialize. This must *not* be done on _operationQueue because other operations added may hold the lock and fill up the queue.
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- //should always be able to aquire the lock unless the below code is running.
- [_instanceLock lockWhenCondition:PINDiskCacheConditionNotReady];
- [self _locked_createCacheDirectory];
- [self _locked_initializeDiskProperties];
- [_instanceLock unlockWithCondition:PINDiskCacheConditionReady];
- });
- }
- return self;
- }
- - (NSString *)description
- {
- return [[NSString alloc] initWithFormat:@"%@.%@.%p", PINDiskCachePrefix, _name, (void *)self];
- }
- + (instancetype)sharedCache
- {
- static id cache;
- static dispatch_once_t predicate;
-
- dispatch_once(&predicate, ^{
- cache = [[self alloc] initWithName:PINDiskCacheSharedName];
- });
-
- return cache;
- }
- #pragma mark - Private Methods -
- - (NSURL *)encodedFileURLForKey:(NSString *)key
- {
- if (![key length])
- return nil;
-
- //Significantly improve performance by indicating that the URL will *not* result in a directory.
- //Also note that accessing _cacheURL is safe without the lock because it is only set on init.
- return [_cacheURL URLByAppendingPathComponent:[self encodedString:key] isDirectory:NO];
- }
- - (NSString *)keyForEncodedFileURL:(NSURL *)url
- {
- NSString *fileName = [url lastPathComponent];
- if (!fileName)
- return nil;
-
- return [self decodedString:fileName];
- }
- - (NSString *)encodedString:(NSString *)string
- {
- if (![string length]) {
- return @"";
- }
-
- if ([string respondsToSelector:@selector(stringByAddingPercentEncodingWithAllowedCharacters:)]) {
- NSString *encodedString = [string stringByAddingPercentEncodingWithAllowedCharacters:[[NSCharacterSet characterSetWithCharactersInString:@".:/%"] invertedSet]];
- if (self.fileExtension.length > 0) {
- return [encodedString stringByAppendingPathExtension:self.fileExtension];
- }
- else {
- return encodedString;
- }
- }
- else {
- CFStringRef static const charsToEscape = CFSTR(".:/%");
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- CFStringRef escapedString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
- (__bridge CFStringRef)string,
- NULL,
- charsToEscape,
- kCFStringEncodingUTF8);
- #pragma clang diagnostic pop
-
- if (self.fileExtension.length > 0) {
- return [(__bridge_transfer NSString *)escapedString stringByAppendingPathExtension:self.fileExtension];
- }
- else {
- return (__bridge_transfer NSString *)escapedString;
- }
- }
- }
- - (NSString *)decodedString:(NSString *)string
- {
- if (![string length]) {
- return @"";
- }
-
- if ([string respondsToSelector:@selector(stringByRemovingPercentEncoding)]) {
- return [string stringByRemovingPercentEncoding];
- }
- else {
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
- CFStringRef unescapedString = CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
- (__bridge CFStringRef)string,
- CFSTR(""),
- kCFStringEncodingUTF8);
- #pragma clang diagnostic pop
- return (__bridge_transfer NSString *)unescapedString;
- }
- }
- -(PINDiskCacheSerializerBlock) defaultSerializer
- {
- return ^NSData*(id<NSCoding> object, NSString *key){
- return [NSKeyedArchiver archivedDataWithRootObject:object];
- };
- }
- -(PINDiskCacheDeserializerBlock) defaultDeserializer
- {
- return ^id(NSData * data, NSString *key){
- return [NSKeyedUnarchiver unarchiveObjectWithData:data];
- };
- }
- #pragma mark - Private Trash Methods -
- + (dispatch_queue_t)sharedTrashQueue
- {
- static dispatch_queue_t trashQueue;
- static dispatch_once_t predicate;
-
- dispatch_once(&predicate, ^{
- NSString *queueName = [[NSString alloc] initWithFormat:@"%@.trash", PINDiskCachePrefix];
- trashQueue = dispatch_queue_create([queueName UTF8String], DISPATCH_QUEUE_SERIAL);
- dispatch_set_target_queue(trashQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0));
- });
-
- return trashQueue;
- }
- + (NSURL *)sharedTrashURL
- {
- static NSURL *sharedTrashURL;
- static dispatch_once_t predicate;
-
- dispatch_once(&predicate, ^{
- sharedTrashURL = [[[NSURL alloc] initFileURLWithPath:NSTemporaryDirectory()] URLByAppendingPathComponent:PINDiskCachePrefix isDirectory:YES];
-
- if (![[NSFileManager defaultManager] fileExistsAtPath:[sharedTrashURL path]]) {
- NSError *error = nil;
- [[NSFileManager defaultManager] createDirectoryAtURL:sharedTrashURL
- withIntermediateDirectories:YES
- attributes:nil
- error:&error];
- PINDiskCacheError(error);
- }
- });
-
- return sharedTrashURL;
- }
- + (BOOL)moveItemAtURLToTrash:(NSURL *)itemURL
- {
- if (![[NSFileManager defaultManager] fileExistsAtPath:[itemURL path]])
- return NO;
-
- NSError *error = nil;
- NSString *uniqueString = [[NSProcessInfo processInfo] globallyUniqueString];
- NSURL *uniqueTrashURL = [[PINDiskCache sharedTrashURL] URLByAppendingPathComponent:uniqueString isDirectory:NO];
- BOOL moved = [[NSFileManager defaultManager] moveItemAtURL:itemURL toURL:uniqueTrashURL error:&error];
- PINDiskCacheError(error);
- return moved;
- }
- + (void)emptyTrash
- {
- dispatch_async([self sharedTrashQueue], ^{
- NSError *searchTrashedItemsError = nil;
- NSArray *trashedItems = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:[self sharedTrashURL]
- includingPropertiesForKeys:nil
- options:0
- error:&searchTrashedItemsError];
- PINDiskCacheError(searchTrashedItemsError);
-
- for (NSURL *trashedItemURL in trashedItems) {
- NSError *removeTrashedItemError = nil;
- [[NSFileManager defaultManager] removeItemAtURL:trashedItemURL error:&removeTrashedItemError];
- PINDiskCacheError(removeTrashedItemError);
- }
- });
- }
- #pragma mark - Private Queue Methods -
- - (BOOL)_locked_createCacheDirectory
- {
- if ([[NSFileManager defaultManager] fileExistsAtPath:[_cacheURL path]])
- return NO;
-
- NSError *error = nil;
- BOOL success = [[NSFileManager defaultManager] createDirectoryAtURL:_cacheURL
- withIntermediateDirectories:YES
- attributes:nil
- error:&error];
- PINDiskCacheError(error);
-
- return success;
- }
- - (void)_locked_initializeDiskProperties
- {
- NSUInteger byteCount = 0;
- NSArray *keys = @[ NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey ];
-
- NSError *error = nil;
- NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:_cacheURL
- includingPropertiesForKeys:keys
- options:NSDirectoryEnumerationSkipsHiddenFiles
- error:&error];
- PINDiskCacheError(error);
-
- for (NSURL *fileURL in files) {
- NSString *key = [self keyForEncodedFileURL:fileURL];
-
- error = nil;
- NSDictionary *dictionary = [fileURL resourceValuesForKeys:keys error:&error];
- PINDiskCacheError(error);
-
- NSDate *date = [dictionary objectForKey:NSURLContentModificationDateKey];
- if (date && key)
- [_dates setObject:date forKey:key];
-
- NSNumber *fileSize = [dictionary objectForKey:NSURLTotalFileAllocatedSizeKey];
- if (fileSize) {
- [_sizes setObject:fileSize forKey:key];
- byteCount += [fileSize unsignedIntegerValue];
- }
- }
-
- if (byteCount > 0)
- self.byteCount = byteCount; // atomic
- }
- - (void)asynchronouslySetFileModificationDate:(NSDate *)date forURL:(NSURL *)fileURL
- {
- __weak PINDiskCache *weakSelf = self;
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (strongSelf) {
- [strongSelf lock];
- [strongSelf _locked_setFileModificationDate:date forURL:fileURL];
- [strongSelf unlock];
- }
- } withPriority:PINOperationQueuePriorityLow];
- }
- - (BOOL)_locked_setFileModificationDate:(NSDate *)date forURL:(NSURL *)fileURL
- {
- if (!date || !fileURL) {
- return NO;
- }
-
- NSError *error = nil;
- BOOL success = [[NSFileManager defaultManager] setAttributes:@{ NSFileModificationDate: date }
- ofItemAtPath:[fileURL path]
- error:&error];
- PINDiskCacheError(error);
-
- if (success) {
- NSString *key = [self keyForEncodedFileURL:fileURL];
- if (key) {
- [_dates setObject:date forKey:key];
- }
- }
-
- return success;
- }
- - (BOOL)removeFileAndExecuteBlocksForKey:(NSString *)key
- {
- NSURL *fileURL = [self encodedFileURLForKey:key];
-
- [self lock];
- if (!fileURL || ![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
- [self unlock];
- return NO;
- }
-
- PINDiskCacheObjectBlock willRemoveObjectBlock = _willRemoveObjectBlock;
- if (willRemoveObjectBlock) {
- [self unlock];
- willRemoveObjectBlock(self, key, nil);
- [self lock];
- }
-
- BOOL trashed = [PINDiskCache moveItemAtURLToTrash:fileURL];
- if (!trashed) {
- [self unlock];
- return NO;
- }
-
- [PINDiskCache emptyTrash];
-
- NSNumber *byteSize = [_sizes objectForKey:key];
- if (byteSize)
- self.byteCount = _byteCount - [byteSize unsignedIntegerValue]; // atomic
-
- [_sizes removeObjectForKey:key];
- [_dates removeObjectForKey:key];
-
- PINDiskCacheObjectBlock didRemoveObjectBlock = _didRemoveObjectBlock;
- if (didRemoveObjectBlock) {
- [self unlock];
- _didRemoveObjectBlock(self, key, nil);
- [self lock];
- }
-
- [self unlock];
-
- return YES;
- }
- - (void)trimDiskToSize:(NSUInteger)trimByteCount
- {
- [self lock];
- if (_byteCount > trimByteCount) {
- NSArray *keysSortedBySize = [_sizes keysSortedByValueUsingSelector:@selector(compare:)];
-
- for (NSString *key in [keysSortedBySize reverseObjectEnumerator]) { // largest objects first
- [self unlock];
-
- //unlock, removeFileAndExecuteBlocksForKey handles locking itself
- [self removeFileAndExecuteBlocksForKey:key];
-
- [self lock];
-
- if (_byteCount <= trimByteCount)
- break;
- }
- }
- [self unlock];
- }
- - (void)trimDiskToSizeByDate:(NSUInteger)trimByteCount
- {
- [self lock];
- if (_byteCount > trimByteCount) {
- NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)];
-
- for (NSString *key in keysSortedByDate) { // oldest objects first
- [self unlock];
-
- //unlock, removeFileAndExecuteBlocksForKey handles locking itself
- [self removeFileAndExecuteBlocksForKey:key];
-
- [self lock];
-
- if (_byteCount <= trimByteCount)
- break;
- }
- }
- [self unlock];
- }
- - (void)trimDiskToDate:(NSDate *)trimDate
- {
- [self lock];
- NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)];
-
- for (NSString *key in keysSortedByDate) { // oldest files first
- NSDate *accessDate = [_dates objectForKey:key];
- if (!accessDate)
- continue;
-
- if ([accessDate compare:trimDate] == NSOrderedAscending) { // older than trim date
- [self unlock];
-
- //unlock, removeFileAndExecuteBlocksForKey handles locking itself
- [self removeFileAndExecuteBlocksForKey:key];
-
- [self lock];
- } else {
- break;
- }
- }
- [self unlock];
- }
- - (void)trimToAgeLimitRecursively
- {
- [self lock];
- NSTimeInterval ageLimit = _ageLimit;
- [self unlock];
- if (ageLimit == 0.0)
- return;
-
- NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-ageLimit];
- [self trimDiskToDate:date];
-
- __weak PINDiskCache *weakSelf = self;
-
- dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_ageLimit * NSEC_PER_SEC));
- dispatch_after(time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
- PINDiskCache *strongSelf = weakSelf;
- [strongSelf.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- [strongSelf trimToAgeLimitRecursively];
- } withPriority:PINOperationQueuePriorityLow];
- });
- }
- #pragma mark - Public Asynchronous Methods -
- - (void)lockFileAccessWhileExecutingBlock:(void(^)(PINDiskCache *diskCache))block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (block) {
- [strongSelf lock];
- block(strongSelf);
- [strongSelf unlock];
- }
- } withPriority:PINOperationQueuePriorityLow];
- }
- - (void)containsObjectForKey:(NSString *)key block:(PINDiskCacheContainsBlock)block
- {
- if (!key || !block)
- return;
-
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- block([strongSelf containsObjectForKey:key]);
- } withPriority:PINOperationQueuePriorityLow];
- }
- - (void)objectForKey:(NSString *)key block:(PINDiskCacheObjectBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- NSURL *fileURL = nil;
- id <NSCoding> object = [strongSelf objectForKey:key fileURL:&fileURL];
-
- if (block) {
- block(strongSelf, key, object);
- }
- } withPriority:PINOperationQueuePriorityLow];
- }
- - (void)fileURLForKey:(NSString *)key block:(PINDiskCacheFileURLBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- NSURL *fileURL = [strongSelf fileURLForKey:key];
-
- if (block) {
- [strongSelf lock];
- block(key, fileURL);
- [strongSelf unlock];
- }
- } withPriority:PINOperationQueuePriorityLow];
- }
- - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key block:(PINDiskCacheObjectBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- NSURL *fileURL = nil;
- [strongSelf setObject:object forKey:key fileURL:&fileURL];
-
- if (block) {
- block(strongSelf, key, object);
- }
- } withPriority:PINOperationQueuePriorityLow];
- }
- - (void)removeObjectForKey:(NSString *)key block:(PINDiskCacheObjectBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- NSURL *fileURL = nil;
- [strongSelf removeObjectForKey:key fileURL:&fileURL];
-
- if (block) {
- block(strongSelf, key, nil);
- }
- } withPriority:PINOperationQueuePriorityLow];
- }
- - (void)trimToSize:(NSUInteger)trimByteCount block:(PINDiskCacheBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- [strongSelf trimToSize:trimByteCount];
-
- if (block) {
- block(strongSelf);
- }
- } withPriority:PINOperationQueuePriorityLow];
- }
- - (void)trimToDate:(NSDate *)trimDate block:(PINDiskCacheBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- [strongSelf trimToDate:trimDate];
-
- if (block) {
- block(strongSelf);
- }
- } withPriority:PINOperationQueuePriorityLow];
- }
- - (void)trimToSizeByDate:(NSUInteger)trimByteCount block:(PINDiskCacheBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- [strongSelf trimToSizeByDate:trimByteCount];
-
- if (block) {
- block(strongSelf);
- }
- } withPriority:PINOperationQueuePriorityLow];
- }
- - (void)removeAllObjects:(PINDiskCacheBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- [strongSelf removeAllObjects];
-
- if (block) {
- block(strongSelf);
- }
- } withPriority:PINOperationQueuePriorityLow];
- }
- - (void)enumerateObjectsWithBlock:(PINDiskCacheFileURLBlock)block completionBlock:(PINDiskCacheBlock)completionBlock
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- [strongSelf enumerateObjectsWithBlock:block];
-
- if (completionBlock) {
- completionBlock(strongSelf);
- }
- } withPriority:PINOperationQueuePriorityLow];
- }
- #pragma mark - Public Synchronous Methods -
- - (void)synchronouslyLockFileAccessWhileExecutingBlock:(void(^)(PINDiskCache *diskCache))block
- {
- if (block) {
- [self lock];
- block(self);
- [self unlock];
- }
- }
- - (BOOL)containsObjectForKey:(NSString *)key
- {
- return ([self fileURLForKey:key updateFileModificationDate:NO] != nil);
- }
- - (__nullable id<NSCoding>)objectForKey:(NSString *)key
- {
- return [self objectForKey:key fileURL:nil];
- }
- - (id)objectForKeyedSubscript:(NSString *)key
- {
- return [self objectForKey:key];
- }
- - (__nullable id <NSCoding>)objectForKey:(NSString *)key fileURL:(NSURL **)outFileURL
- {
- NSDate *now = [[NSDate alloc] init];
-
- if (!key)
- return nil;
-
- id <NSCoding> object = nil;
- NSURL *fileURL = [self encodedFileURLForKey:key];
-
- [self lock];
- if (!self->_ttlCache || self->_ageLimit <= 0 || fabs([[_dates objectForKey:key] timeIntervalSinceDate:now]) < self->_ageLimit) {
- // If the cache should behave like a TTL cache, then only fetch the object if there's a valid ageLimit and the object is still alive
- NSData *objectData = [[NSData alloc] initWithContentsOfFile:[fileURL path]];
-
- if (objectData) {
- //Be careful with locking below. We unlock here so that we're not locked while deserializing, we re-lock after.
- [self unlock];
- @try {
- object = _deserializer(objectData, key);
- }
- @catch (NSException *exception) {
- NSError *error = nil;
- [self lock];
- [[NSFileManager defaultManager] removeItemAtPath:[fileURL path] error:&error];
- [self unlock];
- PINDiskCacheError(error);
- }
- [self lock];
- }
- if (object && !self->_ttlCache) {
- [self asynchronouslySetFileModificationDate:now forURL:fileURL];
- }
- }
- [self unlock];
-
- if (outFileURL) {
- *outFileURL = fileURL;
- }
-
- return object;
- }
- /// Helper function to call fileURLForKey:updateFileModificationDate:
- - (NSURL *)fileURLForKey:(NSString *)key
- {
- // Don't update the file modification time, if self is a ttlCache
- return [self fileURLForKey:key updateFileModificationDate:!self->_ttlCache];
- }
- - (NSURL *)fileURLForKey:(NSString *)key updateFileModificationDate:(BOOL)updateFileModificationDate
- {
- if (!key) {
- return nil;
- }
-
- NSDate *now = [[NSDate alloc] init];
- NSURL *fileURL = [self encodedFileURLForKey:key];
-
- [self lock];
- if ([[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
- if (updateFileModificationDate) {
- [self asynchronouslySetFileModificationDate:now forURL:fileURL];
- }
- } else {
- fileURL = nil;
- }
- [self unlock];
- return fileURL;
- }
- - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key
- {
- [self setObject:object forKey:key fileURL:nil];
- }
- - (void)setObject:(id)object forKeyedSubscript:(NSString *)key
- {
- [self setObject:object forKey:key];
- }
- - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key fileURL:(NSURL **)outFileURL
- {
- if (!key || !object)
- return;
-
- #if TARGET_OS_IPHONE
- NSDataWritingOptions writeOptions = NSDataWritingAtomic | self.writingProtectionOption;
- #else
- NSDataWritingOptions writeOptions = NSDataWritingAtomic;
- #endif
-
- NSURL *fileURL = [self encodedFileURLForKey:key];
-
- [self lock];
- PINDiskCacheObjectBlock willAddObjectBlock = self->_willAddObjectBlock;
- if (willAddObjectBlock) {
- [self unlock];
- willAddObjectBlock(self, key, object);
- [self lock];
- }
-
- //We unlock here so that we're not locked while serializing.
- [self unlock];
- NSData *data = _serializer(object, key);
- [self lock];
-
- NSError *writeError = nil;
-
- BOOL written = [data writeToURL:fileURL options:writeOptions error:&writeError];
- PINDiskCacheError(writeError);
-
- if (written) {
- NSError *error = nil;
- NSDictionary *values = [fileURL resourceValuesForKeys:@[ NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey ] error:&error];
- PINDiskCacheError(error);
-
- NSNumber *diskFileSize = [values objectForKey:NSURLTotalFileAllocatedSizeKey];
- if (diskFileSize) {
- NSNumber *prevDiskFileSize = [self->_sizes objectForKey:key];
- if (prevDiskFileSize) {
- self.byteCount = self->_byteCount - [prevDiskFileSize unsignedIntegerValue];
- }
- [self->_sizes setObject:diskFileSize forKey:key];
- self.byteCount = self->_byteCount + [diskFileSize unsignedIntegerValue]; // atomic
- }
- NSDate *date = [values objectForKey:NSURLContentModificationDateKey];
- if (date) {
- [self->_dates setObject:date forKey:key];
- }
-
- if (self->_byteLimit > 0 && self->_byteCount > self->_byteLimit)
- [self trimToSizeByDate:self->_byteLimit block:nil];
- } else {
- fileURL = nil;
- }
-
- PINDiskCacheObjectBlock didAddObjectBlock = self->_didAddObjectBlock;
- if (didAddObjectBlock) {
- [self unlock];
- didAddObjectBlock(self, key, object);
- [self lock];
- }
- [self unlock];
-
- if (outFileURL) {
- *outFileURL = fileURL;
- }
- }
- - (void)removeObjectForKey:(NSString *)key
- {
- [self removeObjectForKey:key fileURL:nil];
- }
- - (void)removeObjectForKey:(NSString *)key fileURL:(NSURL **)outFileURL
- {
- if (!key)
- return;
-
- NSURL *fileURL = nil;
-
- fileURL = [self encodedFileURLForKey:key];
-
- [self removeFileAndExecuteBlocksForKey:key];
-
- if (outFileURL) {
- *outFileURL = fileURL;
- }
- }
- - (void)trimToSize:(NSUInteger)trimByteCount
- {
- if (trimByteCount == 0) {
- [self removeAllObjects];
- return;
- }
-
- [self trimDiskToSize:trimByteCount];
- }
- - (void)trimToDate:(NSDate *)trimDate
- {
- if (!trimDate)
- return;
-
- if ([trimDate isEqualToDate:[NSDate distantPast]]) {
- [self removeAllObjects];
- return;
- }
-
- [self trimDiskToDate:trimDate];
- }
- - (void)trimToSizeByDate:(NSUInteger)trimByteCount
- {
- if (trimByteCount == 0) {
- [self removeAllObjects];
- return;
- }
-
- [self trimDiskToSizeByDate:trimByteCount];
- }
- - (void)removeAllObjects
- {
- [self lock];
- PINDiskCacheBlock willRemoveAllObjectsBlock = self->_willRemoveAllObjectsBlock;
- if (willRemoveAllObjectsBlock) {
- [self unlock];
- willRemoveAllObjectsBlock(self);
- [self lock];
- }
-
- [PINDiskCache moveItemAtURLToTrash:self->_cacheURL];
- [PINDiskCache emptyTrash];
-
- [self _locked_createCacheDirectory];
-
- [self->_dates removeAllObjects];
- [self->_sizes removeAllObjects];
- self.byteCount = 0; // atomic
-
- PINDiskCacheBlock didRemoveAllObjectsBlock = self->_didRemoveAllObjectsBlock;
- if (didRemoveAllObjectsBlock) {
- [self unlock];
- didRemoveAllObjectsBlock(self);
- [self lock];
- }
-
- [self unlock];
- }
- - (void)enumerateObjectsWithBlock:(PINDiskCacheFileURLBlock)block
- {
- if (!block)
- return;
-
- [self lock];
- NSDate *now = [NSDate date];
- NSArray *keysSortedByDate = [self->_dates keysSortedByValueUsingSelector:@selector(compare:)];
-
- for (NSString *key in keysSortedByDate) {
- NSURL *fileURL = [self encodedFileURLForKey:key];
- // If the cache should behave like a TTL cache, then only fetch the object if there's a valid ageLimit and the object is still alive
- if (!self->_ttlCache || self->_ageLimit <= 0 || fabs([[_dates objectForKey:key] timeIntervalSinceDate:now]) < self->_ageLimit) {
- block(key, fileURL);
- }
- }
- [self unlock];
- }
- #pragma mark - Public Thread Safe Accessors -
- - (PINDiskCacheObjectBlock)willAddObjectBlock
- {
- PINDiskCacheObjectBlock block = nil;
-
- [self lock];
- block = _willAddObjectBlock;
- [self unlock];
-
- return block;
- }
- - (void)setWillAddObjectBlock:(PINDiskCacheObjectBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (!strongSelf)
- return;
- [strongSelf lock];
- strongSelf->_willAddObjectBlock = [block copy];
- [strongSelf unlock];
- } withPriority:PINOperationQueuePriorityHigh];
- }
- - (PINDiskCacheObjectBlock)willRemoveObjectBlock
- {
- PINDiskCacheObjectBlock block = nil;
-
- [self lock];
- block = _willRemoveObjectBlock;
- [self unlock];
-
- return block;
- }
- - (void)setWillRemoveObjectBlock:(PINDiskCacheObjectBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (!strongSelf)
- return;
-
- [strongSelf lock];
- strongSelf->_willRemoveObjectBlock = [block copy];
- [strongSelf unlock];
- } withPriority:PINOperationQueuePriorityHigh];
- }
- - (PINDiskCacheBlock)willRemoveAllObjectsBlock
- {
- PINDiskCacheBlock block = nil;
-
- [self lock];
- block = _willRemoveAllObjectsBlock;
- [self unlock];
-
- return block;
- }
- - (void)setWillRemoveAllObjectsBlock:(PINDiskCacheBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (!strongSelf)
- return;
-
- [strongSelf lock];
- strongSelf->_willRemoveAllObjectsBlock = [block copy];
- [strongSelf unlock];
- } withPriority:PINOperationQueuePriorityHigh];
- }
- - (PINDiskCacheObjectBlock)didAddObjectBlock
- {
- PINDiskCacheObjectBlock block = nil;
-
- [self lock];
- block = _didAddObjectBlock;
- [self unlock];
-
- return block;
- }
- - (void)setDidAddObjectBlock:(PINDiskCacheObjectBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (!strongSelf)
- return;
-
- [strongSelf lock];
- strongSelf->_didAddObjectBlock = [block copy];
- [strongSelf unlock];
- } withPriority:PINOperationQueuePriorityHigh];
- }
- - (PINDiskCacheObjectBlock)didRemoveObjectBlock
- {
- PINDiskCacheObjectBlock block = nil;
-
- [self lock];
- block = _didRemoveObjectBlock;
- [self unlock];
-
- return block;
- }
- - (void)setDidRemoveObjectBlock:(PINDiskCacheObjectBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (!strongSelf)
- return;
-
- [strongSelf lock];
- strongSelf->_didRemoveObjectBlock = [block copy];
- [strongSelf unlock];
- } withPriority:PINOperationQueuePriorityHigh];
- }
- - (PINDiskCacheBlock)didRemoveAllObjectsBlock
- {
- PINDiskCacheBlock block = nil;
-
- [self lock];
- block = _didRemoveAllObjectsBlock;
- [self unlock];
-
- return block;
- }
- - (void)setDidRemoveAllObjectsBlock:(PINDiskCacheBlock)block
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (!strongSelf)
- return;
-
- [strongSelf lock];
- strongSelf->_didRemoveAllObjectsBlock = [block copy];
- [strongSelf unlock];
- } withPriority:PINOperationQueuePriorityHigh];
- }
- - (NSUInteger)byteLimit
- {
- NSUInteger byteLimit;
-
- [self lock];
- byteLimit = _byteLimit;
- [self unlock];
-
- return byteLimit;
- }
- - (void)setByteLimit:(NSUInteger)byteLimit
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (!strongSelf)
- return;
-
- [strongSelf lock];
- strongSelf->_byteLimit = byteLimit;
- [strongSelf unlock];
-
- if (byteLimit > 0)
- [strongSelf trimDiskToSizeByDate:byteLimit];
- } withPriority:PINOperationQueuePriorityHigh];
- }
- - (NSTimeInterval)ageLimit
- {
- NSTimeInterval ageLimit;
-
- [self lock];
- ageLimit = _ageLimit;
- [self unlock];
-
- return ageLimit;
- }
- - (void)setAgeLimit:(NSTimeInterval)ageLimit
- {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (!strongSelf)
- return;
-
- [strongSelf lock];
- strongSelf->_ageLimit = ageLimit;
- [strongSelf unlock];
-
- [strongSelf trimToAgeLimitRecursively];
- } withPriority:PINOperationQueuePriorityHigh];
- }
- - (BOOL)isTTLCache {
- BOOL isTTLCache;
-
- [self lock];
- isTTLCache = _ttlCache;
- [self unlock];
-
- return isTTLCache;
- }
- - (void)setTtlCache:(BOOL)ttlCache {
- __weak PINDiskCache *weakSelf = self;
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (!strongSelf)
- return;
- [strongSelf lock];
- strongSelf->_ttlCache = ttlCache;
- [strongSelf unlock];
- } withPriority:PINOperationQueuePriorityHigh];
- }
- #if TARGET_OS_IPHONE
- - (NSDataWritingOptions)writingProtectionOption {
- NSDataWritingOptions option;
-
- [self lock];
- option = _writingProtectionOption;
- [self unlock];
-
- return option;
- }
- - (void)setWritingProtectionOption:(NSDataWritingOptions)writingProtectionOption {
- __weak PINDiskCache *weakSelf = self;
-
- [self.operationQueue addOperation:^{
- PINDiskCache *strongSelf = weakSelf;
- if (!strongSelf)
- return;
-
- NSDataWritingOptions option = NSDataWritingFileProtectionMask & writingProtectionOption;
-
- [strongSelf lock];
- strongSelf->_writingProtectionOption = option;
- [strongSelf unlock];
- } withPriority:PINOperationQueuePriorityHigh];
- }
- #endif
- - (void)lock
- {
- [_instanceLock lockWhenCondition:PINDiskCacheConditionReady];
- }
- - (void)unlock
- {
- [_instanceLock unlockWithCondition:PINDiskCacheConditionReady];
- }
- @end
|