Skip to content

Commit

Permalink
Merge pull request #30 from rewcraig/master
Browse files Browse the repository at this point in the history
Fix PINDiskCache byteCount tracking
  • Loading branch information
garrettmoon committed Sep 23, 2015
2 parents 3d03a98 + 4371dcd commit 33c83f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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

0 comments on commit 33c83f5

Please sign in to comment.