Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PINDiskCache] Respect small byteLimit settings by checking object size in setObject: #198

Merged
merged 4 commits into from
Sep 21, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Add your own contributions to the next release on the line below this with your name.

## 3.0.1 -- Beta 5
- [fix] Respect small byteLimit settings by checking object size in setObject: [#198](https://github.com/pinterest/PINCache/pull/198)
- [new] Added an ability to set custom encoder/decoder for file names: [#192](https://github.com/pinterest/PINCache/pull/192)

## 2.2.1 -- 2016 Mar 5
Expand Down
27 changes: 18 additions & 9 deletions Source/PINDiskCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ - (id)objectForKeyedSubscript:(NSString *)key
{
NSDate *now = [[NSDate alloc] init];

if (!key)
if (!key || (_dates.count == 0 && _sizes.count == 0))
Copy link
Collaborator

@garrettmoon garrettmoon Sep 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need a lock here both because this is not thread-safe (at least not formally) and because the disk cache may not have been initialized yet which the lockWhenCondition ensures.

return nil;

id <NSCoding> object = nil;
Expand Down Expand Up @@ -936,8 +936,23 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key fileURL:(NSURL **
NSDataWritingOptions writeOptions = NSDataWritingAtomic;
#endif

NSURL *fileURL = [self encodedFileURLForKey:key];

// Remain unlocked here so that we're not locked while serializing.
NSData *data = _serializer(object, key);
NSURL *fileURL = nil;

NSUInteger byteLimit = self.byteLimit;
if (data.length <= byteLimit || byteLimit == 0) {
// The cache is large enough to fit this object (although we may need to evict others).
fileURL = [self encodedFileURLForKey:key];
} else {
// The cache isn't large enough to fit this object (even if all others were evicted).
// We should not write it to disk because it will be deleted immediately after.
if (outFileURL) {
*outFileURL = nil;
}
return;
}

[self lock];
PINCacheObjectBlock willAddObjectBlock = self->_willAddObjectBlock;
if (willAddObjectBlock) {
Expand All @@ -946,13 +961,7 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key fileURL:(NSURL **
[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);

Expand Down
2 changes: 1 addition & 1 deletion Tests/PINCacheTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ - (void)testAsyncDiskInitialization

testCache = [[PINDiskCache alloc] initWithName:cacheName];
//This should not return until *after* disk cache directory has been created
[testCache objectForKey:@"some bogus key"];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually shouldn't be changed, it failed because the optimization above, without the lock, isn't valid. I think it would cause problems for systems requesting objects early in their lifecycle which might be critical to pull from the cache.

[testCache setObject:[NSNumber numberWithInt:1] forKey:@"some bogus key"];
XCTAssertTrue([[NSFileManager defaultManager] fileExistsAtPath:[testCacheURL path]]);
}

Expand Down