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
120 stars 70 forks source link

Extend supported methods #140

Open FixThisWorld opened 11 months ago

FixThisWorld commented 11 months ago

Currently, only GET requests seem to be handled and optionally POST methods, if allowPostMethod == true. I suggest replacing allowPostMethod with allowedMethods: Set, so the user can configure which methods should be cached. As HTTP is used e.g. for WebDAV, that set may even include non-standard HTTP methods like PROPFIND, so just hardcoding all HTTP methods seems too restrictive to me. This should be a minor change in the _shouldSkip() function.

mahmoud-othmane commented 11 months ago

In addition to the above, and I'm not sure if the feature already exists, but anyway, it is good if we have an option to skip the caching for a specific route/endpoint. Basically, I need to be able to cache all the GET requests of the app except for a list of defined endpoints. This can be added to _shouldSkip() function as well, so you can accept a list like final List<String> skipForRoutes, and use this list to see which request to skip the caching for. Thank you!

mahmoud-othmane commented 11 months ago

In addition to the above, and I'm not sure if the feature already exists, but anyway, it is good if we have an option to skip the caching for a specific route/endpoint. Basically, I need to be able to cache all the GET requests of the app except for a list of defined endpoints. This can be added to _shouldSkip() function as well, so you can accept a list like final List<String> skipForRoutes, and use this list to see which request to skip the caching for. Thank you!

I might have found a way to go around this, but it seems like hack and not a straightforward solution, but I think it works. Basically, in the extra pass the policy you need for the specific request with a key @cache_options@, which is defined as follows in the package:

  // Key to retrieve options from request
  static const _extraKey = '@cache_options@';

In my app I do this:

   extra: {
        "@cache_options@": currentCacheOptions.copyWith(
          policy: CachePolicy.noCache,
        ),
      },
llfbandit commented 11 months ago

@mahmoud-othmane you can do this explicitly for each route. This option is available like shown in the readme.

response = await dio.get('https://www.foo.com',
  options: options.copyWith(policy: CachePolicy.noCache).toOptions(),
);

This way allows to cache every routes but those you've chosen or you can invert it by specifying CachePolicy.noCache by default.

mahmoud-othmane commented 11 months ago

Oh okay got it. Thank you! @llfbandit