TumblrArchive/TMCache

If setObject:forKey:block is called for a key that already exists (i.e. update object for a key), the byte count is not changed to reflect the update.

SudeepSidhu opened this issue · 1 comments

In the 1.2.0 release:

- (void)setObject:(id <NSCoding>)object forKey:(NSString *)key block:(TMDiskCacheObjectBlock)block
NSNumber *diskFileSize = [values objectForKey:NSURLTotalFileAllocatedSizeKey];
if (diskFileSize) {
[strongSelf->_sizes setObject:diskFileSize forKey:key];
                strongSelf.byteCount = strongSelf->_byteCount + [diskFileSize unsignedIntegerValue]; // atomic
}

Should be changed to something like:

NSNumber *diskFileSize = [values objectForKey:NSURLTotalFileAllocatedSizeKey];

            if (diskFileSize) {

                NSNumber *oldEntry = [strongSelf->_sizes objectForKey:key];

                if (oldEntry && [oldEntry isKindOfClass:NSNumber.class]){
                    strongSelf.byteCount = strongSelf->_byteCount - [oldEntry unsignedIntegerValue];
                }

                [strongSelf->_sizes setObject:diskFileSize forKey:key];
                strongSelf.byteCount = strongSelf->_byteCount + [diskFileSize unsignedIntegerValue]; // atomic
            }

Nice catch!