feat: max cache size

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-04-20 20:39:17 +08:00
parent a2af297a84
commit ae901c709d
7 changed files with 78 additions and 10 deletions

View File

@@ -95,7 +95,7 @@
- [x] 合集图片 - [x] 合集图片
- [x] 删除/置顶/撤回私信 - [x] 删除/置顶/撤回私信
- [x] 举报用户/评论/视频/动态 - [x] 举报用户/评论/视频/动态
- [x] 删除/发布文本/图片动态 - [x] 删除/发布/置顶文本/图片动态
- [x] 其他 - [x] 其他
## opt ## opt

View File

@@ -31,8 +31,16 @@ void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
MediaKit.ensureInitialized(); MediaKit.ensureInitialized();
await GStorage.init(); await GStorage.init();
if (GStorage.setting.get(SettingBoxKey.autoClearCache, defaultValue: true)) { if (GStorage.setting.get(SettingBoxKey.autoClearCache, defaultValue: false)) {
await CacheManage.clearLibraryCache(); await CacheManage.clearLibraryCache();
} else {
final num maxCacheSize = GStorage.maxCacheSize;
if (maxCacheSize != 0) {
final double currCache = await CacheManage().loadApplicationCache();
if (currCache >= maxCacheSize) {
await CacheManage.clearLibraryCache();
}
}
} }
if (GStorage.setting if (GStorage.setting
.get(SettingBoxKey.horizontalScreen, defaultValue: false)) { .get(SettingBoxKey.horizontalScreen, defaultValue: false)) {

View File

@@ -46,7 +46,8 @@ class _AboutPageState extends State<AboutPage> {
} }
Future getCacheSize() async { Future getCacheSize() async {
cacheSize.value = await CacheManage().loadApplicationCache(); cacheSize.value =
CacheManage.formatSize(await CacheManage().loadApplicationCache());
} }
Future getCurrentApp() async { Future getCurrentApp() async {

View File

@@ -35,6 +35,7 @@ import 'package:PiliPlus/plugin/pl_player/models/bottom_progress_behavior.dart';
import 'package:PiliPlus/plugin/pl_player/models/fullscreen_mode.dart'; import 'package:PiliPlus/plugin/pl_player/models/fullscreen_mode.dart';
import 'package:PiliPlus/plugin/pl_player/utils/fullscreen.dart'; import 'package:PiliPlus/plugin/pl_player/utils/fullscreen.dart';
import 'package:PiliPlus/utils/accounts/account_manager/account_mgr.dart'; import 'package:PiliPlus/utils/accounts/account_manager/account_mgr.dart';
import 'package:PiliPlus/utils/cache_manage.dart';
import 'package:PiliPlus/utils/extension.dart'; import 'package:PiliPlus/utils/extension.dart';
import 'package:PiliPlus/utils/feed_back.dart'; import 'package:PiliPlus/utils/feed_back.dart';
import 'package:PiliPlus/utils/global_data.dart'; import 'package:PiliPlus/utils/global_data.dart';
@@ -2464,7 +2465,56 @@ List<SettingsModel> get extraSettings => [
subtitle: '每次启动时清除缓存', subtitle: '每次启动时清除缓存',
leading: const Icon(Icons.auto_delete_outlined), leading: const Icon(Icons.auto_delete_outlined),
setKey: SettingBoxKey.autoClearCache, setKey: SettingBoxKey.autoClearCache,
defaultVal: true, defaultVal: false,
),
SettingsModel(
settingsType: SettingsType.normal,
title: '最大缓存大小',
getSubtitle: () {
final num = GStorage.maxCacheSize;
return '当前最大缓存大小: 「${num == 0 ? '无限' : CacheManage.formatSize(GStorage.maxCacheSize)}';
},
onTap: (setState) {
showDialog(
context: Get.context!,
builder: (context) {
String valueStr = '';
return AlertDialog(
title: const Text('最大缓存大小'),
content: TextField(
autofocus: true,
onChanged: (value) => valueStr = value,
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'[\d\.]+')),
],
decoration: InputDecoration(suffixText: 'MB'),
),
actions: [
TextButton(
onPressed: Get.back,
child: Text(
'取消',
style: TextStyle(
color: Theme.of(context).colorScheme.outline),
),
),
TextButton(
onPressed: () async {
Get.back();
num value = num.tryParse(valueStr) ?? 0;
await GStorage.setting
.put(SettingBoxKey.maxCacheSize, value * 1024 * 1024);
setState();
},
child: const Text('确定'),
),
],
);
},
);
},
leading: const Icon(Icons.delete_outlined),
), ),
SettingsModel( SettingsModel(
settingsType: SettingsType.sw1tch, settingsType: SettingsType.sw1tch,

View File

@@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:PiliPlus/utils/extension.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
class CacheManage { class CacheManage {
@@ -10,7 +11,7 @@ class CacheManage {
factory CacheManage() => cacheManage; factory CacheManage() => cacheManage;
// 获取缓存目录 // 获取缓存目录
Future<String> loadApplicationCache() async { Future<double> loadApplicationCache() async {
/// clear all of image in memory /// clear all of image in memory
// clearMemoryImageCache(); // clearMemoryImageCache();
/// get ImageCache /// get ImageCache
@@ -41,7 +42,7 @@ class CacheManage {
cacheSize += value; cacheSize += value;
} }
return formatSize(cacheSize); return cacheSize;
} }
// 循环计算文件的大小(递归) // 循环计算文件的大小(递归)
@@ -62,15 +63,15 @@ class CacheManage {
} }
// 缓存大小格式转换 // 缓存大小格式转换
static String formatSize(double value) { static String formatSize(num value) {
List<String> unitArr = ['B', 'K', 'M', 'G']; List<String> unitArr = const ['B', 'K', 'M', 'G', 'T', 'P'];
int index = 0; int index = 0;
while (value > 1024) { while (value >= 1024) {
index++; index++;
value = value / 1024; value = value / 1024;
} }
String size = value.toStringAsFixed(2); String size = value.toStringAsFixed(2);
return size + unitArr[index]; return size + unitArr.getOrElse(index, orElse: () => '');
} }
/// 清除 Documents 目录下的 DioCache.db /// 清除 Documents 目录下的 DioCache.db

View File

@@ -45,6 +45,10 @@ extension ListExt<T> on List<T>? {
return this![index]; return this![index];
} }
T getOrElse(int index, {required T Function() orElse}) {
return getOrNull(index) ?? orElse();
}
bool eq(List<T>? other) { bool eq(List<T>? other) {
if (this == null) { if (this == null) {
return other == null; return other == null;

View File

@@ -468,6 +468,9 @@ class GStorage {
SettingBoxKey.pageTransition, SettingBoxKey.pageTransition,
defaultValue: Transition.native.index)]; defaultValue: Transition.native.index)];
static num get maxCacheSize => GStorage.setting
.get(SettingBoxKey.maxCacheSize, defaultValue: pow(1024, 3));
static List<double> get dynamicDetailRatio => List<double>.from(setting static List<double> get dynamicDetailRatio => List<double>.from(setting
.get(SettingBoxKey.dynamicDetailRatio, defaultValue: [60.0, 40.0])); .get(SettingBoxKey.dynamicDetailRatio, defaultValue: [60.0, 40.0]));
@@ -669,6 +672,7 @@ class SettingBoxKey {
/// 其他 /// 其他
autoUpdate = 'autoUpdate', autoUpdate = 'autoUpdate',
autoClearCache = 'autoClearCache', autoClearCache = 'autoClearCache',
maxCacheSize = 'maxCacheSize',
defaultShowComment = 'defaultShowComment', defaultShowComment = 'defaultShowComment',
replySortType = 'replySortType', replySortType = 'replySortType',
defaultDynamicType = 'defaultDynamicType', defaultDynamicType = 'defaultDynamicType',