fix cache

This commit is contained in:
nyne
2024-10-27 18:45:57 +08:00
parent da5f6968b9
commit 8b95442dad

View File

@@ -1,5 +1,3 @@
import 'dart:io';
import 'package:crypto/crypto.dart'; import 'package:crypto/crypto.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:sqlite3/sqlite3.dart'; import 'package:sqlite3/sqlite3.dart';
@@ -84,9 +82,7 @@ class CacheManager {
if(_currentSize != null) { if(_currentSize != null) {
_currentSize = _currentSize! + data.length; _currentSize = _currentSize! + data.length;
} }
if(_currentSize != null && _currentSize! > _limitSize){ checkCacheIfRequired();
await checkCache();
}
} }
Future<CachingFile> openWrite(String key) async{ Future<CachingFile> openWrite(String key) async{
@@ -123,6 +119,12 @@ class CacheManager {
bool _isChecking = false; bool _isChecking = false;
void checkCacheIfRequired() {
if(_currentSize != null && _currentSize! > _limitSize){
checkCache();
}
}
Future<void> checkCache() async{ Future<void> checkCache() async{
if(_isChecking){ if(_isChecking){
return; return;
@@ -279,6 +281,7 @@ class CachingFile{
CacheManager()._db.execute(''' CacheManager()._db.execute('''
INSERT OR REPLACE INTO cache (key, dir, name, expires) VALUES (?, ?, ?, ?) INSERT OR REPLACE INTO cache (key, dir, name, expires) VALUES (?, ?, ?, ?)
''', [key, dir, name, DateTime.now().millisecondsSinceEpoch + 7 * 24 * 60 * 60 * 1000]); ''', [key, dir, name, DateTime.now().millisecondsSinceEpoch + 7 * 24 * 60 * 60 * 1000]);
CacheManager().checkCacheIfRequired();
} }
Future<void> cancel() async{ Future<void> cancel() async{