diff --git a/flutter_cache_manager/CHANGELOG.md b/flutter_cache_manager/CHANGELOG.md index 275c791b..5003cb06 100644 --- a/flutter_cache_manager/CHANGELOG.md +++ b/flutter_cache_manager/CHANGELOG.md @@ -1,3 +1,7 @@ +## [3.4.7] - 2026-10-28 + +* Keeps cache cleanup going when a cached file cannot be deleted: the cache info of the other removed files is still deleted, and only the failed file's cache info is kept so the next cleanup retries it. + ## [3.4.6] - 2026-10-27 * Catches unhandled asynchronous errors when updating cache-hit metadata and during scheduled cleanup in `CacheStore` ([#535](https://github.com/Baseflow/flutter_cache_manager/issues/535)). diff --git a/flutter_cache_manager/lib/src/cache_store.dart b/flutter_cache_manager/lib/src/cache_store.dart index eaf4d189..67fad4c8 100644 --- a/flutter_cache_manager/lib/src/cache_store.dart +++ b/flutter_cache_manager/lib/src/cache_store.dart @@ -198,34 +198,48 @@ class CacheStore { } Future _cleanupCache() async { - final toRemove = []; + final started = {}; + final removed = []; final provider = await _cacheInfoRepository; + final removals = >[]; final overCapacity = await provider.getObjectsOverCapacity(_capacity); - final futures = []; for (final cacheObject in overCapacity) { - futures.add(_removeCachedFile(cacheObject, toRemove)); + removals.add( + _removeCachedFile(cacheObject, started: started, removed: removed), + ); } final oldObjects = await provider.getOldObjects(_maxAge); for (final cacheObject in oldObjects) { - futures.add(_removeCachedFile(cacheObject, toRemove)); + removals.add( + _removeCachedFile(cacheObject, started: started, removed: removed), + ); } - await Future.wait(futures); - await provider.deleteAll(toRemove); + await _deleteCacheInfoOfRemoved( + provider: provider, + removals: removals, + removed: removed, + ); } Future emptyCache() async { final provider = await _cacheInfoRepository; - final toRemove = []; + final started = {}; + final removed = []; final allObjects = await provider.getAllObjects(); - var futures = []; + final removals = >[]; for (final cacheObject in allObjects) { - futures.add(_removeCachedFile(cacheObject, toRemove)); + removals.add( + _removeCachedFile(cacheObject, started: started, removed: removed), + ); } - await Future.wait(futures); - await provider.deleteAll(toRemove); + await _deleteCacheInfoOfRemoved( + provider: provider, + removals: removals, + removed: removed, + ); } void emptyMemoryCache() { @@ -234,18 +248,61 @@ class CacheStore { Future removeCachedFile(CacheObject cacheObject) async { final provider = await _cacheInfoRepository; - final toRemove = []; - await _removeCachedFile(cacheObject, toRemove); - await provider.deleteAll(toRemove); + final removed = []; + final removal = _removeCachedFile( + cacheObject, + started: {}, + removed: removed, + ); + await _deleteCacheInfoOfRemoved( + provider: provider, + removals: [removal], + removed: removed, + ); + } + + /// Waits for [removals] and then deletes the cache info of the files in + /// [removed], even when some removals failed. + Future _deleteCacheInfoOfRemoved({ + required CacheInfoRepository provider, + required List> removals, + required List removed, + }) async { + Object? removalError; + StackTrace? removalStack; + try { + await Future.wait(removals); + } catch (e, s) { + removalError = e; + removalStack = s; + } + + try { + await provider.deleteAll(removed); + } catch (e, s) { + if (removalError == null) rethrow; + cacheLogger.log( + 'CacheStore: Failed to delete cache info with error:\n$e\n$s', + CacheManagerLogLevel.warning, + ); + } + + if (removalError != null) { + Error.throwWithStackTrace(removalError, removalStack!); + } } + /// Adds the id to [removed] only once the file is gone, so the cache info of + /// a file that could not be deleted is kept and retried. [started] stops the + /// same object from being removed twice. Future _removeCachedFile( - CacheObject cacheObject, - List toRemove, - ) async { - if (toRemove.contains(cacheObject.id)) return; + CacheObject cacheObject, { + required Set started, + required List removed, + }) async { + final id = cacheObject.id!; + if (!started.add(id)) return; - toRemove.add(cacheObject.id!); if (_memCache.containsKey(cacheObject.key)) { _memCache.remove(cacheObject.key); } @@ -260,6 +317,7 @@ class CacheStore { // File has already been deleted. Do nothing #184 } } + removed.add(id); } bool memoryCacheContainsKey(String key) { diff --git a/flutter_cache_manager/pubspec.yaml b/flutter_cache_manager/pubspec.yaml index 8456c4b5..c3d22cd7 100644 --- a/flutter_cache_manager/pubspec.yaml +++ b/flutter_cache_manager/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_cache_manager description: Generic cache manager for flutter. Saves web files on the storages of the device and saves the cache info using sqflite. -version: 3.4.6 +version: 3.4.7 homepage: https://github.com/Baseflow/flutter_cache_manager topics: - cache diff --git a/flutter_cache_manager/test/cache_store_test.dart b/flutter_cache_manager/test/cache_store_test.dart index 91bdf864..f51a1b79 100644 --- a/flutter_cache_manager/test/cache_store_test.dart +++ b/flutter_cache_manager/test/cache_store_test.dart @@ -6,6 +6,8 @@ import 'package:file/memory.dart'; import 'package:flutter_cache_manager/src/cache_store.dart'; import 'package:flutter_cache_manager/src/logger.dart'; import 'package:flutter_cache_manager/src/storage/cache_object.dart'; +import 'package:flutter_cache_manager/src/storage/file_system/file_system.dart' + as cache_file_system; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; @@ -464,6 +466,196 @@ void main() { ).called(1); }); + test( + 'Store should not remove cache info before the cached file is removed', + () async { + var config = createTestConfig(); + var store = CacheStore(config); + store.cleanupRunMinInterval = const Duration(milliseconds: 1); + var file = await config.returnsFile('testimage.png'); + + var cacheObject = CacheObject( + 'baseflow.com/test.png', + relativePath: 'testimage.png', + id: 1, + validTill: clock.now().add(const Duration(days: 7)), + ); + + when( + config.mockRepo.getObjectsOverCapacity(any), + ).thenAnswer((_) => Future.value([])); + when( + config.mockRepo.getOldObjects(any), + ).thenAnswer((_) => Future.value([cacheObject])); + when( + config.mockRepo.get('baseflow.com/test.png'), + ).thenAnswer((_) => Future.value(cacheObject)); + + expect(await store.getFile('baseflow.com/test.png'), isNotNull); + + // From here on, getting to the file takes a while, like a slow disk. + final fileAvailable = Completer(); + store.fileSystem = _DelayedFileSystem( + store.fileSystem, + fileAvailable.future, + ); + + // Give the cleanup time to run. It must not delete the cache info + // while the file is still on disk. + await Future.delayed(const Duration(milliseconds: 50)); + verifyNever(config.mockRepo.deleteAll(any)); + expect(file.existsSync(), isTrue); + + fileAvailable.complete(); + await untilCalled(config.mockRepo.deleteAll(any)); + + expect(file.existsSync(), isFalse); + verify( + config.mockRepo.deleteAll(argThat(contains(cacheObject.id))), + ).called(1); + }, + ); + + test( + 'Store should keep cache info of a file that cannot be removed', + () async { + final fileSystem = _FailingDeleteFileSystem('broken.png'); + final config = createTestConfig(); + final store = CacheStore(config); + store.cleanupRunMinInterval = const Duration(milliseconds: 1); + store.fileSystem = fileSystem; + await fileSystem.createFileWithData('ok.png'); + await fileSystem.createFileWithData('broken.png'); + + final okObject = CacheObject( + 'baseflow.com/ok.png', + relativePath: 'ok.png', + id: 1, + validTill: clock.now().add(const Duration(days: 7)), + ); + final brokenObject = CacheObject( + 'baseflow.com/broken.png', + relativePath: 'broken.png', + id: 2, + validTill: clock.now().add(const Duration(days: 7)), + ); + + when( + config.mockRepo.getObjectsOverCapacity(any), + ).thenAnswer((_) => Future.value([])); + when( + config.mockRepo.getOldObjects(any), + ).thenAnswer((_) => Future.value([okObject, brokenObject])); + when( + config.mockRepo.get('baseflow.com/ok.png'), + ).thenAnswer((_) => Future.value(okObject)); + + // The failed removal is still reported as a logged cleanup failure. + final logger = setUpTestLogger(); + expect(await store.getFile('baseflow.com/ok.png'), isNotNull); + final log = await logger.waitForLog('Failed to cleanup cache'); + expect(log, contains('Cannot delete')); + verify( + config.mockRepo.deleteAll( + argThat( + allOf(contains(okObject.id), isNot(contains(brokenObject.id))), + ), + ), + ).called(1); + }, + ); + + test( + 'Store should report the file error when deleting the cache info also fails', + () async { + final fileSystem = _FailingDeleteFileSystem('broken.png'); + final config = createTestConfig(); + final store = CacheStore(config); + store.cleanupRunMinInterval = const Duration(milliseconds: 1); + store.fileSystem = fileSystem; + await fileSystem.createFileWithData('ok.png'); + await fileSystem.createFileWithData('broken.png'); + + final okObject = CacheObject( + 'baseflow.com/ok.png', + relativePath: 'ok.png', + id: 1, + validTill: clock.now().add(const Duration(days: 7)), + ); + final brokenObject = CacheObject( + 'baseflow.com/broken.png', + relativePath: 'broken.png', + id: 2, + validTill: clock.now().add(const Duration(days: 7)), + ); + + when( + config.mockRepo.getObjectsOverCapacity(any), + ).thenAnswer((_) => Future.value([])); + when( + config.mockRepo.getOldObjects(any), + ).thenAnswer((_) => Future.value([okObject, brokenObject])); + when( + config.mockRepo.get('baseflow.com/ok.png'), + ).thenAnswer((_) => Future.value(okObject)); + when( + config.mockRepo.deleteAll(any), + ).thenAnswer((_) => Future.error(Exception('database is locked'))); + + final logger = setUpTestLogger(); + expect(await store.getFile('baseflow.com/ok.png'), isNotNull); + + final cleanupLog = await logger.waitForLog('Failed to cleanup cache'); + expect(cleanupLog, contains('Cannot delete')); + final deleteLog = await logger.waitForLog( + 'Failed to delete cache info', + ); + expect(deleteLog, contains('database is locked')); + }, + ); + + test( + 'Store should keep cache info of a file that cannot be removed when emptying cache', + () async { + final fileSystem = _FailingDeleteFileSystem('broken.png'); + final config = createTestConfig(); + final store = CacheStore(config); + store.fileSystem = fileSystem; + await fileSystem.createFileWithData('ok.png'); + await fileSystem.createFileWithData('broken.png'); + + final okObject = CacheObject( + 'baseflow.com/ok.png', + relativePath: 'ok.png', + id: 1, + validTill: clock.now().add(const Duration(days: 7)), + ); + final brokenObject = CacheObject( + 'baseflow.com/broken.png', + relativePath: 'broken.png', + id: 2, + validTill: clock.now().add(const Duration(days: 7)), + ); + + when( + config.mockRepo.getAllObjects(), + ).thenAnswer((_) => Future.value([okObject, brokenObject])); + + await expectLater( + store.emptyCache(), + throwsA(isA()), + ); + + verify( + config.mockRepo.deleteAll( + argThat( + allOf(contains(okObject.id), isNot(contains(brokenObject.id))), + ), + ), + ).called(1); + }, + ); + test('Store should recheck cache info when file is removed', () async { var config = createTestConfig(); var store = CacheStore(config); @@ -624,3 +816,41 @@ TestCacheLogger setUpTestLogger() { addTearDown(() => cacheLogger = originalLogger); return testLogger; } + +/// A file system that only hands out files once [_available] has completed. +class _DelayedFileSystem extends cache_file_system.FileSystem { + _DelayedFileSystem(this._inner, this._available); + + final cache_file_system.FileSystem _inner; + final Future _available; + + @override + Future createFile(String name) async { + await _available; + return _inner.createFile(name); + } +} + +/// A file system in which deleting the file called [undeletableName] fails. +class _FailingDeleteFileSystem extends cache_file_system.FileSystem { + _FailingDeleteFileSystem(String undeletableName) + : _fileSystem = MemoryFileSystem( + opHandle: (path, operation) { + if (operation == FileSystemOp.delete && + path.endsWith(undeletableName)) { + throw FileSystemException('Cannot delete', path); + } + }, + ); + + final MemoryFileSystem _fileSystem; + late final Directory _directory = _fileSystem.systemTempDirectory + .createTempSync('test'); + + @override + Future createFile(String name) async => _directory.childFile(name); + + Future createFileWithData(String name) async { + await (await createFile(name)).writeAsBytes([1, 3]); + } +}