Use TupleKey(ObjectKey-AccountId-UserName) to store data to hive

(cherry picked from commit e0ace535d5f3af71eb13598d92524caf2c6d9bbf)
This commit is contained in:
dab246
2023-04-27 11:03:39 +07:00
committed by Dat Vu
parent 7d0a5729b2
commit 876cc368b0
107 changed files with 562 additions and 340 deletions
@@ -1,7 +1,7 @@
import 'dart:typed_data';
import 'package:core/core.dart';
import 'package:core/presentation/extensions/map_extensions.dart';
import 'package:hive/hive.dart';
import 'package:tmail_ui_user/features/caching/config/hive_cache_config.dart';
import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart';
@@ -79,14 +79,11 @@ abstract class HiveCacheClient<T> {
});
}
Future<List<T>> getListByCollectionId(String collectionId) {
Future<List<T>> getListByTupleKey(String accountId, String userName) {
return Future.sync(() async {
final boxItem = encryption ? await openBoxEncryption() : await openBox();
return boxItem.toMap()
.where((key, value) {
final tupleKey = TupleKey.fromString(key as String);
return tupleKey.parts.length >= 2 && tupleKey.parts[1] == collectionId;
})
.where((key, value) => _matchedKey(key, accountId, userName))
.values
.toList();
}).catchError((error) {
@@ -94,6 +91,12 @@ abstract class HiveCacheClient<T> {
});
}
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) {
return Future.sync(() async {
final boxItem = encryption
@@ -1,4 +1,22 @@
import 'dart:convert';
import 'package:core/utils/app_logger.dart';
class CacheUtils {
static String encodeKey(String key) {
final keyEncoded = base64.encode(utf8.encode(key));
log('CacheUtils::encodeKey():keyHashed: $keyEncoded');
return keyEncoded;
}
static String decodeKey(String keyEncoded) {
final keyDecoded = utf8.decode(base64.decode(keyEncoded));
log('CacheUtils::decodeKey():keyDecoded: $keyDecoded');
return keyDecoded;
}
}
class TupleKey {
final List<String> parts;
@@ -26,4 +44,6 @@ class TupleKey {
@override
int get hashCode => Object.hashAll(parts);
String get encodeKey => CacheUtils.encodeKey(toString());
}