Add local memcache for add/get individual connections

merge-requests/67/merge
Hank Grabowski 2023-01-18 12:12:53 -05:00
rodzic dfef64d748
commit 2a3c2296a1
1 zmienionych plików z 18 dodań i 3 usunięć

Wyświetl plik

@ -5,10 +5,12 @@ import '../../models/connection.dart';
import '../../models/exec_error.dart';
import '../../objectbox.g.dart';
import '../interfaces/connections_repo_intf.dart';
import '../memory/memory_connections_repo.dart';
import 'objectbox_cache.dart';
class ObjectBoxConnectionsRepo implements IConnectionsRepo {
late final Box<Connection> box;
final memCache = MemoryConnectionsRepo();
ObjectBoxConnectionsRepo() {
box = getIt<ObjectBoxCache>().store.box<Connection>();
@ -16,18 +18,25 @@ class ObjectBoxConnectionsRepo implements IConnectionsRepo {
@override
bool addAllConnections(Iterable<Connection> newConnections) {
memCache.addAllConnections(newConnections);
final result = box.putMany(newConnections.toList());
return result.length == newConnections.length;
}
@override
bool addConnection(Connection connection) {
memCache.addConnection(connection);
box.putAsync(connection);
return true;
}
@override
Result<Connection, ExecError> getById(String id) {
final result = memCache.getById(id);
if (result.isSuccess) {
return result;
}
return _getConnection(
Connection_.id.equals(id),
).mapError(
@ -39,6 +48,10 @@ class ObjectBoxConnectionsRepo implements IConnectionsRepo {
@override
Result<Connection, ExecError> getByName(String name) {
final result = memCache.getByName(name);
if (result.isSuccess) {
return result;
}
return _getConnection(
Connection_.name.equals(name),
).mapError(
@ -74,16 +87,18 @@ class ObjectBoxConnectionsRepo implements IConnectionsRepo {
@override
bool updateConnection(Connection connection) {
memCache.updateConnection(connection);
box.put(connection);
return true;
}
Result<Connection, ExecError> _getConnection(Condition<Connection> query) {
final result = box.query(query).build().findFirst();
if (result == null) {
final connection = box.query(query).build().findFirst();
if (connection == null) {
return buildErrorResult(type: ErrorType.notFound);
}
return Result.ok(result);
memCache.addConnection(connection);
return Result.ok(connection);
}
}