Skip to content

Commit 2e9604a

Browse files
committed
Fix race condition of expired cache files
While cache expired, the original implementation returns older cache information without waiting for possible new-downloaded file or modification check. In situations where ETag has changed, updated file will be downloaded using a new file name and old cache file be deleted (according to `WebHelper._manageResponse()` and `WebHelper._setDataFromHeaders()`). If old cache file is returned immediately as in the original implementation, in cases where the old cache file is still consuming, it'll be very likely deleted by web helper routine. Thus downloading-accessing race condition arise. Here `CacheManager.getSingleFile()` was refactored to clarify two situations. - Cache exist and not expired: return the cache. - Otherwise: just call `downloadFile()` and wait. Class `WebHelper` will do the rest for us smartly (Using `If-None-Match` HTTP request header).
1 parent 30de944 commit 2e9604a

File tree

1 file changed

+1
-4
lines changed

1 file changed

+1
-4
lines changed

flutter_cache_manager/lib/src/cache_manager.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ class CacheManager implements BaseCacheManager {
7878
}) async {
7979
key ??= url;
8080
final cacheFile = await getFileFromCache(key);
81-
if (cacheFile != null) {
82-
if (cacheFile.validTill.isBefore(DateTime.now())) {
83-
unawaited(downloadFile(url, key: key, authHeaders: headers));
84-
}
81+
if (cacheFile != null && cacheFile.validTill.isAfter(DateTime.now())) {
8582
return cacheFile.file;
8683
}
8784
return (await downloadFile(url, key: key, authHeaders: headers)).file;

0 commit comments

Comments
 (0)