- Save
last-modified
header anddata
from http response to app database when makingGET
request the first time (only work if request method isGET
and reponse header haslast-modified
). (1) - Append
if-modified-since
to request header if making the sameGET
request described in (1). - If reponse status code is
304
, get the data from previous saved reponse and put it indata
.
This package uses Hive as database resource with LRU cache strategy. If your project uses Hive too, please read below for advoiding conflict configuration.
- include the package to your project as dependency:
dependencies:
dio_304_cache: <latest version>
- 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());
}
- Add
Cache304Interceptor
to dio instance interceptor:
final dio = Dio();
dio.interceptors.add(Cache304Interceptor());
Go to example project for full testing code.
- When calling
initCache304Interceptor
, you can pass aCache304Config
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();