Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Add [TMCache](http://cocoapods.org/?q=name%3ATMCache) to your `Podfile` and run

## Requirements ##

__TMCache__ requires iOS 5.0 or OS X 10.7 and greater.
__TMCache__ requires iOS 7.0 or OS X 10.7 and greater.

## Contact ##

Expand Down
2 changes: 1 addition & 1 deletion TMCache.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Pod::Spec.new do |s|
s.frameworks = 'Foundation'
s.ios.weak_frameworks = 'UIKit'
s.osx.weak_frameworks = 'AppKit'
s.ios.deployment_target = '5.0'
s.ios.deployment_target = '7.0'
s.osx.deployment_target = '10.7'
end
2 changes: 1 addition & 1 deletion TMCache/TMCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ typedef void (^TMCacheObjectBlock)(TMCache *cache, NSString *key, id object);
@param rootPath The path of the cache on disk.
@result A new cache with the specified name.
*/
- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath;
- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath NS_DESIGNATED_INITIALIZER;

#pragma mark -
/// @name Asynchronous Methods
Expand Down
55 changes: 4 additions & 51 deletions TMCache/TMCache.m
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
#import "TMCache.h"

NSString * const TMCachePrefix = @"com.tumblr.TMCache";
NSString * const TMCacheSharedName = @"TMCacheShared";
static NSString * const TMCachePrefix = @"com.tumblr.TMCache";
static NSString * const TMCacheSharedName = @"TMCacheShared";

@interface TMCache ()
#if OS_OBJECT_USE_OBJC
@property (strong, nonatomic) dispatch_queue_t queue;
#else
@property (assign, nonatomic) dispatch_queue_t queue;
#endif
@end

@implementation TMCache

#pragma mark - Initialization -

#if !OS_OBJECT_USE_OBJC
- (void)dealloc
{
dispatch_release(_queue);
_queue = nil;
}
#endif

- (instancetype)initWithName:(NSString *)name
{
return [self initWithName:name rootPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
return [self initWithName:name
rootPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]];
}

- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath
Expand Down Expand Up @@ -151,10 +140,6 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key block:(TMCacheObj
if (strongSelf)
block(strongSelf, key, object);
});

#if !OS_OBJECT_USE_OBJC
dispatch_release(group);
#endif
}
}

Expand Down Expand Up @@ -191,10 +176,6 @@ - (void)removeObjectForKey:(NSString *)key block:(TMCacheObjectBlock)block
if (strongSelf)
block(strongSelf, key, nil);
});

#if !OS_OBJECT_USE_OBJC
dispatch_release(group);
#endif
}
}

Expand Down Expand Up @@ -228,10 +209,6 @@ - (void)removeAllObjects:(TMCacheBlock)block
if (strongSelf)
block(strongSelf);
});

#if !OS_OBJECT_USE_OBJC
dispatch_release(group);
#endif
}
}

Expand Down Expand Up @@ -268,10 +245,6 @@ - (void)trimToDate:(NSDate *)date block:(TMCacheBlock)block
if (strongSelf)
block(strongSelf);
});

#if !OS_OBJECT_USE_OBJC
dispatch_release(group);
#endif
}
}

Expand Down Expand Up @@ -306,10 +279,6 @@ - (id)objectForKey:(NSString *)key

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif

return objectForKey;
}

Expand All @@ -325,10 +294,6 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif
}

- (void)removeObjectForKey:(NSString *)key
Expand All @@ -343,10 +308,6 @@ - (void)removeObjectForKey:(NSString *)key
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif
}

- (void)trimToDate:(NSDate *)date
Expand All @@ -361,10 +322,6 @@ - (void)trimToDate:(NSDate *)date
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif
}

- (void)removeAllObjects
Expand All @@ -376,10 +333,6 @@ - (void)removeAllObjects
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif
}

@end
Expand Down
2 changes: 1 addition & 1 deletion TMCache/TMDiskCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ typedef void (^TMDiskCacheObjectBlock)(TMDiskCache *cache, NSString *key, id <NS
@param rootPath The path of the cache.
@result A new cache with the specified name.
*/
- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath;
- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath NS_DESIGNATED_INITIALIZER;

#pragma mark -
/// @name Asynchronous Methods
Expand Down
90 changes: 28 additions & 62 deletions TMCache/TMDiskCache.m
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
#import "TMDiskCache.h"

#define TMDiskCacheError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \
[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
__LINE__, [error localizedDescription]); }

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0
#define TMCacheStartBackgroundTask() UIBackgroundTaskIdentifier taskID = UIBackgroundTaskInvalid; \
taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ \
[[UIApplication sharedApplication] endBackgroundTask:taskID]; }];
#define TMCacheEndBackgroundTask() [[UIApplication sharedApplication] endBackgroundTask:taskID];
#else
#define TMCacheStartBackgroundTask()
#define TMCacheEndBackgroundTask()
#endif

