Optimize HiveCacheClient

This commit is contained in:
dab246
2022-10-24 10:47:54 +07:00
committed by Dat H. Pham
parent ae89802845
commit bdbc8b4a11
10 changed files with 135 additions and 1097 deletions
@@ -1,6 +1,4 @@
import 'package:core/core.dart';
import 'package:hive/hive.dart';
import 'package:jmap_dart_client/jmap/mail/mailbox/mailbox.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_client.dart';
import 'package:tmail_ui_user/features/thread/data/model/email_cache.dart';
@@ -10,115 +8,6 @@ class EmailCacheClient extends HiveCacheClient<EmailCache> {
@override
String get tableName => 'EmailCache';
@override
Future<Box<EmailCache>> openBox() async {
if (Hive.isBoxOpen(tableName)) {
return Hive.box<EmailCache>(tableName);
}
return Hive.openBox<EmailCache>(tableName);
}
@override
Future<bool> isExistItem(String key) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.containsKey(key);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteItem(String key) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.delete(key);
}).catchError((error) {
throw error;
});
}
@override
Future<EmailCache?> getItem(String key) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.get(key);
}).catchError((error) {
throw error;
});
}
@override
Future<List<EmailCache>> getAll() {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.values.toList();
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertItem(String key, EmailCache newObject) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> insertMultipleItem(Map<String, EmailCache> mapObject) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
@override
Future<void> updateItem(String key, EmailCache newObject) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.put(key, newObject);
}).catchError((error) {
throw error;
});
}
@override
Future<bool> isExistTable() {
return Future.sync(() async {
return Hive.boxExists(tableName);
}).catchError((error) {
throw error;
});
}
@override
Future<void> deleteMultipleItem(List<String> listKey) {
return Future.sync(() async {
log('EmailCacheClient::deleteMultipleItem(): listKey: ${listKey.length}');
final boxEmail = await openBox();
return boxEmail.deleteAll(listKey);
}).catchError((error) {
log('EmailCacheClient::deleteMultipleItem(): error: $error');
throw error;
});
}
@override
Future<void> updateMultipleItem(Map<String, EmailCache> mapObject) {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
Future<List<EmailCache>> getListEmailCacheByMailboxId(MailboxId mailboxId) {
return Future.sync(() async {
final boxEmail = await openBox();
@@ -131,14 +20,4 @@ class EmailCacheClient extends HiveCacheClient<EmailCache> {
throw error;
});
}
@override
Future<void> clearAllData() {
return Future.sync(() async {
final boxEmail = await openBox();
return boxEmail.clear();
}).catchError((error) {
throw error;
});
}
}