// ignore_for_file: unnecessary_null_comparison, unused_element import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart' show DefaultCacheManager; import 'package:hgdj/tools_base/image/image_data_handle/image_cache_disk.dart'; import 'package:hgdj/tools_base/image/image_data_handle/image_manager.dart'; import 'package:hgdj/tools_base/video_download/video_download_manager.dart'; import 'package:path/path.dart' as path; import 'package:path_provider/path_provider.dart'; import '../../hj_utils/file_util.dart'; import '../../hj_utils/video_cache_manager.dart'; import 'image_cache_manager.dart'; ///加载缓存 统计缓存大小 Future loadCache() async { double total = 0; Directory videoCacheDir = Directory(await VideoCacheManager().getFilePath() ?? ''); if (await videoCacheDir.exists()) { total += await _getTotalSizeOfFilesInDir(videoCacheDir); } Directory videoLoadedDir = Directory(await VideoDownloadManager.instance.cacheDir()); if (await videoLoadedDir.exists()) { total += await _getTotalSizeOfFilesInDir(videoLoadedDir); } // 图片磁盘缓存(ImageCacheDisk) Directory imageDiskCacheDir = Directory(await ImageCacheDisk.findSavePath()); if (await imageDiskCacheDir.exists()) { total += await _getTotalSizeOfFilesInDir(imageDiskCacheDir); } // CachedNetworkImage 缓存(ImageCacheManager) Directory imageCacheDir = Directory(await ImageCacheManager().getFilePath()); if (await imageCacheDir.exists()) { total += await _getTotalSizeOfFilesInDir(imageCacheDir); } // NetworkImageLoader 在 encrypt:false 时(直播封面)走 DefaultCacheManager,目录单独统计 Directory defaultImageCacheDir = Directory(await _defaultImageCacheDirPath()); if (await defaultImageCacheDir.exists()) { total += await _getTotalSizeOfFilesInDir(defaultImageCacheDir); } return FileUtil.byteFmt(total.toInt()); } /// DefaultCacheManager 的磁盘目录:临时目录下的 'libCachedImageData'(flutter_cache_manager 默认 key) Future _defaultImageCacheDirPath() async { final dir = await getTemporaryDirectory(); return path.join(dir.path, 'libCachedImageData'); } Future _getTotalSizeOfFilesInDir(final FileSystemEntity file) async { try { if (file is File) { return (await file.length()).toDouble(); } if (file is Directory && await file.exists()) { double total = 0; // recursive 已一次性列出所有后代文件,只累加 File 即可;不再对子目录二次递归, // 避免同一文件被重复计入导致缓存大小翻倍。异步 list 替代 listSync,避免大目录阻塞 UI。 await for (final child in file.list(recursive: true, followLinks: false)) { if (child is File) { total += (await child.length()).toDouble(); } } return total; } } catch (e) { return 0; } return 0; } // 手动清理缓存 Future handleClearAllCache() async { await VideoDownloadManager.instance.emptyCache(); await ImageCacheDisk.emptyCache(); await VideoCacheManager().emptyCache(); await ImageCacheManager().emptyCache(); await DefaultCacheManager().emptyCache(); // 直播封面(encrypt:false)走的默认缓存 } /// 清除缓存如果必要 /// [force] 强至清除 Future clearCacheIfNeed({bool force = false, bool isHandle = false}) async { if (force) { await VideoCacheManager().emptyCache(); // await VideoSubCacheManager().emptyCache(); // CachedVideoStore().clean(); await ImageCacheManager().emptyCache(); if (isHandle) { //await VideoDownloadManager.instance.emptyCache(); // await ImageCacheDisk.emptyCache(); } debugPrint( " ------------------ VideoCacheManager ImageCacheManager clear completely"); } else { double total = 0; Directory videoCacheDir = Directory(await VideoCacheManager().getFilePath() ?? ''); if (await videoCacheDir.exists()) { total += await _getTotalSizeOfFilesInDir(videoCacheDir); } //ts流最大2M,应该是>1000个ts流 if (total > 500 * MB_SIZE) { await VideoCacheManager().emptyCache(); } total = 0; Directory imageCacheDir = Directory(await ImageCacheManager().getFilePath()); if (await imageCacheDir.exists()) { total += await _getTotalSizeOfFilesInDir(imageCacheDir); } //按照100Kb一张图片的化,估计是5000张网络图片 if (total > 500 * MB_SIZE) { await ImageCacheManager().emptyCache(); } } } clearAllCache(bool isHandle) async { //await clearCacheIfNeed(force: true,isHandle: isHandle); ImageManager.instance.clearBySize(maxSize: 1000); PaintingBinding.instance.imageCache.clear(); PaintingBinding.instance.imageCache.clearLiveImages(); debugPrint( " ----------- PaintingBinding.instance.imageCache.clearLiveImages()"); } ///递归方式删除目录 Future delDir(FileSystemEntity file) async { try { if (file is Directory) { final List children = file.listSync(); for (final FileSystemEntity child in children) { await delDir(child); } } await file.delete(); } catch (e) {} }