Respect must-revalidate cache-control header
I've been using flutter_cache_manager within my application and, in general, it's been a life saver for me. So please allow me to start by thanking the team for working on this plugin!
That said, I've been troubleshooting some unexpected behavior in my application, and I think I've tracked it down to this line within getSingleFile():
https://github.com/Baseflow/flutter_cache_manager/blob/7ef4334c84bca6d4ab869e7bae7eddf6402a42f0/flutter_cache_manager/lib/src/cache_manager.dart#L86
If I'm reading this correctly, getSingleFile() will prefer to return a file from the cache even if that file has expired per the test on line 83. Is that correct behavior? In my API server, I am returning Cache-Control: max-age=900 with the expectation that user agents will never display content which has expired without re-fetching it first.
Is this a bug or is this expected behavior?
It doesn't look like flutter_cache_manager respects must-revalidate either, so do you have a recommendation on how I could accomplish the behavior I'm looking for?
Thanks again!
For the time being it looks like I can get the behavior I want by extending CacheManager like so:
class MyCustomCacheManager extends CacheManager {
MyCustomCacheManager(Config config) : super(config);
@override
Future<File> getSingleFile(
String url, {
String key,
Map<String, String> headers,
}) async {
key ??= url;
FileInfo cacheFile = await getFileFromCache(key);
if (cacheFile?.validTill == null || cacheFile.validTill.isBefore(DateTime.now())) {
cacheFile = await downloadFile(url, key: key, authHeaders: headers);
}
return cacheFile.file;
}
}
Does this seem like a reasonable approach?
@alexmarkley that's also what's written in the docs:
/// When a file is cached it is return directly, when it is too old the file is
/// downloaded in the background. When a cached file is not available the
/// newly downloaded file is returned.
This is made this way so you get a result as quick as possible. By using getFileStream you get the file from cache and one from the web if the cache is outdated. You could use this stream with a check on the first result as that also contains the validTill property.
The must-revalidate is indeed not used, which sounds like a good idea.