Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions flutter_cache_manager/lib/src/cache_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ class CacheStore {

Future<void> _removeCachedFile(
CacheObject cacheObject, List<int> toRemove) async {
if (!toRemove.contains(cacheObject.id)) {
toRemove.add(cacheObject.id);
if (_memCache.containsKey(cacheObject.url)) {
_memCache.remove(cacheObject.url);
}
if (_futureCache.containsKey(cacheObject.url)) {
unawaited(_futureCache.remove(cacheObject.url));
}
if (toRemove.contains(cacheObject.id)) return;

toRemove.add(cacheObject.id);
if (_memCache.containsKey(cacheObject.url)) {
_memCache.remove(cacheObject.url);
}
if (_futureCache.containsKey(cacheObject.url)) {
unawaited(_futureCache.remove(cacheObject.url));
}
final file = (await fileDir).childFile(cacheObject.relativePath);
if (await file.exists()) {
Expand Down
31 changes: 31 additions & 0 deletions flutter_cache_manager/test/cache_store_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,37 @@ void main() {
verify(repo.deleteAll(argThat(contains(cacheObject.id)))).called(1);
});



test('Store should remove file old and over capacity', () async {
var repo = MockRepo();
var directory = createDir();

var store = CacheStore(directory, 'test', 2, const Duration(days: 7),
cacheRepoProvider: Future.value(repo),
cleanupRunMinInterval: const Duration());

var cacheObject = CacheObject('baseflow.com/test.png',
relativePath: 'testimage.png', id: 1);
var file = await (await directory).childFile('testimage.png').create();

when(repo.getObjectsOverCapacity(any))
.thenAnswer((_) => Future.value([cacheObject]));
when(repo.getOldObjects(any))
.thenAnswer((_) => Future.value([cacheObject]));
when(repo.get('baseflow.com/test.png'))
.thenAnswer((_) => Future.value(cacheObject));

expect(await store.getFile('baseflow.com/test.png'), isNotNull);

await untilCalled(repo.deleteAll(any));
await Future.delayed(const Duration(milliseconds: 5));

verify(repo.getObjectsOverCapacity(any)).called(1);
verify(repo.getOldObjects(any)).called(1);
verify(repo.deleteAll(argThat(contains(cacheObject.id)))).called(1);
});

test('Store should not remove files that are not old or over capacity',
() async {
var repo = MockRepo();
Expand Down