Repository: SDWebImage/SDWebImage
File: Tests/Tests/SDWebImageTestCache.m (Objective-C)

Code before:

- (void)removeDataForKey:(nonnull NSString *)key {
    [self.fileManager removeItemAtPath:[self cachePathForKey:key] error:nil];
}

- (void)removeExpiredData {
    NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxDiskAge];
    NSURL *diskCacheURL = [NSURL fileURLWithPath:self.cachePath isDirectory:YES];
    NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLAttributeContentAccessDateKey];
    NSDirectoryEnumerator<NSURL *> *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
                                                   includingPropertiesForKeys:resourceKeys
                                                                      options:NSDirectoryEnumerationSkipsHiddenFiles
                                                                 errorHandler:NULL];
    
    for (NSURL *fileURL in fileEnumerator) {
        @autoreleasepool {
            NSError *error;
            NSDictionary<NSString *, id> *resourceValues = [fileURL resourceValuesForKeys:resourceKeys error:&error];
            
            // Skip directories and errors.
            if (error || !resourceValues || [resourceValues[NSURLIsDirectoryKey] boolValue]) {
                continue;;
            }
            
            // Remove files that are older than the expiration date;
            NSDate *accessDate = resourceValues[NSURLAttributeContentAccessDateKey];
            if (expirationDate && [[accessDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
                [self.fileManager removeItemAtURL:fileURL error:nil];
            }
        }
    }
}

- (void)setData:(nullable NSData *)data forKey:(nonnull NSString *)key {
    [self.fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
}

- (NSUInteger)totalCount {
    NSUInteger count = 0;
    @autoreleasepool {
        count = [self.fileManager contentsOfDirectoryAtPath:self.cachePath error:nil].count;
    }
    return count;
}

- (NSUInteger)totalSize {
    NSUInteger size = 0;
    @autoreleasepool {
        NSURL *pathURL = [NSURL fileURLWithPath:self.cachePath isDirectory:YES];
        NSDirectoryEnumerator<NSURL *> *fileEnumerator = [self.fileManager enumeratorAtURL:pathURL
                                                  includingPropertiesForKeys:@[NSURLFileSizeKey]

Review comment (suggestion):
```suggestion
            NSDate *accessDate = resourceValues[NSURLContentAccessDateKey];
```

Code after:

- (void)removeDataForKey:(nonnull NSString *)key {
    [self.fileManager removeItemAtPath:[self cachePathForKey:key] error:nil];
}

- (void)removeExpiredData {
    NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxDiskAge];
    NSURL *diskCacheURL = [NSURL fileURLWithPath:self.cachePath isDirectory:YES];
    NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentAccessDateKey];
    NSDirectoryEnumerator<NSURL *> *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
                                                   includingPropertiesForKeys:resourceKeys
                                                                      options:NSDirectoryEnumerationSkipsHiddenFiles
                                                                 errorHandler:NULL];
    
    for (NSURL *fileURL in fileEnumerator) {
        @autoreleasepool {
            NSError *error;
            NSDictionary<NSString *, id> *resourceValues = [fileURL resourceValuesForKeys:resourceKeys error:&error];
            
            // Skip directories and errors.
            if (error || !resourceValues || [resourceValues[NSURLIsDirectoryKey] boolValue]) {
                continue;;
            }
            
            // Remove files that are older than the expiration date;
            NSDate *accessDate = resourceValues[NSURLContentAccessDateKey];
            if (expirationDate && [[accessDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
                [self.fileManager removeItemAtURL:fileURL error:nil];
            }
        }
    }
}

- (void)setData:(nullable NSData *)data forKey:(nonnull NSString *)key {
    [self.fileManager createFileAtPath:[self cachePathForKey:key] contents:data attributes:nil];
}

- (NSUInteger)totalCount {
    NSUInteger count = 0;
    @autoreleasepool {
        count = [self.fileManager contentsOfDirectoryAtPath:self.cachePath error:nil].count;
    }
    return count;
}

- (NSUInteger)totalSize {
    NSUInteger size = 0;
    @autoreleasepool {
        NSURL *pathURL = [NSURL fileURLWithPath:self.cachePath isDirectory:YES];
        NSDirectoryEnumerator<NSURL *> *fileEnumerator = [self.fileManager enumeratorAtURL:pathURL
                                                  includingPropertiesForKeys:@[NSURLFileSizeKey]