Add no expiration file support of disk cache

This commit is contained in:
zhongwuzw 2018-08-24 15:54:04 +08:00
parent 6246c0ea0b
commit 9afbc8bf7e
2 changed files with 5 additions and 4 deletions

View File

@ -135,7 +135,7 @@
options:NSDirectoryEnumerationSkipsHiddenFiles options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:NULL]; errorHandler:NULL];
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxCacheAge]; NSDate *expirationDate = (self.config.maxCacheAge < 0) ? nil: [NSDate dateWithTimeIntervalSinceNow:-self.config.maxCacheAge];
NSMutableDictionary<NSURL *, NSDictionary<NSString *, id> *> *cacheFiles = [NSMutableDictionary dictionary]; NSMutableDictionary<NSURL *, NSDictionary<NSString *, id> *> *cacheFiles = [NSMutableDictionary dictionary];
NSUInteger currentCacheSize = 0; NSUInteger currentCacheSize = 0;
@ -155,7 +155,7 @@
// Remove files that are older than the expiration date; // Remove files that are older than the expiration date;
NSDate *modifiedDate = resourceValues[cacheContentDateKey]; NSDate *modifiedDate = resourceValues[cacheContentDateKey];
if ([[modifiedDate laterDate:expirationDate] isEqualToDate:expirationDate]) { if (expirationDate && [[modifiedDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
[urlsToDelete addObject:fileURL]; [urlsToDelete addObject:fileURL];
continue; continue;
} }

View File

@ -68,13 +68,14 @@ typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) {
@property (assign, nonatomic) NSDataWritingOptions diskCacheWritingOptions; @property (assign, nonatomic) NSDataWritingOptions diskCacheWritingOptions;
/** /**
* The maximum length of time to keep an image in the cache, in seconds. * The maximum length of time to keep an image in the disk cache, in seconds.
* Setting this to a negative value means no expiring.
* Defaults to 1 weak. * Defaults to 1 weak.
*/ */
@property (assign, nonatomic) NSTimeInterval maxCacheAge; @property (assign, nonatomic) NSTimeInterval maxCacheAge;
/** /**
* The maximum size of the cache, in bytes. * The maximum size of the disk cache, in bytes.
* Defaults to 0. Which means there is no cache size limit. * Defaults to 0. Which means there is no cache size limit.
*/ */
@property (assign, nonatomic) NSUInteger maxCacheSize; @property (assign, nonatomic) NSUInteger maxCacheSize;