Merge pull request #353 from jparise/max-cache-size

Support a maximum disk cache size in -cleanDisk.
This commit is contained in:
Olivier Poitrey 2013-04-26 14:22:52 -07:00
commit 116258502b
2 changed files with 68 additions and 17 deletions

View File

@ -37,6 +37,11 @@ typedef enum SDImageCacheType SDImageCacheType;
*/ */
@property (assign, nonatomic) NSInteger maxCacheAge; @property (assign, nonatomic) NSInteger maxCacheAge;
/**
* The maximum size of the cache, in bytes.
*/
@property (assign, nonatomic) unsigned long long maxCacheSize;
/** /**
* Returns global shared cache instance * Returns global shared cache instance
* *

View File

@ -257,30 +257,76 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
{ {
dispatch_async(self.ioQueue, ^ dispatch_async(self.ioQueue, ^
{ {
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.maxCacheAge]; NSFileManager *fileManager = [NSFileManager defaultManager];
// convert NSString path to NSURL path
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES]; NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
// build an enumerator by also prefetching file properties we want to read NSArray *resourceKeys = @[ NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey ];
NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtURL:diskCacheURL
includingPropertiesForKeys:@[ NSURLIsDirectoryKey, NSURLContentModificationDateKey ] // This enumerator prefetches useful properties for our cache files.
options:NSDirectoryEnumerationSkipsHiddenFiles NSDirectoryEnumerator *fileEnumerator = [fileManager enumeratorAtURL:diskCacheURL
errorHandler:NULL]; includingPropertiesForKeys:resourceKeys
options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:NULL];
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.maxCacheAge];
NSMutableDictionary *cacheFiles = [NSMutableDictionary dictionary];
unsigned long long currentCacheSize = 0;
// Enumerate all of the files in the cache directory. This loop has two purposes:
//
// 1. Removing files that are older than the expiration date.
// 2. Storing file attributes for the size-based cleanup pass.
for (NSURL *fileURL in fileEnumerator) for (NSURL *fileURL in fileEnumerator)
{ {
// skip folder NSDictionary *resourceValues = [fileURL resourceValuesForKeys:resourceKeys error:NULL];
NSNumber *isDirectory;
[fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:NULL]; // Skip directories.
if ([isDirectory boolValue]) if ([resourceValues[NSURLIsDirectoryKey] boolValue])
{ {
continue; continue;
} }
// compare file date with the max age // Remove files that are older than the expiration date;
NSDate *fileModificationDate; NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey];
[fileURL getResourceValue:&fileModificationDate forKey:NSURLContentModificationDateKey error:NULL]; if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate])
if ([[fileModificationDate laterDate:expirationDate] isEqualToDate:expirationDate])
{ {
[[NSFileManager defaultManager] removeItemAtURL:fileURL error:nil]; [fileManager removeItemAtURL:fileURL error:nil];
continue;
}
// Store a reference to this file and account for its total size.
NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
currentCacheSize += [totalAllocatedSize unsignedLongLongValue];
[cacheFiles setObject:resourceValues forKey:fileURL];
}
// If our remaining disk cache exceeds a configured maximum size, perform a second
// size-based cleanup pass. We delete the oldest files first.
if (self.maxCacheSize > 0 && currentCacheSize > self.maxCacheSize)
{
// Target half of our maximum cache size for this cleanup pass.
const unsigned long long desiredCacheSize = self.maxCacheSize / 2;
// Sort the remaining cache files by their last modification time (oldest first).
NSArray *sortedFiles = [cacheFiles keysSortedByValueWithOptions:NSSortConcurrent
usingComparator:^NSComparisonResult(id obj1, id obj2)
{
return [obj1[NSURLContentModificationDateKey] compare:obj2[NSURLContentModificationDateKey]];
}];
// Delete files until we fall below our desired cache size.
for (NSURL *fileURL in sortedFiles)
{
if ([fileManager removeItemAtURL:fileURL error:nil])
{
NSDictionary *resourceValues = cacheFiles[fileURL];
NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
currentCacheSize -= [totalAllocatedSize unsignedLongLongValue];
if (currentCacheSize < desiredCacheSize)
{
break;
}
}
} }
} }
}); });