iOS开发笔记-33: 一般app中使用的缓存清除

    //PINCache
    [[PINCache sharedCache]removeAllObjects];
    [[PINMemoryCache sharedCache] removeAllObjects];
    //[JJCacheManager clearPINRemoteImageManagerCache];

    //YYCache
    [[JJYYCacheManager defaultManager] removAllCache];
    //SDWCashe
    //[[JJSDWManager defaultManager] clearCash];
    [[[SDWebImageManager sharedManager] imageCache] clearDisk];
    最新版本clearDisk弃用,改为:[[[SDWebImageManager sharedManager] imageCache] clearDiskOnCompletion:nil];
    [[[SDWebImageManager sharedManager] imageCache] clearMemory];
    //webkitCache
    [JJCacheManager clearWKWebKitCache];


#pragma mark - 获取path路径下文件夹大小
+ (NSString *)getCacheSizeWithFilePath:(NSString *)path;
#pragma mark - 清除path文件夹下缓存大小 ps:webkit缓存里面有些内容没有权限删除,需要用clearWKWebKitCache方法删除
+ (BOOL)clearCacheWithFilePath:(NSString *)path;
#pragma mark - 删除webkit缓存
+ (BOOL)clearWKWebKitCache;
#pragma mark - pincahce中的缓存需要手动删除
+ (BOOL)clearPINRemoteImageManagerCache;
+ (BOOL)clearPINRemoteImageManagerCache {
    
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSString *path = [cachesPath stringByAppendingPathComponent:@"/com.pinterest.PINDiskCache.PINRemoteImageManagerCache"];
    //拿到path路径的下一级目录的子文件夹
    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) {
            JJLog(@"clearPINRemoteImageManagerCache : %@",error);
            return NO;
        }
    }
    return YES;
}

+ (BOOL)clearWKWebKitCache {
    
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    NSString *path = [cachesPath stringByAppendingPathComponent:@"/WebKit"];
    //拿到path路径的下一级目录的子文件夹
    NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
    NSString *filePath = nil;
    NSError *error = nil;
    for (NSString *subPath in subPathArr) {
        filePath = [path stringByAppendingPathComponent:subPath];
        //  其实主要就是这个没有清除权限
        if (![filePath containsString:@"/Caches/Snapshots"]) {
            //删除子文件夹
            [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        }
        if (error) {
            JJLog(@"clearWKWebKitCache : %@",error);
            return NO;
        }
    }
    return YES;
}

#pragma mark - 获取path路径下文件夹大小
+ (NSString *)getCacheSizeWithFilePath:(NSString *)path{
    
    // 获取“path”文件夹下的所有文件
    NSArray *subPathArr = [[NSFileManager defaultManager] subpathsAtPath:path];
    
    NSString *filePath  = nil;
    NSInteger totleSize = 0;
    
    for (NSString *subPath in subPathArr){
        
        // 1. 拼接每一个文件的全路径
        filePath =[path stringByAppendingPathComponent:subPath];
        
        // 2. 是否是文件夹,默认不是
        BOOL isDirectory = NO;
        
        // 3. 判断文件是否存在
        BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
        
        
        // 4. 以上判断目的是忽略不需要计算的文件
        if (!isExist || isDirectory || [filePath containsString:@".DS"]){
            // 过滤: 1. 文件夹不存在  2. 过滤文件夹  3. 隐藏文件
            continue;
        }
        
        // 5. 指定路径,获取这个路径的属性
        NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        /**
         attributesOfItemAtPath: 文件夹路径
         该方法只能获取文件的属性, 无法获取文件夹属性, 所以也是需要遍历文件夹的每一个文件的原因
         */
        
        // 6. 获取每一个文件的大小
        NSInteger size = [dict[@"NSFileSize"] integerValue];
        
        // 7. 计算总大小
        totleSize += size;
    }
    
    //8. 将文件夹大小转换为 M/KB/B
    NSString *totleStr = nil;
    
    if (totleSize > 1000 * 1000){
        totleStr = [NSString stringWithFormat:@"%.2fM",totleSize / 1000.00f /1000.00f];
        
    }else if (totleSize > 1000){
        totleStr = [NSString stringWithFormat:@"%.2fKB",totleSize / 1000.00f ];
        
    }else{
        totleStr = [NSString stringWithFormat:@"%.2fB",totleSize / 1.00f];
    }
    
    return totleStr;
}


#pragma mark - 清除path文件夹下缓存大小
+ (BOOL)clearCacheWithFilePath:(NSString *)path{
    
    //拿到path路径的下一级目录的子文件夹
    NSArray *subPathArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
    
    NSString *message  = nil;
    NSString *filePath = nil;
    
    NSError *error = nil;
    
    for (NSString *subPath in subPathArr)
    {
        filePath = [path stringByAppendingPathComponent:subPath];
        
//        NSString *cacheSize = [JJCacheManager getCacheSizeWithFilePath:filePath];
        
//        JJLog(@"%@\n\n%@\n",filePath,cacheSize);
        //删除子文件夹
        [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
        if (error) {
            return NO;
        }
    }
    return YES;
}

你可能感兴趣的:(iOS开发笔记-33: 一般app中使用的缓存清除)