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

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

View File

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

View File

@@ -468,6 +468,9 @@ class GStorage {
SettingBoxKey.pageTransition,
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
.get(SettingBoxKey.dynamicDetailRatio, defaultValue: [60.0, 40.0]));
@@ -669,6 +672,7 @@ class SettingBoxKey {
/// 其他
autoUpdate = 'autoUpdate',
autoClearCache = 'autoClearCache',
maxCacheSize = 'maxCacheSize',
defaultShowComment = 'defaultShowComment',
replySortType = 'replySortType',
defaultDynamicType = 'defaultDynamicType',