* feat (Connection Statistics): Restored the server connection statistics feature * perf(store): Optimize data storage performance and implement caching mechanisms - Implement caching mechanisms in SnippetStore and ServerStore to reduce redundant loading - Refactor ConnectionStatsStore to use indexes and optimize query performance - Adopt a more efficient approach when cleaning up expired records - Add a maximum record limit to prevent data bloat * perf(store): Optimize data storage performance and add a caching mechanism Add a caching mechanism to PrivateKeyStore to reduce redundant loading Make the cleanup and index rebuilding of ConnectionStatsStore asynchronous Add database compression and size statistics Display database size in the interface and optimize compression operations * fix (Cache): Fixed cache invalidation and join statistics issues - Added a cache invalidation call to the reload method - Fixed an error in the calculation of join statistics timestamps - Optimized the cache index rebuild logic - Added tooltips and click effects for join statistics * refactor(connection_stats): Convert file operations from synchronous to asynchronous and optimize record cleanup logic Convert the database size retrieval method from synchronous to asynchronous to prevent UI blocking Optimize server record cleanup logic by directly deleting redundant records instead of rebuilding indexes * fix(connection_stats): Fixed an initialization issue when the index database is empty During Stores initialization, the code now checks whether `connectionStats.indexDbKeys` is empty; if so, it calls `rebuildIndexAndCompact` to rebuild and compact the database. Additionally, the implementation of the `_pruneExcessRecords` method has been optimized to use tuples instead of temporary lists, thereby improving performance. A `mounted` check has been added at the UI layer to prevent state update issues during asynchronous operations. * fix(server): Improved error string matching logic to more accurately identify connection issues Error strings are now uniformly converted to lowercase for comparison, and matching criteria have been expanded to cover a wider range of error scenarios, including timeouts, authentication failures, and network errors * fix(PrivateKeyStore): Fixed an issue where the cache state was not updated when clearing the cache When clearing the private key store, ensure that the internal cache state is updated simultaneously to maintain consistency * refactor(store): Add close methods and clean up subscription logic Add close methods to PrivateKeyStore, SnippetStore, and ServerStore to unsubscribe Unify cache cleanup logic to prevent memory leaks * fix(store): Add a cache update suppression mechanism to prevent circular updates Add an _suppressWatch flag to multiple Store classes to suppress cache invalidation during internal operations Add a _putWithoutInvalidatingCache method to prevent recursive watchers from being triggered during data updates * refactor(store): Improve caching and state management using try-finally In PrivateKeyStore, ServerStore, and SnippetStore: 1. Remove redundant close methods 2. Use try-finally to ensure the _suppressWatch state is reset correctly 3. Optimize cache invalidation logic 4. Standardize transaction handling for update operations * refactor(store): Optimize data storage operations and fix potential issues - Ensure the safety and consistency of list operations in ConnectionStatsStore - Replace direct calls to `box.put` with the `set` method in SnippetStore and ServerStore - Extract decoding logic for PrivateKeyStore into a separate method - Add logic to update server-hopping relationships * fix: Fixed an issue where asynchronous operations were not being waited on and optimized storage operations Fixed several issues where asynchronous operations were not being waited on to ensure data consistency Added the _suppressWatch control to ServerStore and PrivateKeyStore Optimized index management in ConnectionStatsStore to maintain record order Added a new GitHub participant * fix: Fixed potential state issues and memory leaks in asynchronous operations Fixed potential state issues that could occur on the server edit page after a delete operation; added a mounted check Changed the statistics clearing operation in connection_stats to run asynchronously Optimized asynchronous operations in PrivateKeyStore and fixed potential memory leaks * refactor(store): Convert asynchronous methods to synchronous ones to simplify the code Fixed an issue where asynchronous operations were not handled correctly on the connection statistics page * fix: Added mounted check and error handling for connection logs Added a mounted check in _ConnectionStatsPageState to prevent the state from being updated after the component is unmounted Added a try-catch block for connection logs in ServerNotifier to catch and log potential storage exceptions
118 lines
2.5 KiB
Dart
118 lines
2.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:fl_lib/fl_lib.dart';
|
|
|
|
import 'package:server_box/data/model/server/snippet.dart';
|
|
|
|
class SnippetStore extends HiveStore {
|
|
SnippetStore._() : super('snippet');
|
|
|
|
static final instance = SnippetStore._();
|
|
|
|
List<Snippet>? _cache;
|
|
StreamSubscription<dynamic>? _boxWatchSub;
|
|
bool _suppressWatch = false;
|
|
|
|
@override
|
|
Future<void> init() async {
|
|
await super.init();
|
|
_boxWatchSub?.cancel();
|
|
_boxWatchSub = box.watch().listen((_) {
|
|
if (!_suppressWatch) {
|
|
_cache = null;
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
bool clear({bool? updateLastUpdateTsOnClear}) {
|
|
_suppressWatch = true;
|
|
try {
|
|
_cache = null;
|
|
return super.clear(updateLastUpdateTsOnClear: updateLastUpdateTsOnClear);
|
|
} finally {
|
|
_suppressWatch = false;
|
|
}
|
|
}
|
|
|
|
void invalidateCache() {
|
|
_cache = null;
|
|
}
|
|
|
|
void put(Snippet snippet) {
|
|
_suppressWatch = true;
|
|
try {
|
|
set(snippet.name, snippet);
|
|
_cache = null;
|
|
} finally {
|
|
_suppressWatch = false;
|
|
}
|
|
}
|
|
|
|
void _putWithoutInvalidatingCache(Snippet snippet) {
|
|
_suppressWatch = true;
|
|
try {
|
|
box.put(snippet.name, snippet);
|
|
} finally {
|
|
_suppressWatch = false;
|
|
}
|
|
}
|
|
|
|
List<Snippet> fetch() {
|
|
return List<Snippet>.from(_cache ??= _loadAll());
|
|
}
|
|
|
|
List<Snippet> _loadAll() {
|
|
final ss = <Snippet>{};
|
|
for (final key in keys()) {
|
|
final s = get<Snippet>(
|
|
key,
|
|
fromObj: (val) {
|
|
if (val is Snippet) return val;
|
|
if (val is Map<dynamic, dynamic>) {
|
|
final map = val.toStrDynMap;
|
|
if (map == null) return null;
|
|
try {
|
|
final snippet = Snippet.fromJson(map as Map<String, dynamic>);
|
|
_putWithoutInvalidatingCache(snippet);
|
|
return snippet;
|
|
} catch (e) {
|
|
dprint('Parsing Snippet from JSON', e);
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
);
|
|
if (s != null) {
|
|
ss.add(s);
|
|
}
|
|
}
|
|
return ss.toList();
|
|
}
|
|
|
|
void delete(Snippet s) {
|
|
_suppressWatch = true;
|
|
try {
|
|
remove(s.name);
|
|
_cache = null;
|
|
} finally {
|
|
_suppressWatch = false;
|
|
}
|
|
}
|
|
|
|
void update(Snippet old, Snippet newInfo) {
|
|
if (!have(old)) {
|
|
throw Exception('Old snippet: $old not found');
|
|
}
|
|
_suppressWatch = true;
|
|
try {
|
|
remove(old.name);
|
|
set(newInfo.name, newInfo);
|
|
_cache = null;
|
|
} finally {
|
|
_suppressWatch = false;
|
|
}
|
|
}
|
|
|
|
bool have(Snippet s) => get(s.name) != null;
|
|
} |