hurshi / dio-http-cache

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

Enhancement: Make cache respect HTTP cache headers #12

Closed cliffordh closed 4 years ago

cliffordh commented 5 years ago

There are multiple cache control headers sent as part of an HTTP response. It would be nice if cache expiration was controlled by those headers by default, and then override using the existing mechanisms.

hurshi commented 5 years ago

Hi, Thanks for your advice. This sounds good, I'll think about it in the next release. Thank you again.

hurshi commented 4 years ago

Hello

It's supported in 0.2.4

Usage

// buildCacheOptions:
options: buildServiceCacheOptions()
// or: set maxAge null or don't set it.
options: buildConfigurableCacheOptions()

Logic

only if maxAge == null, it will try to get maxAge and maxStale from response headers. And if maxStale is setted before, it'll not be overrided by header's value.

slavap commented 4 years ago

@hurshi How to define "reverted" behavior, i.e. always take maxAge and maxStale from response headers as primary, and only if they are not provided by the service then use passed "fallback" maxAge and maxStale? Should CacheConfig.defaultMaxAge and defaultMaxStale be used in such case?

slavap commented 4 years ago

@hurshi Currently defaultMaxAge is not working, because DioCacheManager method has to be fixed:

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);