hurshi / dio-http-cache

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

Clear cache based on resource not key #6

Closed yevyevyev closed 5 years ago

yevyevyev commented 5 years ago

Hello, @hurshi I'm using this package and it's awesome! What do you think, right now we can delete the resource via key and subkey but if we want to delete an entire resource what to do? E.g, I have a https://resource.com?page=1 and 100 pages of it but I need to refresh the whole resource, not only a particular page. From my perspective, I see two solutions: add key as the path and subkey as the query by default or add a wildcard delete that will take only the URL path into consideration. Have a nice day ✨

hurshi commented 5 years ago

Hello, thank you very much for your advice. As what you suggested, I have modified the implementation of deleting cache. By default, host+path is used as primaryKey and query as subKey.

You can see details in readme and if you find any omission or have a better option, please feel free to correct me.

You're making dio-http-cache better and better, Thank you and have a nice day!

How to delete caches

  1. No matter what subKey is, delete local cache if primary matched.

      // Automatically parses primarykey from path
      _dioCacheManager.deleteByPrimaryKey(path); 
  2. Delete local cache when both primaryKey and subKey matched.

      // delete local cache when both primaryKey and subKey matched.
      _dioCacheManager.deleteByPrimaryKeyAndSubKey(path); 

    INPORTANT: If you have additional parameters when requesting the http interface, you must take them with it, for example:

      _dio.get(_url, queryParameters: {'k': keyword}, 
        options: buildCacheOptions(Duration(hours: 1)))
      //delete the cache:
      _dioCacheManager.deleteByPrimaryKeyAndSubKey(path, queryParameters:{'k': keyword}); 
      _dio.post(_url, data: {'k': keyword}, 
        options: buildCacheOptions(Duration(hours: 1)))
      //delete the cache:
      _dioCacheManager.deleteByPrimaryKeyAndSubKey(path, data:{'k': keyword}); 
  3. Delete local cache by primaryKey and optional subKey if you know your primarykey and subkey exactly.

      // delete local cache by primaryKey and optional subKey
      _dioCacheManager.delete(primaryKey,{subKey});
yevyevyev commented 5 years ago

Wow, that's very cool! thanks 🙏