Baseflow / flutter_cache_manager

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

Question about breaking changes #280

Closed febg11 closed 3 years ago

febg11 commented 3 years ago

Hi

I have just updated cache manager to 2.1.1 (was on 1.4.2) and I just want to check there are no other behaviour changes, as you wrote 'expects a Config object with some settings you were used to, but some are slightly different'.

I have created a short cache manager which should only store files for 1 day 3 hours with a max cache amount of 300. My setup for this is below.

class ShortCacheManager  {
  static const key = "shortCustomCacheKey";

  static CacheManager instance = CacheManager(
    Config(
      key,
      stalePeriod: const Duration(days: 1, hours: 3),
      maxNrOfCacheObjects: 300,
      repo: JsonCacheInfoRepository(databaseName: key),
      fileService: ShortCacheHttpFileService(),
    ),
  );
}

class ShortCacheHttpFileService implements FileService {
  http.Client _httpClient;

  @override
  int concurrentFetches = 5;

  ShortCacheHttpFileService({http.Client httpClient}) {
    _httpClient = httpClient ?? http.Client();
  }

  @override
  Future<FileServiceResponse> get(String url, {Map<String, String> headers = const {}}) async {

    print('ACTUAL DOWNLOAD - Short cache manager : $url');

    final req = http.Request('GET', Uri.parse(url));
    req.headers.addAll(headers);

    final httpResponse = await _httpClient.send(req);
    httpResponse.headers.addAll({'cache-control': 'max-age=${Duration(days: 1, hours: 3).inSeconds}'});

    return HttpGetResponse(httpResponse);
  }
}

I think the Config() part is ok but wanted to double check the FileService doesn’t need to be changed. I only got an error asking to implement the concurrent fetches, is everything else alright to leave the same?

Thanks a lot

... Edit

Also could you explain what JsonCacheInfoRepository does?

renefloor commented 3 years ago

Because you did implement you indeed have to implement concurrentFetches, if you do extend you would get the default of 5. The web helper now has a queueing mechanism. Because some servers are not happy when you request many files at the same time you can indicate how many requests you can have simultaneously (the concurrentFetches) and the rest will be queued.

JsonCacheInfoRepository is a way to store information about your cache. The default on mobile and mac was (and is) that it is stored in an sqflite database. However, because on windows and linux sqflite is/was not supported and is actually quite some overhead we added JsonCacheInfoRepository. This stores the data in a simple file as a json string and parses it on load. So perhaps in some time the default on all platforms will be JsonCacheInfoRepository, but for that we have to make migrations.

The setup of your config is fine.