| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import 'dart:convert';
-
- /// 离线缓存服务
- /// 基于 Hive 提供本地数据存储能力
- class CacheService {
- static CacheService? _instance;
- final Map<String, dynamic> _memoryCache = {};
- bool _isInitialized = false;
-
- CacheService._internal();
-
- factory CacheService() {
- _instance ??= CacheService._internal();
- return _instance!;
- }
-
- /// 初始化缓存(打开 Hive Box)
- Future<void> initialize() async {
- if (_isInitialized) return;
-
- try {
- // TODO: 初始化 Hive
- // await Hive.initFlutter();
- // await Hive.openBox(AppConstants.cacheBoxName);
- // await Hive.openBox(AppConstants.userBoxName);
- _isInitialized = true;
- print('[CacheService] 缓存服务已初始化(模拟)');
- } catch (e) {
- print('[CacheService] 初始化失败: $e');
- }
- }
-
- /// 写入缓存
- Future<void> put(String key, dynamic value, {String box = 'default'}) async {
- try {
- // TODO: 使用 Hive 持久化
- // final cacheBox = Hive.box(AppConstants.cacheBoxName);
- // await cacheBox.put(key, value);
- _memoryCache['$box:$key'] = value;
- } catch (e) {
- print('[CacheService] 写入缓存失败: $e');
- }
- }
-
- /// 读取缓存
- Future<T?> get<T>(String key, {String box = 'default'}) async {
- try {
- // TODO: 使用 Hive 读取
- // final cacheBox = Hive.box(AppConstants.cacheBoxName);
- // return cacheBox.get(key) as T?;
- return _memoryCache['$box:$key'] as T?;
- } catch (e) {
- print('[CacheService] 读取缓存失败: $e');
- return null;
- }
- }
-
- /// 删除缓存
- Future<void> delete(String key, {String box = 'default'}) async {
- try {
- _memoryCache.remove('$box:$key');
- } catch (e) {
- print('[CacheService] 删除缓存失败: $e');
- }
- }
-
- /// 清空指定 Box 的所有缓存
- Future<void> clearBox({String box = 'default'}) async {
- try {
- _memoryCache.removeWhere((key, _) => key.startsWith('$box:'));
- } catch (e) {
- print('[CacheService] 清空缓存失败: $e');
- }
- }
-
- /// 清空所有缓存
- Future<void> clearAll() async {
- try {
- _memoryCache.clear();
- } catch (e) {
- print('[CacheService] 清空所有缓存失败: $e');
- }
- }
-
- /// 缓存 JSON 对象
- Future<void> cacheJson(String key, Map<String, dynamic> data, {String box = 'default'}) async {
- await put(key, json.encode(data), box: box);
- }
-
- /// 读取缓存的 JSON 对象
- Future<Map<String, dynamic>?> getCachedJson(String key, {String box = 'default'}) async {
- final data = await get<String>(key, box: box);
- if (data == null) return null;
- try {
- return json.decode(data) as Map<String, dynamic>;
- } catch (e) {
- return null;
- }
- }
-
- /// 缓存列表数据
- Future<void> cacheList(String key, List<Map<String, dynamic>> items, {String box = 'default'}) async {
- await put(key, json.encode(items), box: box);
- }
-
- /// 读取缓存的列表数据
- Future<List<Map<String, dynamic>>?> getCachedList(String key, {String box = 'default'}) async {
- final data = await get<String>(key, box: box);
- if (data == null) return null;
- try {
- final decoded = json.decode(data) as List;
- return decoded.cast<Map<String, dynamic>>();
- } catch (e) {
- return null;
- }
- }
-
- /// 检查是否有缓存
- Future<bool> has(String key, {String box = 'default'}) async {
- return _memoryCache.containsKey('$box:$key');
- }
- }
|