NSString * const TMDiskCachePrefix = @"com.tumblr.TMDiskCache";
NSString * const TMDiskCacheSharedName = @"TMDiskCacheShared";
#define TMDiskCacheError(error) \
if (error) { \
NSLog(@"%@ (%d) ERROR: %@", \
[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
__LINE__, [error localizedDescription]); \
}

#define TMCacheStartBackgroundTask() \
UIBackgroundTaskIdentifier taskID = UIBackgroundTaskInvalid; \
taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ \
[[UIApplication sharedApplication] endBackgroundTask:taskID]; \
}];
#define TMCacheEndBackgroundTask() \
[[UIApplication sharedApplication] endBackgroundTask:taskID];

static NSString * const TMDiskCachePrefix = @"com.tumblr.TMDiskCache";
static NSString * const TMDiskCacheSharedName = @"TMDiskCacheShared";

@interface TMDiskCache ()
@property (assign) NSUInteger byteCount;
Expand All @@ -40,7 +41,8 @@ @implementation TMDiskCache

- (instancetype)initWithName:(NSString *)name
{
return [self initWithName:name rootPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
return [self initWithName:name
rootPath:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]];
}

- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath
Expand Down Expand Up @@ -264,13 +266,13 @@ - (void)initializeDiskProperties
NSDictionary *dictionary = [fileURL resourceValuesForKeys:keys error:&error];
TMDiskCacheError(error);

NSDate *date = [dictionary objectForKey:NSURLContentModificationDateKey];
NSDate *date = dictionary[NSURLContentModificationDateKey];
if (date && key)
[_dates setObject:date forKey:key];
_dates[key] = date;

NSNumber *fileSize = [dictionary objectForKey:NSURLTotalFileAllocatedSizeKey];
NSNumber *fileSize = dictionary[NSURLTotalFileAllocatedSizeKey];
if (fileSize) {
[_sizes setObject:fileSize forKey:key];
_sizes[key] = fileSize;
byteCount += [fileSize unsignedIntegerValue];
}
}
Expand All @@ -294,7 +296,7 @@ - (BOOL)setFileModificationDate:(NSDate *)date forURL:(NSURL *)fileURL
if (success) {
NSString *key = [self keyForEncodedFileURL:fileURL];
if (key) {
[_dates setObject:date forKey:key];
_dates[key] = date;
}
}

Expand All @@ -316,7 +318,7 @@ - (BOOL)removeFileAndExecuteBlocksForKey:(NSString *)key

[TMDiskCache emptyTrash];

NSNumber *byteSize = [_sizes objectForKey:key];
NSNumber *byteSize = _sizes[key];
if (byteSize)
self.byteCount = _byteCount - [byteSize unsignedIntegerValue]; // atomic

Expand Down Expand Up @@ -364,7 +366,7 @@ - (void)trimDiskToDate:(NSDate *)trimDate
NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)];

for (NSString *key in keysSortedByDate) { // oldest files first
NSDate *accessDate = [_dates objectForKey:key];
NSDate *accessDate = _dates[key];
if (!accessDate)
continue;

Expand Down Expand Up @@ -479,9 +481,9 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key block:(TMDiskCach
NSDictionary *values = [fileURL resourceValuesForKeys:@[ NSURLTotalFileAllocatedSizeKey ] error:&error];
TMDiskCacheError(error);

NSNumber *diskFileSize = [values objectForKey:NSURLTotalFileAllocatedSizeKey];
NSNumber *diskFileSize = values[NSURLTotalFileAllocatedSizeKey];
if (diskFileSize) {
[strongSelf->_sizes setObject:diskFileSize forKey:key];
strongSelf->_sizes[key] = diskFileSize;
strongSelf.byteCount = strongSelf->_byteCount + [diskFileSize unsignedIntegerValue]; // atomic
}

Expand Down Expand Up @@ -694,10 +696,6 @@ - (void)enumerateObjectsWithBlock:(TMDiskCacheObjectBlock)block completionBlock:

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif

return objectForKey;
}

Expand All @@ -717,10 +715,6 @@ - (NSURL *)fileURLForKey:(NSString *)key

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif

return fileURLForKey;
}

Expand All @@ -736,10 +730,6 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif
}

- (void)removeObjectForKey:(NSString *)key
Expand All @@ -754,10 +744,6 @@ - (void)removeObjectForKey:(NSString *)key
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif
}

- (void)trimToSize:(NSUInteger)byteCount
Expand All @@ -769,10 +755,6 @@ - (void)trimToSize:(NSUInteger)byteCount
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif
}

- (void)trimToDate:(NSDate *)date
Expand All @@ -792,10 +774,6 @@ - (void)trimToDate:(NSDate *)date
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif
}

- (void)trimToSizeByDate:(NSUInteger)byteCount
Expand All @@ -807,10 +785,6 @@ - (void)trimToSizeByDate:(NSUInteger)byteCount
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif
}

- (void)removeAllObjects
Expand All @@ -822,10 +796,6 @@ - (void)removeAllObjects
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif
}

- (void)enumerateObjectsWithBlock:(TMDiskCacheObjectBlock)block
Expand All @@ -840,10 +810,6 @@ - (void)enumerateObjectsWithBlock:(TMDiskCacheObjectBlock)block
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#if !OS_OBJECT_USE_OBJC
dispatch_release(semaphore);
#endif
}

#pragma mark - Public Thread Safe Accessors -
Expand Down
Loading