iOS计算缓存和清除缓存

/** 缓存大小 */

@property (nonatomic, assign) NSInteger fileTotlaSize;

/开始计算缓存大小

- (void)calculateCacheSizeWithoutURLCache:(BOOL)withoutURLCache{

    //开始计算缓存大小

    _fileTotlaSize = 0;

    WS(weakSelf);

    if(withoutURLCache ==NO) {

        //计算APP中H5占用内存和硬盘空间小

        _fileTotlaSize += [NSURLCache sharedURLCache].currentMemoryUsage;

        _fileTotlaSize += [NSURLCache sharedURLCache].currentDiskUsage;

    } 

     //计算APP下载的图片大小

    [SDWebImageManager.sharedManager.imageCache calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {

        weakSelf.fileTotlaSize+= totalSize;

        [weakSelf updateCacheSize];

    }];

}

- (void)updateCacheSize{

    [_cacheLabel setText:[NSString stringWithFormat:@"%.2fM",_fileTotlaSize/1024.0f/1024.0f]];

    [MessageTool hideCurrentProgressHUD];

}
1.//删除本地H5缓存数据

+ (void)cleanCacheAndCookie{

    if(@available(iOS9.0, *)) {

        //清除WKWebView的缓存

        NSSet*websiteDataTypes = [WKWebsiteDataStoreallWebsiteDataTypes];

        // Date from

        NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];

        // Execute

        dispatch_async(dispatch_get_main_queue(), ^{

            [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{

            }];

        });

    }

    NSURLCache * cache = [NSURLCache sharedURLCache];

    [cacheremoveAllCachedResponses];

    [cachesetDiskCapacity:0];

    [cachesetMemoryCapacity:0];

}

2./ /删除SDWebImage 下载图片缓存数据

    [SDWebImageManager.sharedManager.imageCache clearMemory];

    //删除下载图片本地数据

    [SDWebImageManager.sharedManager.imageCache clearDiskOnCompletion:^{

    }];

3.//删除本地简单存储数据

#define SimpleSavePath [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"SFNetwork"]

+ (void)clearSimpleSaveLocallyData{

    //删除简单存储文件夹

    NSFileManager *fm = [NSFileManager defaultManager];

    if ([fm removeItemAtPath:SimpleSavePath error:nil]) {

        NSLog(@"删除简单本地存储文件夹成功!");

    }

}

你可能感兴趣的:(iOS计算缓存和清除缓存)