I have been trying to add a cache for images in flutter, but I haven't been able to set the cache duration.
I have tried using the cached_network_image package, where I have a custom cache manager where I overwrite the cache duration to 2 minutes (instead of having it as the default, which is 30 days), but the images are being cached for more then 2 minutes, it is even still being cached 1 day later).
When I simply use the Image.network
component, the images are not being cached.
My custom cache manager:
class ImageCacheManager extends BaseCacheManager {
static const key = "libCachedImageData";
static ImageCacheManager _instance;
factory ImageCacheManager() {
if (_instance == null) {
_instance = new ImageCacheManager._();
}
return _instance;
}
ImageCacheManager._() : super(key, maxAgeCacheObject: Duration(minutes: 2));
Future<String> getFilePath() async {
var directory = await getTemporaryDirectory();
return p.join(directory.path, key);
}
}
expected: the images will not be cached longer than I specify (e.g. 2 minutes)
actual: the images are cached for longer time than I specify (at least 36 hours)