TF-1869 Reopen box when update cache on background

(cherry picked from commit bf2dd9664cc9d319e3ce9d0fe02374cbe7c586f2)
This commit is contained in:
dab246
2023-05-24 09:40:28 +07:00
committed by Dat Vu
parent 36e08e56d2
commit acda93f8e1
2 changed files with 20 additions and 4 deletions
@@ -57,8 +57,11 @@ abstract class HiveCacheClient<T> {
});
}
Future<T?> getItem(String key) {
Future<T?> getItem(String key, {bool needToReopen = false}) {
return Future.sync(() async {
if (needToReopen) {
await closeBox();
}
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
@@ -68,8 +71,11 @@ abstract class HiveCacheClient<T> {
});
}
Future<List<T>> getAll() {
Future<List<T>> getAll({bool needToReopen = false}) {
return Future.sync(() async {
if (needToReopen) {
await closeBox();
}
final boxItem = encryption
? await openBoxEncryption()
: await openBox();
@@ -79,8 +85,11 @@ abstract class HiveCacheClient<T> {
});
}
Future<List<T>> getListByTupleKey(String accountId, String userName) {
Future<List<T>> getListByTupleKey(String accountId, String userName, {bool needToReopen = false}) {
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))
@@ -174,4 +183,11 @@ abstract class HiveCacheClient<T> {
throw error;
});
}
Future<void> closeBox() async {
if (Hive.isBoxOpen(tableName)) {
await Hive.box<T>(tableName).close();
}
return Future.value();
}
}
@@ -99,6 +99,6 @@ class EmailCacheManager {
Future<EmailCache?> getEmailFromCache(AccountId accountId, UserName userName, EmailId emailId) {
final keyCache = TupleKey(emailId.asString, accountId.asString, userName.value).encodeKey;
return _emailCacheClient.getItem(keyCache);
return _emailCacheClient.getItem(keyCache, needToReopen: true);
}
}