Baseflow / flutter_cache_manager

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

removeFile not working as expected #352

Closed Zelfapp closed 2 years ago

Zelfapp commented 2 years ago

Testing on Android 21, target SDK 31.

clearCache works just fine. Physical files are deleted from cache. However, removeFile never removes a file from the cache.

My expectation was that calling removeFile and passing the key that was used when caching the file would delete the file reference in the database and also the physical file in the cache, just like clearCache, but for a single file. Is this not correct?

I've had to resort to deleting the file using dart's io rather than trying to rely on removeFile. Here's my code. What am I missing?

static const key = 'myAppCacheKey';

static CacheManager instance = CacheManager(
  Config(
    key,
    stalePeriod: const Duration(days: 3),
    maxNrOfCacheObjects: 20,
    repo: JsonCacheInfoRepository(databaseName: key),
  ),
);

static Future<void> putFileInCache({
  required String url,
  required String key,
  required String eTag,
  required String fileExtension,
}) async {
  await instance.putFile(
    url,
    Uint8List(16),
    key: key,
    eTag: eTag,
    fileExtension: fileExtension,
    maxAge: const Duration(days: 3),
  );
}

static Future<void> removeFile({
  required String path,
  required String key,
}) async {
  await instance.removeFile(key).then((value) {
    // This prints successfully that the key has been deleted, no error, but the file still exists.
    debugPrint('deleted key: $key');
  }).onError((error, stackTrace) {
    debugPrint(error.toString());
  });

  // Deleting file, bc removeFile does not.
  final File file = File(path);
  final bool exists = await file.exists();

  if (exists) {
    file.delete();
  }
}

static Future<void> clearCache() async {
  await instance.emptyCache();
}
sidrao2006 commented 2 years ago

Hey @Zelfapp Would you mind sharing the version of the package you're using? A change was made in https://github.com/Baseflow/flutter_cache_manager/pull/323 and landed in v3.1.2 which awaits file removal. All prior versions complete before the file is actually removed and so, the file removal happens in the 'background'.

no-response[bot] commented 2 years ago

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.