Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
import 'package:dio/dio.dart';
class CacheInterceptor extends Interceptor {
CacheInterceptor();
final _cache = <Uri, Response>{};
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
var response = _cache[options.uri];
if (options.extra['refresh'] == true) {
print('${options.uri}: force refresh, ignore cache! \n');
return handler.next(options);
} else if (response != null) {
print('cache hit: ${options.uri} \n');
return handler.resolve(response);
}
super.onRequest(options, handler);
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
_cache[response.requestOptions.uri] = response;
super.onResponse(response, handler);
}
@override
void onError(DioError err, ErrorInterceptorHandler handler) {
print('onError: $err');
super.onError(err, handler);
}
}
void main() async {
var dio = Dio();
dio.options.baseUrl = 'https://baidu.com';
dio.interceptors
..add(CacheInterceptor())
..add(LogInterceptor(requestHeader: false, responseHeader: false));
await dio.get('/'); // second request
await dio.get('/'); // Will hit cache
// Force refresh
await dio.get('/', options: Options(extra: {'refresh': true}));
}