|
@@ -20,6 +20,7 @@ static NSUInteger diskCapacity = 40 * 1024 * 1024;
|
|
|
|
|
|
static NSTimeInterval cacheTime = 7 * 24 * 60 * 60;
|
|
static NSTimeInterval cacheTime = 7 * 24 * 60 * 60;
|
|
|
|
|
|
|
|
+#define CACHE_PATH ([NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject])
|
|
@implementation KSCacheManager
|
|
@implementation KSCacheManager
|
|
|
|
|
|
+ (KSCacheManager *)shareManager {
|
|
+ (KSCacheManager *)shareManager {
|
|
@@ -64,10 +65,11 @@ static NSTimeInterval cacheTime = 7 * 24 * 60 * 60;
|
|
NSString *directoryPath = nil;
|
|
NSString *directoryPath = nil;
|
|
directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:cacheDirKey];
|
|
directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:cacheDirKey];
|
|
if (!directoryPath) {
|
|
if (!directoryPath) {
|
|
- directoryPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"YQNetworking"] stringByAppendingPathComponent:@"networkCache"];
|
|
|
|
|
|
+ directoryPath = @"KSNetworking/networkCache";
|
|
[[NSUserDefaults standardUserDefaults] setObject:directoryPath forKey:cacheDirKey];
|
|
[[NSUserDefaults standardUserDefaults] setObject:directoryPath forKey:cacheDirKey];
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
}
|
|
}
|
|
|
|
+ directoryPath = [CACHE_PATH stringByAppendingPathComponent:directoryPath];
|
|
[KSDiskCache writeData:data toDir:directoryPath filename:hash];
|
|
[KSDiskCache writeData:data toDir:directoryPath filename:hash];
|
|
|
|
|
|
[[KSLRUManager shareManager] addFileNode:hash];
|
|
[[KSLRUManager shareManager] addFileNode:hash];
|
|
@@ -90,7 +92,7 @@ static NSTimeInterval cacheTime = 7 * 24 * 60 * 60;
|
|
|
|
|
|
if (!cacheData) {
|
|
if (!cacheData) {
|
|
NSString *directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:cacheDirKey];
|
|
NSString *directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:cacheDirKey];
|
|
-
|
|
|
|
|
|
+ directoryPath = [CACHE_PATH stringByAppendingPathComponent:directoryPath];
|
|
if (directoryPath) {
|
|
if (directoryPath) {
|
|
cacheData = [KSDiskCache readDataFromDir:directoryPath filename:hash];
|
|
cacheData = [KSDiskCache readDataFromDir:directoryPath filename:hash];
|
|
|
|
|
|
@@ -125,13 +127,12 @@ static NSTimeInterval cacheTime = 7 * 24 * 60 * 60;
|
|
NSString *directoryPath = nil;
|
|
NSString *directoryPath = nil;
|
|
directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:downloadDirKey];
|
|
directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:downloadDirKey];
|
|
if (!directoryPath) {
|
|
if (!directoryPath) {
|
|
- directoryPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"YQNetworking"] stringByAppendingPathComponent:@"download"];
|
|
|
|
-
|
|
|
|
|
|
+ directoryPath = @"KSNetworking/download";
|
|
[[NSUserDefaults standardUserDefaults] setObject:directoryPath forKey:downloadDirKey];
|
|
[[NSUserDefaults standardUserDefaults] setObject:directoryPath forKey:downloadDirKey];
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+ directoryPath = [CACHE_PATH stringByAppendingPathComponent:directoryPath];
|
|
[KSDiskCache writeData:data toDir:directoryPath filename:fileName];
|
|
[KSDiskCache writeData:data toDir:directoryPath filename:fileName];
|
|
|
|
|
|
}
|
|
}
|
|
@@ -159,7 +160,7 @@ static NSTimeInterval cacheTime = 7 * 24 * 60 * 60;
|
|
|
|
|
|
|
|
|
|
NSString *directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:downloadDirKey];
|
|
NSString *directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:downloadDirKey];
|
|
-
|
|
|
|
|
|
+ directoryPath = [CACHE_PATH stringByAppendingPathComponent:directoryPath];
|
|
if (directoryPath) data = [KSDiskCache readDataFromDir:directoryPath filename:fileName];
|
|
if (directoryPath) data = [KSDiskCache readDataFromDir:directoryPath filename:fileName];
|
|
|
|
|
|
if (data) {
|
|
if (data) {
|
|
@@ -171,36 +172,38 @@ static NSTimeInterval cacheTime = 7 * 24 * 60 * 60;
|
|
}
|
|
}
|
|
|
|
|
|
- (NSUInteger)totalCacheSize {
|
|
- (NSUInteger)totalCacheSize {
|
|
- NSString *diretoryPath = [[NSUserDefaults standardUserDefaults] objectForKey: cacheDirKey];
|
|
|
|
-
|
|
|
|
|
|
+ NSString *diretoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:cacheDirKey];
|
|
|
|
+ diretoryPath = [CACHE_PATH stringByAppendingPathComponent:diretoryPath];
|
|
return [KSDiskCache dataSizeInDir:diretoryPath];
|
|
return [KSDiskCache dataSizeInDir:diretoryPath];
|
|
}
|
|
}
|
|
|
|
|
|
- (NSUInteger)totalDownloadDataSize {
|
|
- (NSUInteger)totalDownloadDataSize {
|
|
- NSString *diretoryPath = [[NSUserDefaults standardUserDefaults] objectForKey: downloadDirKey];
|
|
|
|
-
|
|
|
|
|
|
+ NSString *diretoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:downloadDirKey];
|
|
|
|
+ diretoryPath = [CACHE_PATH stringByAppendingPathComponent:diretoryPath];
|
|
return [KSDiskCache dataSizeInDir:diretoryPath];
|
|
return [KSDiskCache dataSizeInDir:diretoryPath];
|
|
}
|
|
}
|
|
|
|
|
|
- (void)clearDownloadData {
|
|
- (void)clearDownloadData {
|
|
NSString *diretoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:downloadDirKey];
|
|
NSString *diretoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:downloadDirKey];
|
|
-
|
|
|
|
|
|
+ diretoryPath = [CACHE_PATH stringByAppendingPathComponent:diretoryPath];
|
|
[KSDiskCache clearDataIinDir:diretoryPath];
|
|
[KSDiskCache clearDataIinDir:diretoryPath];
|
|
}
|
|
}
|
|
|
|
|
|
- (NSString *)getDownDirectoryPath {
|
|
- (NSString *)getDownDirectoryPath {
|
|
NSString *diretoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:downloadDirKey];
|
|
NSString *diretoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:downloadDirKey];
|
|
|
|
+ diretoryPath = [CACHE_PATH stringByAppendingPathComponent:diretoryPath];
|
|
return diretoryPath;
|
|
return diretoryPath;
|
|
}
|
|
}
|
|
|
|
|
|
- (NSString *)getCacheDiretoryPath {
|
|
- (NSString *)getCacheDiretoryPath {
|
|
NSString *diretoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:cacheDirKey];
|
|
NSString *diretoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:cacheDirKey];
|
|
|
|
+ diretoryPath = [CACHE_PATH stringByAppendingPathComponent:diretoryPath];
|
|
return diretoryPath;
|
|
return diretoryPath;
|
|
}
|
|
}
|
|
|
|
|
|
- (void)clearTotalCache {
|
|
- (void)clearTotalCache {
|
|
NSString *directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:cacheDirKey];
|
|
NSString *directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:cacheDirKey];
|
|
-
|
|
|
|
|
|
+ directoryPath = [CACHE_PATH stringByAppendingPathComponent:directoryPath];
|
|
[KSDiskCache clearDataIinDir:directoryPath];
|
|
[KSDiskCache clearDataIinDir:directoryPath];
|
|
}
|
|
}
|
|
|
|
|
|
@@ -208,6 +211,7 @@ static NSTimeInterval cacheTime = 7 * 24 * 60 * 60;
|
|
if ([self totalCacheSize] > diskCapacity) {
|
|
if ([self totalCacheSize] > diskCapacity) {
|
|
NSArray *deleteFiles = [[KSLRUManager shareManager] removeLRUFileNodeWithCacheTime:cacheTime];
|
|
NSArray *deleteFiles = [[KSLRUManager shareManager] removeLRUFileNodeWithCacheTime:cacheTime];
|
|
NSString *directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:cacheDirKey];
|
|
NSString *directoryPath = [[NSUserDefaults standardUserDefaults] objectForKey:cacheDirKey];
|
|
|
|
+ directoryPath = [CACHE_PATH stringByAppendingPathComponent:directoryPath];
|
|
if (directoryPath && deleteFiles.count > 0) {
|
|
if (directoryPath && deleteFiles.count > 0) {
|
|
[deleteFiles enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
[deleteFiles enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
NSString *filePath = [directoryPath stringByAppendingPathComponent:obj];
|
|
NSString *filePath = [directoryPath stringByAppendingPathComponent:obj];
|