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

Fix PINDiskCache byteCount tracking #30

Merged
merged 1 commit into from
Sep 23, 2015
Merged
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
4 changes: 4 additions & 0 deletions PINCache/PINDiskCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,10 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key fileURL:(NSURL **

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
}
Expand Down
13 changes: 13 additions & 0 deletions tests/PINCacheTests/PINCacheTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ - (void)testDiskByteCount
XCTAssertTrue(self.cache.diskByteCount > 0, @"disk cache byte count was not greater than zero");
}

- (void)testDiskByteCountWithExistingKey
{
[self.cache setObject:[self image] forKey:@"image"];
NSUInteger initialDiskByteCount = self.cache.diskByteCount;
[self.cache setObject:[self image] forKey:@"image"];

XCTAssertTrue(self.cache.diskByteCount == initialDiskByteCount, @"disk cache byte count should not change by adding object with existing key and size");

[self.cache setObject:[self image] forKey:@"image2"];

XCTAssertTrue(self.cache.diskByteCount > initialDiskByteCount, @"disk cache byte count should increase with new key and object added to disk cache");
}

- (void)testOneThousandAndOneWrites
{
NSUInteger max = 1001;
Expand Down