智慧水务管理系统 - 精河县供水工程综合管理平台

cache_service.dart 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import 'dart:convert';
  2. /// 离线缓存服务
  3. /// 基于 Hive 提供本地数据存储能力
  4. class CacheService {
  5. static CacheService? _instance;
  6. final Map<String, dynamic> _memoryCache = {};
  7. bool _isInitialized = false;
  8. CacheService._internal();
  9. factory CacheService() {
  10. _instance ??= CacheService._internal();
  11. return _instance!;
  12. }
  13. /// 初始化缓存(打开 Hive Box)
  14. Future<void> initialize() async {
  15. if (_isInitialized) return;
  16. try {
  17. // TODO: 初始化 Hive
  18. // await Hive.initFlutter();
  19. // await Hive.openBox(AppConstants.cacheBoxName);
  20. // await Hive.openBox(AppConstants.userBoxName);
  21. _isInitialized = true;
  22. print('[CacheService] 缓存服务已初始化(模拟)');
  23. } catch (e) {
  24. print('[CacheService] 初始化失败: $e');
  25. }
  26. }
  27. /// 写入缓存
  28. Future<void> put(String key, dynamic value, {String box = 'default'}) async {
  29. try {
  30. // TODO: 使用 Hive 持久化
  31. // final cacheBox = Hive.box(AppConstants.cacheBoxName);
  32. // await cacheBox.put(key, value);
  33. _memoryCache['$box:$key'] = value;
  34. } catch (e) {
  35. print('[CacheService] 写入缓存失败: $e');
  36. }
  37. }
  38. /// 读取缓存
  39. Future<T?> get<T>(String key, {String box = 'default'}) async {
  40. try {
  41. // TODO: 使用 Hive 读取
  42. // final cacheBox = Hive.box(AppConstants.cacheBoxName);
  43. // return cacheBox.get(key) as T?;
  44. return _memoryCache['$box:$key'] as T?;
  45. } catch (e) {
  46. print('[CacheService] 读取缓存失败: $e');
  47. return null;
  48. }
  49. }
  50. /// 删除缓存
  51. Future<void> delete(String key, {String box = 'default'}) async {
  52. try {
  53. _memoryCache.remove('$box:$key');
  54. } catch (e) {
  55. print('[CacheService] 删除缓存失败: $e');
  56. }
  57. }
  58. /// 清空指定 Box 的所有缓存
  59. Future<void> clearBox({String box = 'default'}) async {
  60. try {
  61. _memoryCache.removeWhere((key, _) => key.startsWith('$box:'));
  62. } catch (e) {
  63. print('[CacheService] 清空缓存失败: $e');
  64. }
  65. }
  66. /// 清空所有缓存
  67. Future<void> clearAll() async {
  68. try {
  69. _memoryCache.clear();
  70. } catch (e) {
  71. print('[CacheService] 清空所有缓存失败: $e');
  72. }
  73. }
  74. /// 缓存 JSON 对象
  75. Future<void> cacheJson(String key, Map<String, dynamic> data, {String box = 'default'}) async {
  76. await put(key, json.encode(data), box: box);
  77. }
  78. /// 读取缓存的 JSON 对象
  79. Future<Map<String, dynamic>?> getCachedJson(String key, {String box = 'default'}) async {
  80. final data = await get<String>(key, box: box);
  81. if (data == null) return null;
  82. try {
  83. return json.decode(data) as Map<String, dynamic>;
  84. } catch (e) {
  85. return null;
  86. }
  87. }
  88. /// 缓存列表数据
  89. Future<void> cacheList(String key, List<Map<String, dynamic>> items, {String box = 'default'}) async {
  90. await put(key, json.encode(items), box: box);
  91. }
  92. /// 读取缓存的列表数据
  93. Future<List<Map<String, dynamic>>?> getCachedList(String key, {String box = 'default'}) async {
  94. final data = await get<String>(key, box: box);
  95. if (data == null) return null;
  96. try {
  97. final decoded = json.decode(data) as List;
  98. return decoded.cast<Map<String, dynamic>>();
  99. } catch (e) {
  100. return null;
  101. }
  102. }
  103. /// 检查是否有缓存
  104. Future<bool> has(String key, {String box = 'default'}) async {
  105. return _memoryCache.containsKey('$box:$key');
  106. }
  107. }