atefsawaed
atefsawaed

Reputation: 601

Maximum limit of cache size in flutter

I am using Firestore as a database and cached_network_image to load and cache images in my flutter app (iOS & Android). I noticed that the app cache size gets too big (+300 mb) after running the app for a while (in debug mode). Is there a maximum limit on the cache size that app uses in flutter? Is there a way to force some limit on the cache size such that whenever the cache size reaches its maximum limit, oldest cached files will be removed?

Upvotes: 5

Views: 6901

Answers (3)

akash maurya
akash maurya

Reputation: 656

Image cache can cache up to 1000 images, and up to 100 MB. This may be more but it is min 100 MB.

Upvotes: 1

Siddharth jha
Siddharth jha

Reputation: 618

For your use case use extended_image for caching the image and clear the cache using clearDiskCachedImages method

// Clear the disk cache directory then return if it succeed.
///  <param name="duration">timespan to compute whether file has expired or not</param>
Future<bool> clearDiskCachedImages({Duration duration})

Upvotes: 0

griffins
griffins

Reputation: 8256

cached_network_image relies on flutter_cache_manager

A CacheManager to download and cache files in the cache directory of the app. Various settings on how long to keep a file can be changed.

How it works

By default the cached files are stored in the temporary directory of the app. This means the OS can delete the files any time.

Information about the files is stored in a database using sqflite. The file name of the database is the key of the cacheManager, that's why that has to be unique.

This cache information contains the end date till when the file is valid and the eTag to use with the http cache-control.

methods

removeFile removes a file from the cache.

emptyCache removes all files from the cache.

example

void _clearCache() {
    DefaultCacheManager().emptyCache();
    
  }

if you want to be able to delete images after sometime you will have to implement a custom cache that deletes images after a given no of days.

from docs TL:DR

class CustomCacheManager extends BaseCacheManager {
  static const key = "customCache";

  static CustomCacheManager _instance;

  factory CustomCacheManager() {
    if (_instance == null) {
      _instance = new CustomCacheManager._();
    }
    return _instance;
  }

  CustomCacheManager._() : super(key,
      maxAgeCacheObject: Duration(days: 7),
      maxNrOfCacheObjects: 20);

  Future<String> getFilePath() async {
    var directory = await getTemporaryDirectory();
    return p.join(directory.path, key);
  }

Upvotes: 3

Related Questions