我的应用程序我正在使用Dio
的Retrofit
子库,在其中我不确定如何为此定义缓存机制,
Retrofit
我在示例代码中找到了这一行:
@GET("")
Future<String> testCustomOptions(@DioOptions() Options options);
现在我如何在这个http
请求上定义缓存?
改造文档:
void main(List<String> args) {
final dio = Dio(); // Provide a dio instance
dio.options.headers["Demo-Header"] = "demo header"; // config your dio headers globally
final client = RestClient(dio);
client.getTasks().then((it) => logger.i(it));
QuickStart
Add a dio-http-cache interceptor in Dio :
dio.interceptors.add(DioCacheManager(CacheConfig(baseUrl: "http://www.google.com")).interceptor);
Set maxAge for a request :
Dio().get(
"http://www.google.com",
options: buildCacheOptions(Duration(days: 7)),
);
我的完整实现代码:
Provider(
create: (_) => MyApis.create(),
),
abstract class MyApis{
factory MyApis(Dio dio, {String baseUrl}) = _MyApis;
@GET("/login")
Future<HttpResponse<PageInformation>> login(@DioOptions() Options options);
static MyApis create() {
final dio = Dio();
dio.options.headers['Content-Type'] = 'application/json';
dio.options.receiveTimeout = 60000;
dio.options.connectTimeout = 120000;
return _MyApis(dio);
}
}