缓存逻辑调整
This commit is contained in:
parent
79ebe4c58a
commit
c3a7fbec29
|
@ -1,8 +1,11 @@
|
|||
// 是否启用缓存
|
||||
const CacheEnable = false;
|
||||
const CacheEnable = true;
|
||||
|
||||
// 缓存的最长时间,单位(秒)
|
||||
const CacheMaxAge = 1000;
|
||||
|
||||
|
||||
const DiskCacheMaxAge = 6000;
|
||||
|
||||
// 最大缓存数
|
||||
const CacheMaxCount = 100;
|
||||
const CacheMaxCount = 30;
|
|
@ -13,6 +13,10 @@ class StorageService extends GetxService {
|
|||
return this;
|
||||
}
|
||||
|
||||
Future<bool> clear() async {
|
||||
return await _prefs.clear();
|
||||
}
|
||||
|
||||
Future<bool> setString(String key, String value) async {
|
||||
return await _prefs.setString(key, value);
|
||||
}
|
||||
|
|
|
@ -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<AccountController> {
|
|||
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,
|
||||
|
|
|
@ -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);
|
||||
// 分类对应的数据(推荐、新闻)
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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 = <String, dynamic>{
|
||||
"timestamp": DateTime.now().millisecondsSinceEpoch,
|
||||
"data": response.data,
|
||||
};
|
||||
StorageService.to.setJSON(key, diskData);
|
||||
}
|
||||
|
||||
// 内存缓存
|
||||
cache[key] = CacheObject(response);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue