mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-06-04 01:54:58 +08:00
committed by
GitHub
parent
e619946b9c
commit
66a191b018
@@ -1,27 +1,29 @@
|
||||
import 'dart:io' show Directory, File;
|
||||
|
||||
import 'package:PiliPlus/utils/extension/file_ext.dart';
|
||||
import 'package:PiliPlus/utils/platform_utils.dart';
|
||||
import 'package:PiliPlus/utils/storage_pref.dart';
|
||||
import 'package:cached_network_image_ce/cached_network_image.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
abstract final class CacheManager {
|
||||
static late final DefaultCacheManager manager;
|
||||
|
||||
static Future<void> ensureInitialized() =>
|
||||
DefaultCacheManager.init().then((i) => manager = i);
|
||||
|
||||
// 获取缓存目录
|
||||
@pragma('vm:notify-debugger-on-exception')
|
||||
static Future<int> loadApplicationCache([
|
||||
final num maxSize = double.infinity,
|
||||
]) async {
|
||||
static Future<int> loadApplicationCache() async {
|
||||
try {
|
||||
final Directory tempDirectory = await getTemporaryDirectory();
|
||||
if (PlatformUtils.isDesktop) {
|
||||
final dir = Directory('${tempDirectory.path}/libCachedImageData');
|
||||
if (dir.existsSync()) {
|
||||
return await getTotalSizeOfFilesInDir(dir, maxSize);
|
||||
}
|
||||
return 0;
|
||||
return manager.getTotalLength();
|
||||
}
|
||||
|
||||
if (tempDirectory.existsSync()) {
|
||||
return await getTotalSizeOfFilesInDir(tempDirectory, maxSize);
|
||||
return await getTotalSizeOfFilesInDir(tempDirectory);
|
||||
}
|
||||
} catch (_) {}
|
||||
return 0;
|
||||
@@ -29,16 +31,21 @@ abstract final class CacheManager {
|
||||
|
||||
// 循环计算文件的大小
|
||||
@pragma('vm:notify-debugger-on-exception')
|
||||
static Future<int> getTotalSizeOfFilesInDir(
|
||||
final Directory file, [
|
||||
final num maxSize = double.infinity,
|
||||
]) async {
|
||||
final children = file.list(recursive: true);
|
||||
static Future<int> getTotalSizeOfFilesInDir(final Directory file) async {
|
||||
int total = 0;
|
||||
await for (final child in children) {
|
||||
await for (final child in file.list(recursive: false)) {
|
||||
if (child is File) {
|
||||
total += await child.length();
|
||||
if (total >= maxSize) break;
|
||||
} else if (child is Directory) {
|
||||
if (path.equals(child.path, manager.cacheDir)) {
|
||||
total += manager.getTotalLength();
|
||||
} else {
|
||||
await for (final i in child.list(recursive: true)) {
|
||||
if (i is File) {
|
||||
total += await i.length();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return total;
|
||||
@@ -57,16 +64,13 @@ abstract final class CacheManager {
|
||||
}
|
||||
|
||||
// 清除 Library/Caches 目录及文件缓存
|
||||
@pragma('vm:notify-debugger-on-exception')
|
||||
static Future<void> clearLibraryCache() async {
|
||||
try {
|
||||
final Directory tempDirectory = await getTemporaryDirectory();
|
||||
if (PlatformUtils.isDesktop) {
|
||||
final dir = Directory('${tempDirectory.path}/libCachedImageData');
|
||||
if (dir.existsSync()) {
|
||||
await dir.delete(recursive: true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
await manager.emptyCache();
|
||||
if (PlatformUtils.isDesktop) return;
|
||||
|
||||
final tempDirectory = await getTemporaryDirectory();
|
||||
if (tempDirectory.existsSync()) {
|
||||
final children = tempDirectory.list(recursive: false);
|
||||
await for (final file in children) {
|
||||
@@ -77,12 +81,16 @@ abstract final class CacheManager {
|
||||
}
|
||||
|
||||
static Future<void> autoClearCache() async {
|
||||
// TODO: remove
|
||||
Directory(
|
||||
'${(await getTemporaryDirectory()).path}/libCachedImageData',
|
||||
).tryDel(recursive: true);
|
||||
if (Pref.autoClearCache) {
|
||||
await clearLibraryCache();
|
||||
} else {
|
||||
final maxCacheSize = Pref.maxCacheSize;
|
||||
if (maxCacheSize != 0) {
|
||||
final currCache = await loadApplicationCache(maxCacheSize);
|
||||
final currCache = await loadApplicationCache();
|
||||
if (currCache >= maxCacheSize) {
|
||||
await clearLibraryCache();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user