hurshi / dio-http-cache

http cache lib for Flutter dio like RxCache
Apache License 2.0
274 stars 223 forks source link

CacheConfig.defaultMaxAge is ignored by DioCacheManager #30

Closed slavap closed 4 years ago

slavap commented 4 years ago

Example:

rest.interceptors.add(
      DioCacheManager(
        CacheConfig(
          baseUrl: rest.options.baseUrl,
          defaultMaxAge: Duration(minutes: 5),
        ),
      ).interceptor,
    );

defaultMaxAge is NOT used in case server does not return any max-age in response. The bug is probably in:

Future<bool> _pushToCache(Response response) {
    RequestOptions options = response.request;
    Duration maxAge = options.extra[DIO_CACHE_KEY_MAX_AGE];
    Duration maxStale = options.extra[DIO_CACHE_KEY_MAX_STALE];
    if (null == maxAge) {
      _tryParseHead(response, maxStale, (_maxAge, _maxStale) {
        maxAge = _maxAge;
        maxStale = _maxStale;
      });
    }

    // FIX
    if (null == maxAge) {
        maxAge = _manager?._config.defaultMaxAge;
        maxStale = _manager?._config.defaultMaxStale;
    }
    // End of FIX

    if (null == maxAge) return Future.value(false);

See also comments in https://github.com/hurshi/dio-http-cache/issues/12

hurshi commented 4 years ago

Hello, thanks for your feedback. Yes, it's a bug. and it has fixed in new version: 0.2.6