Baseflow / flutter_cache_manager

Generic cache manager for flutter
https://baseflow.com
MIT License
749 stars 438 forks source link

'maxNrOfCacheObjects' can not WORK #349

Closed zkjxx1548 closed 2 years ago

zkjxx1548 commented 2 years ago

💬 'maxNrOfCacheObjects' can not WORK

Related version info:

Dart: 2.10.4 (stable)
cached_network_image: ^2.5.1
flutter_cache_manager: ^2.1.2

Context:

// how we use
CachedNetworkImage(
  imageUrl: imageUrl,
  cacheManager: CacheManager(
    Config(
      'libCachedImageData',
      maxNrOfCacheObjects: 5,
    ),
  ),
);

Result:

It is not working, we find that when the number of currently cached pictures is greater than maxNrOfCacheObjects, the operation will not trigger the cache cleanup mechanism in any case.

We found the reason may be:

The sql statement is redundant with a question mark:

// sql statement
SELECT * FROM cacheObject WHERE touched < ? ORDER BY touched DESC LIMIT 100 OFFSET 5
where the '?' from:
//path: flutter/.pub-cache/hosted/pub.dartlang.org/flutter_cache_manager-2.1.2/lib/src/storage/cache_info_repositories/cache_object_provider.dart
Future<List<CacheObject>> getObjectsOverCapacity(int capacity) async {
  return CacheObject.fromMapList(await db.query(
    _tableCacheObject,
    columns: null,
    orderBy: '${CacheObject.columnTouched} DESC',
    where: '${CacheObject.columnTouched} < ?',
    whereArgs: [
      DateTime.now().subtract(const Duration(days: 1)).millisecondsSinceEpoch
    ],
    limit: 100,
    offset: capacity,
  ));
}

We try to remove theWHERE touched < ? , it can works.

So it is a bug? Or The way we use is wrong

renefloor commented 2 years ago

@zkjxx1548 Not sure if it is good, bad or badly documented (or 2 out of 3), but this is done so files are kept in the cache for at least 1 day. Having a long list and already start deleting items while you scroll might give unexpected behaviour as the app might expect the file to be there. By setting it to at least 1 day this prevents the behaviour of files being removed directly.

The questionmark is set with:

    whereArgs: [
      DateTime.now().subtract(const Duration(days: 1)).millisecondsSinceEpoch
    ],
zkjxx1548 commented 2 years ago

After testing, the cache will be properly cleaned up after one day.

Thanks!

HugoHeneault commented 2 years ago

Hello from 2022. 👋

Having a long list and already start deleting items while you scroll might give unexpected behaviour as the app might expect the file to be there. By setting it to at least 1 day this prevents the behavior of files being removed directly.

This behavior seems a bit weird, is the only way to change this overriding the CacheObjectProvider ? :)