iOS 實(shí)現(xiàn)緩存的計(jì)算和清理

在實(shí)現(xiàn)緩存的計(jì)算和清理之前,我們要先知道緩存存在哪里了。
應(yīng)用的沙盒主要有以下幾個(gè)目錄:
Document:適合存儲(chǔ)重要的數(shù)據(jù), iTunes同步應(yīng)用時(shí)會(huì)同步該文件下的內(nèi)容
Library/Caches:適合存儲(chǔ)體積大,不需要備份的非重要數(shù)據(jù),iTunes不會(huì)同步該文件
Library/Preferences:通常保存應(yīng)用的設(shè)置信息, iTunes會(huì)同步
tmp:保存應(yīng)用的臨時(shí)文件,用完就刪除,系統(tǒng)可能在應(yīng)用沒在運(yùn)行時(shí)刪除該目錄下的文件,iTunes不會(huì)同步
由此知道,緩存一般是存儲(chǔ)在Library/Caches這個(gè)路徑下。

我們加載網(wǎng)絡(luò)圖片多數(shù)都使用的SDWebImage,SDWebImage提供了方法計(jì)算緩存和清理緩存:

// totalSize緩存大小
[[SDImageCache sharedImageCache] calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
    CGFloat mbSize = totalSize/1000/1000;
    DEBUGLog(@"緩存 : %f",mbSize);
}];
    
//清理緩存
[[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];

不過SDWebImage清理的是它緩存下來的圖片,如果有其他緩存是沒有計(jì)算在內(nèi)的。

自己實(shí)現(xiàn)緩存計(jì)算和清理:

+ (NSInteger)getCacheSizeWithFilePath:(NSString *)path {
    // 獲取“path”文件夾下的所有文件
    NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path];
    NSString *filePath = nil;
    NSInteger totleSize = 0;
    for (NSString *subPath in subPathArr) {
        
        filePath = [path stringByAppendingPathComponent:subPath];
        
        BOOL isDirectory = NO;
        
        BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
        
        //忽略不需要計(jì)算的文件:文件夾不存在/ 過濾文件夾/隱藏文件
        if (!isExist || isDirectory || [filePath containsString:@".DS"]) {
            
            continue;  
        }
        
        /** attributesOfItemAtPath: 文件夾路徑 該方法只能獲取文件的屬性, 無法獲取文件夾屬性, 所以也是需要遍歷文件夾的每一個(gè)文件的原因 */
        NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        
        NSInteger size = [dict[@"NSFileSize"] integerValue];
        // 計(jì)算總大小
        totleSize += size;   
    }
    
    return totleSize;
}

//將文件大小轉(zhuǎn)換為 M/KB/B
+ (NSString *)fileSizeConversion:(NSInteger)totalSize {
    
    NSString *totleStr = nil;
    if (totalSize > 1000 * 1000) {
        totleStr = [NSString stringWithFormat:@"%.2fM",totalSize / 1000.00f /1000.00f];
        
    } else if (totalSize > 1000) {
        totleStr = [NSString stringWithFormat:@"%.2fKB",totalSize / 1000.00f ];
        
    } else {
        totleStr = [NSString stringWithFormat:@"%.2fB",totalSize / 1.00f];
        
    }
    return totleStr;
}

//清除path文件夾下緩存
+ (BOOL)clearCacheWithFilePath:(NSString *)path {
    //拿到path路徑的下一級(jí)目錄的子文件夾
    NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
    NSString *filePath = nil;
    NSError *error = nil;
    for (NSString *subPath in subPathArr) {
        filePath = [path stringByAppendingPathComponent:subPath];
        //刪除子文件夾
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        if (error) {
            return NO;
        }
        
    }
    
    return YES;
}

//緩存路徑
NSString *libraryCachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;

調(diào)用方法getCacheSizeWithFilePath,path傳上面的緩存路徑,就可以計(jì)算出緩存大小,調(diào)用clearCacheWithFilePath清理緩存。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容