dio_304_cache 0.0.4 copy "dio_304_cache: ^0.0.4" to clipboard
dio_304_cache: ^0.0.4 copied to clipboard

A new Flutter plugin for caching http response data and append to 304 response

dio_304_cache

What does this package do? #

  • Save last-modified header and data from http response to app database when making GET request the first time (only work if request method is GET and reponse header has last-modified). (1)
  • Append if-modified-since to request header if making the same GET request described in (1).
  • If reponse status code is 304, get the data from previous saved reponse and put it in data.

This package uses Hive as database resource with LRU cache strategy. If your project uses Hive too, please read below for advoiding conflict configuration.

Quick start: #

  1. include the package to your project as dependency:
dependencies:
  	dio_304_cache: <latest version>
  1. In your main function, call initCache304Interceptor to initialize the database:
import 'package:dio_304_cache/dio_304_cache.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initCache304Interceptor();
  runApp(MyApp());
}
  1. Add Cache304Interceptorto dio instance interceptor:
    final dio = Dio();
    dio.interceptors.add(Cache304Interceptor());

Go to example project for full testing code.

Configuration: #

  • When calling initCache304Interceptor , you can pass a Cache304Config object to adjust following properies:
Property name Type Default Value Description
cacheLength int 25 LRU is used for cache strategy, if saved data length exceeds this number, the one with least used will be removed.
cacheBoxName String dio_304_cache_hive_box the box name used to save data.
hiveInitPath String use path_provider as Hive path init path to save your app data. If your app already called Hive.init(<your_path>), set this property to null to prevent re-initialization. Or you can provide your own path.

example:

  await initCache304Interceptor(config: Cache304Config(cacheLength: 50));

To clear cache, use:

  await Cache304Interceptor.clearCache();
4
likes
40
pub points
0%
popularity

Publisher

unverified uploader

A new Flutter plugin for caching http response data and append to 304 response

Repository (GitHub)
View/report issues

License

BSD-2-Clause (LICENSE)

Dependencies

dio, flutter, hive, path_provider

More

Packages that depend on dio_304_cache