幾乎每個應(yīng)用都有清除緩存的功能,一個應(yīng)用的使用難免會產(chǎn)生緩存,如文件緩存、圖片緩存,我們大部分應(yīng)用使用的SDWebImage就緩存了許多圖片,當(dāng)然它也提供的清除圖片緩存的功能,這里我們介紹使用系統(tǒng)方法清除(肯定的,SDWebImage清除緩存也是封裝的系統(tǒng)方法)為了提高應(yīng)用性能,用戶體驗(yàn)的友好,清除緩存勢在必得!
如可使用如下自己封裝的方法
1.獲取某個文件緩存大小
+(long long)fileSizeWithPath:(NSString *)filePath;
2.獲取整個緩存大小
+ (NSString *)getAllCacheSize;
3.清除所有緩存,清除完成回調(diào)一個block
+ (void)removeCache:(void(^)())removeComplete;
具體各個方法實(shí)現(xiàn).m
- 獲取某個文件緩存大小
//獲取單個文件方法特意提出來寫,方便以后可以有這種業(yè)務(wù)需求
+(long long)fileSizeWithPath:(NSString *)filePath{
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath :filePath]){
return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize];
}
return 0 ;
}
- 獲取整個緩存大小
獲取整個緩存文件大小,我這直接返回了 NSSring 類型,因?yàn)榉奖阄易约喉?xiàng)目的使用,當(dāng)然你可以仿造上面方法用long long類型接收。毋庸置疑,獲取整個緩存文件就是遍歷 所有的單個文件緩存。
+ (NSString *)getAllCacheSize
{
//獲取緩存路徑
NSString *cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES) firstObject];
NSLog(@"路徑:%@",cachePath);
//創(chuàng)建文件管理對象
NSFileManager *filemanager = [NSFileManager defaultManager];
//判斷路徑不存在,返回0.00M
if (![filemanager fileExistsAtPath :cachePath]) return @"0.00M" ;
NSEnumerator *childFilesEnumerator = [[filemanager subpathsAtPath :cachePath] objectEnumerator];
NSString *fileName;
long long sumSize = 0;
//遍歷所有子文件
while ((fileName = [childFilesEnumerator nextObject]) != nil ){
//拼接完整路徑
NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :fileName];
//計(jì)算文件的大小
sumSize += [ self fileSizeWithPath :fileAbsolutePath];
}
float size_m = sumSize/(1024.00*1024.00);//注意:如果你要保持浮點(diǎn)型,1024必須帶小數(shù)點(diǎn),不然得到的是int類型;或者強(qiáng)制轉(zhuǎn)換(float)sumSize/(1024*1024)
return [NSString stringWithFormat:@"%.2fM",size_m];
}
- 異步實(shí)現(xiàn)清理緩存
+ (void)removeCache:(void(^)())removeComplete
{
//異步
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//文件路徑
NSString *directoryPath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSArray *subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
//循環(huán)子路徑清除
for (NSString *subPath in subpaths) {
NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
//返回主線程
dispatch_async(dispatch_get_main_queue(), ^{
if (removeComplete) {
removeComplete();//可在此block做UI刷新操作,如果用了異步,UI操作必須返回主線程
}
});
});
}
- 通過網(wǎng)上的搜索,還發(fā)現(xiàn)有些不同的方法
據(jù)網(wǎng)上作者描述測試,該方法獲取的緩存大小少于第一種方法,刪除也是刪除相應(yīng)方法獲取的緩存。該方法應(yīng)該只是獲取Caches里面的圖片緩存,并沒有計(jì)算其他文件的緩存;
//獲取緩存方法
+ (NSString *)getAllCacheSize{
//定義變量存儲總的緩存大小
long long sumSize = 0;
//獲取當(dāng)前緩存路徑
NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
NSLog(@"路徑:%@",cacheFilePath);
//創(chuàng)建文件管理對象
NSFileManager *filemanager = [NSFileManager defaultManager];
//獲取當(dāng)前緩存路徑下的所有子路徑
NSArray *subPaths = [filemanager subpathsOfDirectoryAtPath:cacheFilePath error:nil];
//遍歷所有子文件
for (NSString *subPath in subPaths) {
//1).拼接完整路徑
NSString *filePath = [cacheFilePath stringByAppendingFormat:@"/%@",subPath];
//2).計(jì)算文件的大小
long long fileSize = [[filemanager attributesOfItemAtPath:filePath error:nil]fileSize];
//3).加載到文件的大小
sumSize += fileSize;
}
float size_m = sumSize/(1024.00*1024.00);
return [NSString stringWithFormat:@"%.2fM",size_m];
}
//刪除緩存方法
+ (void)removeCache{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *cacheFilePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
[fileManager removeItemAtPath:cacheFilePath error:nil];
}