Optimize HiveCacheClient
This commit is contained in:
@@ -8,35 +8,149 @@ abstract class HiveCacheClient<T> {
|
||||
|
||||
String get tableName;
|
||||
|
||||
Future<Box<T>> openBox();
|
||||
bool get encryption => false;
|
||||
|
||||
Future<void> insertItem(String key, T newObject);
|
||||
Future<Uint8List?> _getEncryptionKey() => HiveCacheConfig.getEncryptionKey();
|
||||
|
||||
Future<void> insertMultipleItem(Map<String, T> mapObject);
|
||||
Future<Box<T>> openBox() async {
|
||||
if (Hive.isBoxOpen(tableName)) {
|
||||
return Hive.box<T>(tableName);
|
||||
}
|
||||
return Hive.openBox<T>(tableName);
|
||||
}
|
||||
|
||||
Future<T?> getItem(String key);
|
||||
Future<Box<T>> openBoxEncryption() async {
|
||||
final encryptionKey = await _getEncryptionKey();
|
||||
if (Hive.isBoxOpen(tableName)) {
|
||||
return Hive.box<T>(tableName);
|
||||
} else {
|
||||
return Hive.openBox<T>(
|
||||
tableName,
|
||||
encryptionCipher: encryptionKey != null
|
||||
? HiveAesCipher(encryptionKey)
|
||||
: null);
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<T>> getAll();
|
||||
Future<void> insertItem(String key, T newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> updateItem(String key, T newObject);
|
||||
Future<void> insertMultipleItem(Map<String, T> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> updateMultipleItem(Map<String, T> mapObject);
|
||||
Future<T?> getItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.get(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> deleteItem(String key);
|
||||
Future<List<T>> getAll() {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.values.toList();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> deleteMultipleItem(List<String> listKey);
|
||||
Future<void> updateItem(String key, T newObject) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.put(key, newObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<bool> isExistItem(String key);
|
||||
Future<void> updateMultipleItem(Map<String, T> mapObject) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.putAll(mapObject);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<bool> isExistTable();
|
||||
Future<void> deleteItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.delete(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> deleteMultipleItem(List<String> listKey) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.deleteAll(listKey);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<bool> isExistItem(String key) {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.containsKey(key);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<bool> isExistTable() {
|
||||
return Future.sync(() async {
|
||||
return Hive.boxExists(tableName);
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> deleteBox() {
|
||||
return Hive.deleteBoxFromDisk(tableName);
|
||||
}
|
||||
|
||||
Future<void> clearAllData();
|
||||
|
||||
Future<Uint8List?> getEncryptionKey() {
|
||||
return HiveCacheConfig.getEncryptionKey();
|
||||
Future<void> clearAllData() {
|
||||
return Future.sync(() async {
|
||||
final boxItem = encryption
|
||||
? await openBoxEncryption()
|
||||
: await openBox();
|
||||
return boxItem.clear();
|
||||
}).catchError((error) {
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user