Merge pull request #2569 from dreampiggy/rename_cache_memory_disk
Rename cache memory disk
This commit is contained in:
commit
557b6985a8
|
@ -15,7 +15,7 @@
|
|||
// All of these method are called from the same global queue to avoid blocking on main queue and thread-safe problem. But it's also recommend to ensure thread-safe yourself using lock or other ways.
|
||||
@required
|
||||
/**
|
||||
Create a new disk cache based on the specified path.
|
||||
Create a new disk cache based on the specified path. You can check `maxDiskSize` and `maxDiskAge` used for disk cache.
|
||||
|
||||
@param cachePath Full path of a directory in which the cache will write data.
|
||||
Once initialized you should not read and write to this directory.
|
||||
|
|
|
@ -135,7 +135,7 @@
|
|||
options:NSDirectoryEnumerationSkipsHiddenFiles
|
||||
errorHandler:NULL];
|
||||
|
||||
NSDate *expirationDate = (self.config.maxCacheAge < 0) ? nil: [NSDate dateWithTimeIntervalSinceNow:-self.config.maxCacheAge];
|
||||
NSDate *expirationDate = (self.config.maxDiskAge < 0) ? nil: [NSDate dateWithTimeIntervalSinceNow:-self.config.maxDiskAge];
|
||||
NSMutableDictionary<NSURL *, NSDictionary<NSString *, id> *> *cacheFiles = [NSMutableDictionary dictionary];
|
||||
NSUInteger currentCacheSize = 0;
|
||||
|
||||
|
@ -172,10 +172,10 @@
|
|||
|
||||
// If our remaining disk cache exceeds a configured maximum size, perform a second
|
||||
// size-based cleanup pass. We delete the oldest files first.
|
||||
NSUInteger maxCacheSize = self.config.maxCacheSize;
|
||||
if (maxCacheSize > 0 && currentCacheSize > maxCacheSize) {
|
||||
NSUInteger maxDiskSize = self.config.maxDiskSize;
|
||||
if (maxDiskSize > 0 && currentCacheSize > maxDiskSize) {
|
||||
// Target half of our maximum cache size for this cleanup pass.
|
||||
const NSUInteger desiredCacheSize = maxCacheSize / 2;
|
||||
const NSUInteger desiredCacheSize = maxDiskSize / 2;
|
||||
|
||||
// Sort the remaining cache files by their last modification time or last access time (oldest first).
|
||||
NSArray<NSURL *> *sortedFiles = [cacheFiles keysSortedByValueWithOptions:NSSortConcurrent
|
||||
|
|
|
@ -321,14 +321,14 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
|
|||
#pragma mark - Cache Info
|
||||
|
||||
/**
|
||||
* Get the size used by the disk cache
|
||||
* Get the total bytes size of images in the disk cache
|
||||
*/
|
||||
- (NSUInteger)getSize;
|
||||
- (NSUInteger)totalDiskSize;
|
||||
|
||||
/**
|
||||
* Get the number of images in the disk cache
|
||||
*/
|
||||
- (NSUInteger)getDiskCount;
|
||||
- (NSUInteger)totalDiskCount;
|
||||
|
||||
/**
|
||||
* Asynchronously calculate the disk cache's size.
|
||||
|
|
|
@ -564,7 +564,7 @@
|
|||
|
||||
#pragma mark - Cache Info
|
||||
|
||||
- (NSUInteger)getSize {
|
||||
- (NSUInteger)totalDiskSize {
|
||||
__block NSUInteger size = 0;
|
||||
dispatch_sync(self.ioQueue, ^{
|
||||
size = [self.diskCache totalSize];
|
||||
|
@ -572,7 +572,7 @@
|
|||
return size;
|
||||
}
|
||||
|
||||
- (NSUInteger)getDiskCount {
|
||||
- (NSUInteger)totalDiskCount {
|
||||
__block NSUInteger count = 0;
|
||||
dispatch_sync(self.ioQueue, ^{
|
||||
count = [self.diskCache totalCount];
|
||||
|
|
|
@ -73,13 +73,13 @@ typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) {
|
|||
* Setting this to zero means that all cached files would be removed when do expiration check.
|
||||
* Defaults to 1 weak.
|
||||
*/
|
||||
@property (assign, nonatomic) NSTimeInterval maxCacheAge;
|
||||
@property (assign, nonatomic) NSTimeInterval maxDiskAge;
|
||||
|
||||
/**
|
||||
* The maximum size of the disk cache, in bytes.
|
||||
* Defaults to 0. Which means there is no cache size limit.
|
||||
*/
|
||||
@property (assign, nonatomic) NSUInteger maxCacheSize;
|
||||
@property (assign, nonatomic) NSUInteger maxDiskSize;
|
||||
|
||||
/**
|
||||
* The maximum "total cost" of the in-memory image cache. The cost function is the bytes size held in memory.
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
#import "SDDiskCache.h"
|
||||
|
||||
static SDImageCacheConfig *_defaultCacheConfig;
|
||||
static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
|
||||
static const NSInteger kDefaultCacheMaxDiskAge = 60 * 60 * 24 * 7; // 1 week
|
||||
|
||||
@implementation SDImageCacheConfig
|
||||
|
||||
|
@ -31,8 +31,8 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
|
|||
_shouldRemoveExpiredDataWhenEnterBackground = YES;
|
||||
_diskCacheReadingOptions = 0;
|
||||
_diskCacheWritingOptions = NSDataWritingAtomic;
|
||||
_maxCacheAge = kDefaultCacheMaxCacheAge;
|
||||
_maxCacheSize = 0;
|
||||
_maxDiskAge = kDefaultCacheMaxDiskAge;
|
||||
_maxDiskSize = 0;
|
||||
_diskCacheExpireType = SDImageCacheConfigExpireTypeModificationDate;
|
||||
_memoryCacheClass = [SDMemoryCache class];
|
||||
_diskCacheClass = [SDDiskCache class];
|
||||
|
@ -48,8 +48,8 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
|
|||
config.shouldRemoveExpiredDataWhenEnterBackground = self.shouldRemoveExpiredDataWhenEnterBackground;
|
||||
config.diskCacheReadingOptions = self.diskCacheReadingOptions;
|
||||
config.diskCacheWritingOptions = self.diskCacheWritingOptions;
|
||||
config.maxCacheAge = self.maxCacheAge;
|
||||
config.maxCacheSize = self.maxCacheSize;
|
||||
config.maxDiskAge = self.maxDiskAge;
|
||||
config.maxDiskSize = self.maxDiskSize;
|
||||
config.maxMemoryCost = self.maxMemoryCost;
|
||||
config.maxMemoryCount = self.maxMemoryCount;
|
||||
config.diskCacheExpireType = self.diskCacheExpireType;
|
||||
|
|
|
@ -195,13 +195,13 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
|
|||
}
|
||||
|
||||
- (void)test20InitialCacheSize{
|
||||
expect([[SDImageCache sharedImageCache] getSize]).to.equal(0);
|
||||
expect([[SDImageCache sharedImageCache] totalDiskSize]).to.equal(0);
|
||||
}
|
||||
|
||||
- (void)test21InitialDiskCount{
|
||||
XCTestExpectation *expectation = [self expectationWithDescription:@"getDiskCount"];
|
||||
[[SDImageCache sharedImageCache] storeImage:[self testJPEGImage] forKey:kTestImageKeyJPEG completion:^{
|
||||
expect([[SDImageCache sharedImageCache] getDiskCount]).to.equal(1);
|
||||
expect([[SDImageCache sharedImageCache] totalDiskCount]).to.equal(1);
|
||||
[[SDImageCache sharedImageCache] removeImageForKey:kTestImageKeyJPEG withCompletion:^{
|
||||
[expectation fulfill];
|
||||
}];
|
||||
|
|
|
@ -77,7 +77,7 @@
|
|||
}
|
||||
|
||||
- (void)removeExpiredData {
|
||||
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxCacheAge];
|
||||
NSDate *expirationDate = [NSDate dateWithTimeIntervalSinceNow:-self.config.maxDiskAge];
|
||||
for (NSString *fileName in [self.fileManager enumeratorAtPath:self.cachePath]) {
|
||||
NSString *filePath = [self.cachePath stringByAppendingPathComponent:fileName];
|
||||
NSDate *modificationDate = [[self.fileManager attributesOfItemAtPath:filePath error:nil] objectForKey:NSFileModificationDate];
|
||||
|
|
Loading…
Reference in New Issue