Skip to content

Commit

Permalink
Upgrade pin cache fix warnings (#506)
Browse files Browse the repository at this point in the history
* Modernize project and fix some warnings

* Upgrade PINCache, fix warnings and re-enable warning check

* Update carthage make command

* Remove duplicate dependencies
  • Loading branch information
garrettmoon authored Jun 11, 2019
1 parent 69891fd commit ef10415
Show file tree
Hide file tree
Showing 36 changed files with 215 additions and 154 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [new] Add PINRemoteImageManagerConfiguration configuration object [#492](https://github.com/pinterest/PINRemoteImage/pull/492) [rqueue](https://github.com/rqueue)

## 3.0.0 Beta 14
- [fixed] Re-enable warnings check [#506](https://github.com/pinterest/PINRemoteImage/pull/506) [garrettmoon](https://github.com/garrettmoon)
- [new] Allow use of NSURLCache via a custom NSURLSession [#477](https://github.com/pinterest/PINRemoteImage/pull/477) [wiseoldduck](https://github.com/wiseoldduck)
- [new] Respect Cache-Control and Expires headers if the cache supports TTL. [#462](https://github.com/pinterest/PINRemoteImage/pull/462) [wiseoldduck](https://github.com/wiseoldduck)
- [new] Updated to latest PINCache beta 7. [#461](https://github.com/pinterest/PINRemoteImage/pull/461) [wiseoldduck](https://github.com/wiseoldduck)
Expand Down
3 changes: 1 addition & 2 deletions Cartfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
git "https://chromium.googlesource.com/webm/libwebp" "v0.6.0"
github "pinterest/PINCache" "3.0.1-beta.7"
github "pinterest/PINCache" "3.0.1-beta.8"
github "pinterest/PINOperation"
5 changes: 2 additions & 3 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
git "https://chromium.googlesource.com/webm/libwebp" "v0.6.0"
github "pinterest/PINCache" "3.0.1-beta.7"
github "pinterest/PINOperation" "1.1.1"
github "pinterest/PINCache" "3.0.1-beta.8"
github "pinterest/PINOperation" "1.1.2"
5 changes: 5 additions & 0 deletions Carthage/Checkouts/PINCache/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

* Add your own contributions to the next release on the line below this with your name.

## 3.0.1 -- Beta 8
- [fix] Initing PINCache with TTL enabled should enable TTL on PINMemoryCache. [#246](https://github.com/pinterest/PINCache/pull/246)
- [performance] Return TTL cache objects without waiting for all metadata to be read. [#228](https://github.com/pinterest/PINCache/pull/228)
- [performance] Memory cache now performs some tasks such as trimming and removing experied objects with low priority. [#234](https://github.com/pinterest/PINCache/pull/234)

## 3.0.1 -- Beta 7
- [fix] Fix up warnings and upgrade to PINOperation 1.1.1: [#213](https://github.com/pinterest/PINCache/pull/213)
- [performance] Reduce locking churn in cleanup methods. [#212](https://github.com/pinterest/PINCache/pull/212)
Expand Down
4 changes: 2 additions & 2 deletions Carthage/Checkouts/PINCache/PINCache.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PINCache'
s.version = '3.0.1-beta.7'
s.version = '3.0.1-beta.8'
s.homepage = 'https://github.com/pinterest/PINCache'
s.summary = 'Fast, thread safe, parallel object cache for iOS and OS X.'
s.authors = { 'Garrett Moon' => 'garrett@pinterest.com', 'Justin Ouellette' => 'jstn@tumblr.com' }
Expand All @@ -11,7 +11,7 @@ Pod::Spec.new do |s|
s.ios.weak_frameworks = 'UIKit'
s.osx.weak_frameworks = 'AppKit'
s.ios.deployment_target = '8.0'
s.osx.deployment_target = '10.8'
s.osx.deployment_target = '10.11'
s.tvos.deployment_target = '9.0'
s.watchos.deployment_target = '2.0'
pch_PIN = <<-EOS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
English,
);
mainGroup = CC0105A71E271A1600890935;
productRefGroup = CC0105B21E271A1600890935 /* Products */;
Expand Down Expand Up @@ -760,6 +760,7 @@
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
Expand Down Expand Up @@ -819,6 +820,7 @@
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion Carthage/Checkouts/PINCache/Source/PINCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ - (instancetype)initWithName:(NSString *)name
keyDecoder:keyDecoder
operationQueue:_operationQueue
ttlCache:ttlCache];
_memoryCache = [[PINMemoryCache alloc] initWithOperationQueue:_operationQueue];
_memoryCache = [[PINMemoryCache alloc] initWithName:_name operationQueue:_operationQueue ttlCache:ttlCache];
}
return self;
}
Expand Down
8 changes: 4 additions & 4 deletions Carthage/Checkouts/PINCache/Source/PINDiskCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ PIN_SUBCLASSING_RESTRICTED
- (instancetype)init NS_UNAVAILABLE;

/**
Multiple instances with the same name are allowed and can safely access
the same data on disk thanks to the magic of seriality.
Multiple instances with the same name are *not* allowed as they would conflict
with each other.
@see name
@param name The name of the cache.
Expand All @@ -259,8 +259,8 @@ PIN_SUBCLASSING_RESTRICTED
- (instancetype)initWithName:(nonnull NSString *)name;

/**
Multiple instances with the same name are allowed and can safely access
the same data on disk thanks to the magic of seriality.
Multiple instances with the same name are *not* allowed as they would conflict
with each other.
@see name
@param name The name of the cache.
Expand Down
137 changes: 87 additions & 50 deletions Carthage/Checkouts/PINCache/Source/PINDiskCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ typedef NS_ENUM(NSUInteger, PINDiskCacheCondition) {
}

@interface PINDiskCacheMetadata : NSObject
// When the object was added to the disk cache
@property (nonatomic, strong) NSDate *createdDate;
// Last time the object was accessed
@property (nonatomic, strong) NSDate *lastModifiedDate;
@property (nonatomic, strong) NSNumber *size;
// Age limit is used in conjuction with ttl
@property (nonatomic) NSTimeInterval ageLimit;
@end

Expand Down Expand Up @@ -267,7 +270,7 @@ - (instancetype)initWithName:(NSString *)name

- (NSString *)description
{
return [[NSString alloc] initWithFormat:@"%@.%@.%p", PINDiskCachePrefix, _name, (void *)self];
return [[NSString alloc] initWithFormat:@"%@.%@.%p", PINDiskCachePrefix, _name, (__bridge void *)self];
}

+ (PINDiskCache *)sharedCache
Expand Down Expand Up @@ -322,7 +325,14 @@ - (NSString *)decodedString:(NSString *)string
- (PINDiskCacheSerializerBlock)defaultSerializer
{
return ^NSData*(id<NSCoding> object, NSString *key){
return [NSKeyedArchiver archivedDataWithRootObject:object];
if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) {
NSError *error = nil;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object requiringSecureCoding:NO error:&error];
PINDiskCacheError(error);
return data;
} else {
return [NSKeyedArchiver archivedDataWithRootObject:object];
}
};
}

Expand Down Expand Up @@ -489,64 +499,85 @@ - (BOOL)_locked_createCacheDirectory
return created;
}

+ (NSArray *)resourceKeys
{
static NSArray *resourceKeys = nil;
static dispatch_once_t predicate;

dispatch_once(&predicate, ^{
resourceKeys = @[ NSURLCreationDateKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey ];
});

return resourceKeys;
}

/**
* @return File size in bytes.
*/
- (NSUInteger)_locked_initializeDiskPropertiesForFile:(NSURL *)fileURL fileKey:(NSString *)fileKey
{
NSError *error = nil;

NSDictionary *dictionary = [fileURL resourceValuesForKeys:[PINDiskCache resourceKeys] error:&error];
PINDiskCacheError(error);

if (_metadata[fileKey] == nil) {
_metadata[fileKey] = [[PINDiskCacheMetadata alloc] init];
}

NSDate *createdDate = dictionary[NSURLCreationDateKey];
if (createdDate && fileKey)
_metadata[fileKey].createdDate = createdDate;

NSDate *lastModifiedDate = dictionary[NSURLContentModificationDateKey];
if (lastModifiedDate && fileKey)
_metadata[fileKey].lastModifiedDate = lastModifiedDate;

NSNumber *fileSize = dictionary[NSURLTotalFileAllocatedSizeKey];
if (fileSize) {
_metadata[fileKey].size = fileSize;
}

if (_ttlCache) {
NSTimeInterval ageLimit;
ssize_t res = getxattr(PINDiskCacheFileSystemRepresentation(fileURL), PINDiskCacheAgeLimitAttributeName, &ageLimit, sizeof(NSTimeInterval), 0, 0);
if(res > 0) {
_metadata[fileKey].ageLimit = ageLimit;
} else if (res == -1) {
// Ignore if the extended attribute was never recorded for this file.
if (errno != ENOATTR) {
NSDictionary<NSErrorUserInfoKey, id> *userInfo = @{ PINDiskCacheErrorReadFailureCodeKey : @(errno)};
error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorReadFailure userInfo:userInfo];
PINDiskCacheError(error);
}
}
}

return [fileSize unsignedIntegerValue];
}

- (void)initializeDiskProperties
{
NSUInteger byteCount = 0;
NSArray *keys = @[ NSURLCreationDateKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey ];


NSError *error = nil;

[self lock];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:_cacheURL
includingPropertiesForKeys:keys
includingPropertiesForKeys:[PINDiskCache resourceKeys]
options:NSDirectoryEnumerationSkipsHiddenFiles
error:&error];
[self unlock];

PINDiskCacheError(error);

for (NSURL *fileURL in files) {
NSString *key = [self keyForEncodedFileURL:fileURL];

error = nil;

NSString *fileKey = [self keyForEncodedFileURL:fileURL];
// Continually grab and release lock while processing files to avoid contention
[self lock];
NSDictionary *dictionary = [fileURL resourceValuesForKeys:keys error:&error];
PINDiskCacheError(error);

if (_metadata[key] == nil) {
_metadata[key] = [[PINDiskCacheMetadata alloc] init];
}

NSDate *createdDate = [dictionary objectForKey:NSURLCreationDateKey];
if (createdDate && key)
_metadata[key].createdDate = createdDate;

NSDate *lastModifiedDate = [dictionary objectForKey:NSURLContentModificationDateKey];
if (lastModifiedDate && key)
_metadata[key].lastModifiedDate = lastModifiedDate;

NSNumber *fileSize = [dictionary objectForKey:NSURLTotalFileAllocatedSizeKey];
if (fileSize) {
_metadata[key].size = fileSize;
byteCount += [fileSize unsignedIntegerValue];
}

if (_ttlCache) {
NSTimeInterval ageLimit;
ssize_t res = getxattr(PINDiskCacheFileSystemRepresentation(fileURL), PINDiskCacheAgeLimitAttributeName, &ageLimit, sizeof(NSTimeInterval), 0, 0);
if(res) {
_metadata[key].ageLimit = ageLimit;
} else if (res == -1) {
// Ignore if the extended attribute was never recorded for this file.
if (errno != ENOATTR) {
NSDictionary<NSErrorUserInfoKey, id> *userInfo = @{ PINDiskCacheErrorReadFailureCodeKey : @(errno)};
error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorReadFailure userInfo:userInfo];
PINDiskCacheError(error);
}
}
}
if (_metadata[fileKey] == nil) {
byteCount += [self _locked_initializeDiskPropertiesForFile:fileURL fileKey:fileKey];
}
[self unlock];
}

Expand Down Expand Up @@ -707,6 +738,7 @@ - (void)trimDiskToSize:(NSUInteger)trimByteCount
}
}

// This is the default trimming method which happens automatically
- (void)trimDiskToSizeByDate:(NSUInteger)trimByteCount
{
if (self.isTTLCache) {
Expand All @@ -719,12 +751,14 @@ - (void)trimDiskToSizeByDate:(NSUInteger)trimByteCount
if (_byteCount > trimByteCount) {
keysToRemove = [[NSMutableArray alloc] init];

// last modified represents last access.
NSArray *keysSortedByLastModifiedDate = [_metadata keysSortedByValueUsingComparator:^NSComparisonResult(PINDiskCacheMetadata * _Nonnull obj1, PINDiskCacheMetadata * _Nonnull obj2) {
return [obj1.lastModifiedDate compare:obj2.lastModifiedDate];
}];

NSUInteger bytesSaved = 0;
for (NSString *key in keysSortedByLastModifiedDate) { // oldest objects first
// objects accessed last first.
for (NSString *key in keysSortedByLastModifiedDate) {
[keysToRemove addObject:key];
NSNumber *byteSize = _metadata[key].size;
if (byteSize) {
Expand Down Expand Up @@ -1036,9 +1070,12 @@ - (id)objectForKeyedSubscript:(NSString *)key
NSDate *now = [NSDate date];
[self lock];
if (self->_ttlCache) {
// We actually need to know the entire disk state if we're a TTL cache.
[self unlock];
[self lockAndWaitForKnownState];
if (!_diskStateKnown) {
if (_metadata[key] == nil) {
NSString *fileKey = [self keyForEncodedFileURL:fileURL];
[self _locked_initializeDiskPropertiesForFile:fileURL fileKey:fileKey];
}
}
}

NSTimeInterval ageLimit = _metadata[key].ageLimit > 0.0 ? _metadata[key].ageLimit : self->_ageLimit;
Expand Down Expand Up @@ -1559,7 +1596,7 @@ - (void)lockForWriting
{
[self lock];

// spinlock if the disk isn't writable
// Lock if the disk isn't writable.
if (_diskWritable == NO) {
pthread_cond_wait(&_diskWritableCondition, &_mutex);
}
Expand All @@ -1569,7 +1606,7 @@ - (void)lockAndWaitForKnownState
{
[self lock];

// spinlock if the disk state isn't known
// Lock if the disk state isn't known.
if (_diskStateKnown == NO) {
pthread_cond_wait(&_diskStateKnownCondition, &_mutex);
}
Expand Down
Loading

0 comments on commit ef10415

Please sign in to comment.