讀取緩存大小,目前計算了SDWebimage緩存大小,Documents目錄下某個文件夾大小(類似可以繼續添加)
-(CGFloat)readCacheSize;
清空緩存,目前清空了SDWebimage緩存,Documents目錄下某個文件夾緩存(類似可以繼續添加),Webview緩存
typedef void(^clearCacheSuccessBlock)(void); -(void)clearCacheWithCompletionBlock:(nullable clearCacheSuccessBlock)completionBlock;
代碼示例
-(CGFloat)readCacheSize { CGFloat size=0; //獲取SDWebImage磁盤緩存大小 CGFloat size_SDWebImage = [self readSDWebImageCacheSize]; size = size + size_SDWebImage; //獲取短視頻背景音樂緩存大小 CGFloat size_ShortVideoBgm = [self readShortVideoBgmCacheSize]; size = size + size_ShortVideoBgm; CGFloat sizeMB = size>0?(size/1024.0/1024.0):0; return sizeMB; } #pragma mark - 清空緩存 -(void)clearCacheWithCompletionBlock:(nullable clearCacheSuccessBlock)completionBlock{ [HUDManager showLoadingHUDView:[UIViewController currentViewController].view withText:@"正在清除緩存···"]; //清除H5緩存 [self clearURLCacheAndHTTPCookie]; //清除SDWebImage緩存 [self clearSDWebImageCache]; //清除短視頻背景音樂緩存 [self clearShortVideoBgmCache]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [HUDManager hideHUDView]; [[UIViewController currentViewController].view showMessageWithText:@"已清空緩存"]; }); } //獲取SDWebImage磁盤緩存大小 -(CGFloat)readSDWebImageCacheSize { //SDImageCache NSLog(@"圖片緩存路徑%@",[SDImageCache sharedImageCache].diskCachePath); NSUInteger size = [[SDImageCache sharedImageCache] totalDiskSize]; return size; } #pragma mark - 清空緩存 -(void)clearSDWebImageCache{ [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{ }]; [[SDImageCache sharedImageCache] clearMemory]; } //清除webview緩存 - (void)clearURLCacheAndHTTPCookie { NSSet *websiteDataTypes = [NSSet setWithArray:@[ WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeOfflineWebApplicationCache, WKWebsiteDataTypeMemoryCache, WKWebsiteDataTypeLocalStorage, WKWebsiteDataTypeCookies, WKWebsiteDataTypeSessionStorage, WKWebsiteDataTypeIndexedDBDatabases, WKWebsiteDataTypeWebSQLDatabases ]]; NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0]; [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{ // 結束回調 }]; } //獲取短視頻背景音樂緩存大小 -(CGFloat)readShortVideoBgmCacheSize { float size = 0; NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/bgm"]; if(![[NSFileManager defaultManager] fileExistsAtPath:path]){ return 0; } size=(float)[self totalSizeAtPath:path]; return size; } //清除短視頻背景音樂緩存 -(void)clearShortVideoBgmCache { NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/bgm"]; [[NSFileManager defaultManager] removeItemAtPath:path error:nil]; } //獲得當前文件夾及下面所有文件大小 - (uint64_t)totalSizeAtPath:(NSString *)path { NSFileManager *fileManager = NSFileManager.defaultManager; NSDictionary<NSString *, id> *attributes = [fileManager attributesOfItemAtPath:path error:NULL]; uint64_t totalSize = [attributes fileSize]; for (NSString *fileName in [fileManager enumeratorAtPath:path]) { attributes = [fileManager attributesOfItemAtPath:[path stringByAppendingPathComponent:fileName] error:NULL]; totalSize += [attributes fileSize]; } return totalSize; }
備注:
以下代碼只能獲取當前文件夾大小,不能獲得子目錄文件夾大小
[attributes fileSize];