缓存逻辑调整

This commit is contained in:
胡天 2023-07-25 11:46:08 +08:00
parent 79ebe4c58a
commit c3a7fbec29
6 changed files with 47 additions and 12 deletions

View File

@ -1,8 +1,11 @@
//
const CacheEnable = false;
const CacheEnable = true;
//
const CacheMaxAge = 1000;
const DiskCacheMaxAge = 6000;
//
const CacheMaxCount = 100;
const CacheMaxCount = 30;

View File

@ -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);
}

View File

@ -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,

View File

@ -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);
//

View File

@ -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;

View File

@ -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);
}
}