缓存管理类,是一个简单的缓存管理封装,他通过几个接口共 APP 组件使用。
该类需要使用到以下依赖:
首先建立缓存管理类,并规划好一个大纲:
/// 缓存管理类/// ./lib/utils/cache_util.dartclass CacheUtil { /// 获取缓存大小 static Future<int> total() async {} /// 清除缓存 static Future<int> clear() async {} /// 递归缓存目录,计算缓存大小 static Future<int> _reduce() async {} /// 递归删除缓存目录和文件 static Future<int> _delete() async {}}获取缓存大小,需要递归处理计算缓存目录下的文件大小。
/// 获取缓存大小static Future<int> total() async { Directory tempDir = await getTemporaryDirectory(); if (tempDir == null) return 0; int total = await _reduce(tempDir); return total;}递归计算缓存目录:
/// 递归缓存目录,计算缓存大小static Future<int> _reduce(final FileSystemEntity file) async { /// 如果是一个文件,则直接返回文件大小 if (file is File) { int length = await file.length(); return length; } /// 如果是目录,则遍历目录并累计大小 if (file is Directory) { final List<FileSystemEntity> children = file.listSync(); int total = 0; if (children != null && children.isNotEmpty) for (final FileSystemEntity child in children) total += await _reduce(child); return total; } return 0;}和递归获取缓存目录下的文件大小类似,清除缓存就是遍历删除缓存目录下的文件:
/// 清除缓存static Future<void> clear() async { Directory tempDir = await getTemporaryDirectory(); if (tempDir == null) return; await _delete(tempDir);}递归清除缓存目录:
/// 递归删除缓存目录和文件static Future<void> _delete(FileSystemEntity file) async { if (file is Directory) { final List<FileSystemEntity> children = file.listSync(); for (final FileSystemEntity child in children) { await _delete(child); } } else { await file.delete(); }}完整代码如下:
import 'dart:io';import 'package:path_provider/path_provider.dart';/// 缓存管理类/// ./lib/utils/cache_util.dartclass CacheUtil { /// 获取缓存大小 static Future<int> total() async { Directory tempDir = await getTemporaryDirectory(); if (tempDir == null) return 0; int total = await _reduce(tempDir); return total; } /// 清除缓存 static Future<void> clear() async { Directory tempDir = await getTemporaryDirectory(); if (tempDir == null) return; await _delete(tempDir); } /// 递归缓存目录,计算缓存大小 static Future<int> _reduce(final FileSystemEntity file) async { /// 如果是一个文件,则直接返回文件大小 if (file is File) { int length = await file.length(); return length; } /// 如果是目录,则遍历目录并累计大小 if (file is Directory) { final List<FileSystemEntity> children = file.listSync(); int total = 0; if (children != null && children.isNotEmpty) for (final FileSystemEntity child in children) total += await _reduce(child); return total; } return 0; } /// 递归删除缓存目录和文件 static Future<void> _delete(FileSystemEntity file) async { if (file is Directory) { final List<FileSystemEntity> children = file.listSync(); for (final FileSystemEntity child in children) { await _delete(child); } } else { await file.delete(); } }}实战中,我们添加 filesize(https://links.jianshu.com/go?to=https%3A%2F%2Fpub.flutter-
io.cn%2Fpackages%2Ffilesize) 依赖用来友好显示文件尺寸。使用 ValueNotifier 及
ValueListenableBuilder 更新界面。实现过程如下:
cacheSize :ValueNotifier<int> cacheSize = ValueNotifier(0);initCache 异步方法,用来刷新缓存,在 initStat 和 清除缓存 时调用,已实现实时刷新。Simulator Screen Shot - iPhone 11 - 2021-03-27 at 13.06.05.png
ValueListenableBuilder( valueListenable: cacheSize, builder: (BuildContext context, int size, Widget _) { return Tile( title: Text('本地缓存'), titleSub: Text('点击清除缓存,但不会清除已下载的歌曲'), detail: size != null && size > 0 ? filesize(size) : '', action: SizedBox(width: 16), onPressed: handleClearCache, ); },)Future<void> initCache() async { /// 获取缓存大小 int size = await CacheUtil.total(); /// 复制变量 cacheSize.value = size ?? 0;}Future<void> handleClearCache() async { try { if (cacheSize.value <= 0) throw '没有缓存可清理'; /// 给予适当的提示 /// bool confirm = await showDialog(); /// if (confirm != true) return; /// 执行清除缓存 await CacheUtil.clear(); /// 更新缓存 await initCache(); AppUtil.showToast('缓存清除成功'); } catch (e) { AppUtil.showToast(e); }}原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。