TF-1897 Synchronously handles reopening boxes at one location

(cherry picked from commit e5c0777dd248664d5a1a5bfe728ca1d2379fb2c8)
This commit is contained in:
dab246
2023-06-10 19:06:31 +07:00
committed by Dat Vu
parent fed41f6c2d
commit 87aaeb2cb9
2 changed files with 22 additions and 31 deletions
+2 -2
View File
@@ -116,11 +116,11 @@ class CachingManager {
}
Future<void> _clearSendingEmailCache() async {
final listSendingEmails = await _sendingEmailCacheManager.getAllSendingEmails(needToReopen: true);
final listSendingEmails = await _sendingEmailCacheManager.getAllSendingEmails();
final sendingIds = listSendingEmails.map((sendingEmail) => sendingEmail.sendingId).toSet().toList();
if (sendingIds.isNotEmpty) {
await Future.wait(
sendingIds.map((sendingId) => WorkSchedulerController().cancelByUniqueId(sendingId)),
sendingIds.map(WorkSchedulerController().cancelByUniqueId),
eagerError: true
);
await _sendingEmailCacheManager.clearAllSendingEmails();
@@ -71,25 +71,17 @@ abstract class HiveCacheClient<T> {
});
}
Future<List<T>> getAll({bool needToReopen = false}) {
Future<List<T>> getAll() {
return Future.sync(() async {
if (needToReopen) {
await closeBox();
}
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
final boxItem = encryption ? await openBoxEncryption() : await openBox();
return boxItem.values.toList();
}).catchError((error) {
throw error;
});
}
Future<List<T>> getListByTupleKey(String accountId, String userName, {bool needToReopen = false}) {
Future<List<T>> getListByTupleKey(String accountId, String userName) {
return Future.sync(() async {
if (needToReopen) {
await closeBox();
}
final boxItem = encryption ? await openBoxEncryption() : await openBox();
return boxItem.toMap()
.where((key, value) => _matchedKey(key, accountId, userName))
@@ -100,20 +92,27 @@ abstract class HiveCacheClient<T> {
});
}
Future<List<T>> getValuesByListKey(List<String> listKeys) {
return Future.sync(() async {
final boxItem = encryption ? await openBoxEncryption() : await openBox();
return boxItem.toMap()
.where((key, value) => listKeys.contains(key))
.values
.toList();
}).catchError((error) {
throw error;
});
}
bool _matchedKey(String key, String accountId, String userName) {
final keyDecoded = CacheUtils.decodeKey(key);
final tupleKey = TupleKey.fromString(keyDecoded);
return tupleKey.parts.length >= 3 && tupleKey.parts[1] == accountId && tupleKey.parts[2] == userName;
}
Future<void> updateItem(String key, T newObject, {bool needToReopen = false}) {
Future<void> updateItem(String key, T newObject) {
return Future.sync(() async {
if (needToReopen) {
await closeBox();
}
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
final boxItem = encryption ? await openBoxEncryption() : await openBox();
return boxItem.put(key, newObject);
}).catchError((error) {
throw error;
@@ -122,24 +121,16 @@ abstract class HiveCacheClient<T> {
Future<void> updateMultipleItem(Map<String, T> mapObject) {
return Future.sync(() async {
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
final boxItem = encryption ? await openBoxEncryption() : await openBox();
return boxItem.putAll(mapObject);
}).catchError((error) {
throw error;
});
}
Future<void> deleteItem(String key, {bool needToReopen = false}) {
Future<void> deleteItem(String key) {
return Future.sync(() async {
if (needToReopen) {
await closeBox();
}
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
final boxItem = encryption ? await openBoxEncryption() : await openBox();
return boxItem.delete(key);
}).catchError((error) {
throw error;