diff --git a/lib/config/cache.dart b/lib/config/cache.dart index c5cd7b9..e84501c 100644 --- a/lib/config/cache.dart +++ b/lib/config/cache.dart @@ -1,8 +1,11 @@ // 是否启用缓存 -const CacheEnable = false; +const CacheEnable = true; // 缓存的最长时间,单位(秒) const CacheMaxAge = 1000; + +const DiskCacheMaxAge = 6000; + // 最大缓存数 -const CacheMaxCount = 100; \ No newline at end of file +const CacheMaxCount = 30; \ No newline at end of file diff --git a/lib/data/services/storage.dart b/lib/data/services/storage.dart index 12a4c20..b6c9731 100644 --- a/lib/data/services/storage.dart +++ b/lib/data/services/storage.dart @@ -13,6 +13,10 @@ class StorageService extends GetxService { return this; } + Future clear() async { + return await _prefs.clear(); + } + Future setString(String key, String value) async { return await _prefs.setString(key, value); } diff --git a/lib/modules/account/account_page.dart b/lib/modules/account/account_page.dart index 6e1871b..82f8643 100644 --- a/lib/modules/account/account_page.dart +++ b/lib/modules/account/account_page.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'package:news_getx/data/services/config.dart'; +import 'package:news_getx/data/services/storage.dart'; import 'package:news_getx/data/services/user.dart'; import 'package:news_getx/modules/widgets/button.dart'; import 'package:news_getx/theme/app_colors.dart'; @@ -188,6 +189,16 @@ class AccountPage extends GetView { ConfigService.to.changeGrayTheme(); }, ), + _buildCell( + title: "Clear disk cache", + hasArrow: true, + onTap: () async { + bool success = await StorageService.to.clear(); + if (success) { + Get.snackbar("提示", "缓存清除完成!"); + } + }, + ), _buildCell( title: "Log out", hasArrow: true, diff --git a/lib/modules/main/main_controller.dart b/lib/modules/main/main_controller.dart index f64545c..123d80e 100644 --- a/lib/modules/main/main_controller.dart +++ b/lib/modules/main/main_controller.dart @@ -19,7 +19,7 @@ class MainController extends GetxController { /// 方法 // 拉取数据 asyncLoadAllData() async { - // todo 因为缓存导致这两项无法刷新 得找个地方刷新或者调整逻辑 例如缓存加超时 + // todo 因为磁盘缓存导致这两项无法刷新 得找个地方刷新或者调整逻辑 例如磁盘缓存加超时 state.categories = await newsRepository.categories(cacheDisk: true); state.channels = await newsRepository.channels(cacheDisk: true); // 分类对应的数据(推荐、新闻) diff --git a/lib/utils/http.dart b/lib/utils/http.dart index f0cc2db..a630589 100644 --- a/lib/utils/http.dart +++ b/lib/utils/http.dart @@ -219,7 +219,7 @@ class HttpUtil { bool refresh = false, bool noCache = !CacheEnable, bool list = false, - String cacheKey = '', + String? cacheKey, bool cacheDisk = false, }) async { Options requestOptions = options ?? Options(); @@ -241,7 +241,7 @@ class HttpUtil { var response = await dio.get( path, queryParameters: queryParameters, - options: options, + options: requestOptions, cancelToken: cancelToken, ); return response.data; diff --git a/lib/utils/net_cache.dart b/lib/utils/net_cache.dart index 1fbbbe6..2b2f8d3 100644 --- a/lib/utils/net_cache.dart +++ b/lib/utils/net_cache.dart @@ -1,4 +1,4 @@ -import 'dart:collection'; +import 'dart:convert'; import 'package:dio/dio.dart'; import 'package:news_getx/config/cache.dart'; @@ -53,7 +53,6 @@ class NetCache extends Interceptor { if (options.extra['noCache'] != true && options.method.toLowerCase() == "get") { String key = options.extra['cacheKey'] ?? options.uri.toString(); - // 策略 1 内存缓存优先,2 然后才是磁盘缓存 var ob = cache[key]; // 1 内存缓存 @@ -63,6 +62,7 @@ class NetCache extends Interceptor { CacheMaxAge) { CacheObject? cacheRes = cache[key]; if (cacheRes != null) { + // print('命中内存缓存: $key'); handler.resolve(cacheRes.response); return; } @@ -75,9 +75,22 @@ class NetCache extends Interceptor { // 2 磁盘缓存 if (cacheDisk) { var cacheData = StorageService.to.getJSON(key); - if (cacheData) { - handler.resolve(cacheData.response); - return; + if (cacheData != null) { + // 若缓存未过期,则返回缓存内容 + if ((DateTime.now().millisecondsSinceEpoch - cacheData['timestamp']) / 1000 < + DiskCacheMaxAge) { + handler.resolve( + Response( + requestOptions: options, + statusCode: 200, + data: cacheData['data'], + ), + ); + return; + } else { + //若已过期则删除缓存,继续向服务器请求 + StorageService.to.remove(key); + } } } } @@ -108,9 +121,13 @@ class NetCache extends Interceptor { // 磁盘缓存 if (options.extra["cacheDisk"] == true) { - StorageService.to.setJSON(key, CacheObject(response)); + final diskData = { + "timestamp": DateTime.now().millisecondsSinceEpoch, + "data": response.data, + }; + StorageService.to.setJSON(key, diskData); } - + // 内存缓存 cache[key] = CacheObject(response); } }