llfbandit / dio_cache_interceptor

Dio HTTP cache interceptor with multiple stores respecting HTTP directives (ETag, Last-Modified, Cache-Control) with options.
https://pub.dev/packages/dio_cache_interceptor
119 stars 70 forks source link

Not caching requests with more than one query parameter #144

Closed edeuss closed 7 months ago

edeuss commented 7 months ago

I have been trying to figure out why some endpoints are caching and why some are not. It seems to me like endpoints it's not caching have more than one query parameter and the ones with one cache correctly. Maybe the & symbol is messing something up since they have the same caching rules.

Package versions used:

  dio_cache_interceptor: ^3.5.0
  dio_cache_interceptor_isar_store: ^1.0.1

Cache options:

_cacheOptions = CacheOptions(
          store: IsarCacheStore(value.path, name: 'statsfm_sdk_cache'),
          policy: CachePolicy.request,
          hitCacheOnErrorExcept: [400, 401, 403, 500, 526],
          maxStale: const Duration(hours: 2),
          priority: CachePriority.normal,
          allowPostMethod: false,
        );
edeuss commented 7 months ago

Doesn't seem to be related to the & symbol but that the package is for some reason flipping the query parameter locations so it comes up as a different cache key.

edeuss commented 7 months ago

Turns out to be an issue with Dio on onResponse returning the query parameter's out of order.

Going to open up an issue with Dio, but for a temp fix, if anyone else is having the same issue you can do:

keyBuilder: (RequestOptions request) {
            Map<String, List<String>> queryParams =
                request.uri.queryParametersAll;

            // Sort the query parameters alphabetically
            var sortedParams =
                SplayTreeMap<String, List<String>>.from(queryParams);

            // Reconstruct the URI with sorted query parameters
            Uri sortedUri = request.uri.replace(queryParameters: sortedParams);
            return sortedUri.toString();
          },