From d08475d5af3ac334fb52d776ce824d5c720a3ccf Mon Sep 17 00:00:00 2001 From: Matthias Rupp Date: Mon, 17 Oct 2022 16:40:51 +0200 Subject: [PATCH] Switch to lazyBox --- mobile/lib/constants/hive_box.dart | 4 ++-- mobile/lib/main.dart | 10 ++++++-- .../album/providers/album.provider.dart | 2 +- .../album/services/album_cache.service.dart | 4 ++-- .../home/services/asset_cache.service.dart | 23 ++++++++----------- .../lib/shared/providers/asset.provider.dart | 2 +- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/mobile/lib/constants/hive_box.dart b/mobile/lib/constants/hive_box.dart index e58ca2caf6..d36cfba48b 100644 --- a/mobile/lib/constants/hive_box.dart +++ b/mobile/lib/constants/hive_box.dart @@ -27,9 +27,9 @@ const String backupRequireWifi = "immichBackupRequireWifi"; // Key 2 const String backupRequireCharging = "immichBackupRequireCharging"; // Key 3 // Asset cache -const String assetListCacheBox = "assetListCacheBox"; +const String assetListCacheBox = "assetListCacheBoxl"; const String assetListCachedAssets = "assetListCachedAssets"; // Album cache -const String albumListCacheBox = "albumListCacheBox"; +const String albumListCacheBox = "albumListCacheBoxl"; const String albumListCachedAssets = "albumListCachedAssets"; diff --git a/mobile/lib/main.dart b/mobile/lib/main.dart index 2b79e4fbae..24b3cd2a79 100644 --- a/mobile/lib/main.dart +++ b/mobile/lib/main.dart @@ -37,8 +37,14 @@ void main() async { await Hive.openBox(hiveBackupInfoBox); await Hive.openBox(hiveGithubReleaseInfoBox); await Hive.openBox(userSettingInfoBox); - await Hive.openBox(assetListCacheBox); - await Hive.openBox(albumListCacheBox); + + final sw = Stopwatch(); + sw.start(); + + await Hive.openLazyBox(assetListCacheBox); + await Hive.openLazyBox(albumListCacheBox); + + debugPrint("Hive box open took ${sw.elapsedMilliseconds} ms"); SystemChrome.setSystemUIOverlayStyle( const SystemUiOverlayStyle( diff --git a/mobile/lib/modules/album/providers/album.provider.dart b/mobile/lib/modules/album/providers/album.provider.dart index 85e3b8cbcd..4973779286 100644 --- a/mobile/lib/modules/album/providers/album.provider.dart +++ b/mobile/lib/modules/album/providers/album.provider.dart @@ -15,7 +15,7 @@ class AlbumNotifier extends StateNotifier> { getAllAlbums() async { if (_albumCacheService.isValid() && state.isEmpty) { - state = await _albumCacheService.getAsync(); + state = await _albumCacheService.get(); } List? albums = diff --git a/mobile/lib/modules/album/services/album_cache.service.dart b/mobile/lib/modules/album/services/album_cache.service.dart index f4e5974afd..856423bd0d 100644 --- a/mobile/lib/modules/album/services/album_cache.service.dart +++ b/mobile/lib/modules/album/services/album_cache.service.dart @@ -15,9 +15,9 @@ class AlbumCacheService extends JsonCache> { } @override - List get() { + Future> get() async { try { - final mapList = readRawData() as List; + final mapList = await readRawData() as List; final responseData = mapList .map((e) => AlbumResponseDto.fromJson(e)) diff --git a/mobile/lib/modules/home/services/asset_cache.service.dart b/mobile/lib/modules/home/services/asset_cache.service.dart index 232ae68bfb..f3f0845c21 100644 --- a/mobile/lib/modules/home/services/asset_cache.service.dart +++ b/mobile/lib/modules/home/services/asset_cache.service.dart @@ -11,12 +11,12 @@ import 'package:openapi/api.dart'; abstract class JsonCache { final String boxName; final String valueKey; - final Box _cacheBox; + final LazyBox _cacheBox; - JsonCache(this.boxName, this.valueKey) : _cacheBox = Hive.box(boxName); + JsonCache(this.boxName, this.valueKey) : _cacheBox = Hive.lazyBox(boxName); bool isValid() { - return _cacheBox.containsKey(valueKey) && _cacheBox.get(valueKey) is String; + return _cacheBox.containsKey(valueKey) && _cacheBox.containsKey(valueKey); } void invalidate() { @@ -28,17 +28,13 @@ abstract class JsonCache { _cacheBox.put(valueKey, jsonString); } - dynamic readRawData() { - return json.decode(_cacheBox.get(valueKey)); + dynamic readRawData() async { + final data = await _cacheBox.get(valueKey); + return json.decode(data); } void put(T data); - - T get(); - - Future getAsync() async { - return Future.microtask(() => get()); - } + Future get(); } class AssetCacheService extends JsonCache> { @@ -50,9 +46,9 @@ class AssetCacheService extends JsonCache> { } @override - List get() { + Future> get() async { try { - final mapList = readRawData() as List; + final mapList = await readRawData() as List; final responseData = mapList .map((e) => AssetResponseDto.fromJson(e)) @@ -66,7 +62,6 @@ class AssetCacheService extends JsonCache> { return []; } } - } final assetCacheServiceProvider = Provider( diff --git a/mobile/lib/shared/providers/asset.provider.dart b/mobile/lib/shared/providers/asset.provider.dart index 572f72c463..589a94a84d 100644 --- a/mobile/lib/shared/providers/asset.provider.dart +++ b/mobile/lib/shared/providers/asset.provider.dart @@ -26,7 +26,7 @@ class AssetNotifier extends StateNotifier> { if (_assetCacheService.isValid() && state.isEmpty) { stopwatch.start(); - state = await _assetCacheService.getAsync(); + state = await _assetCacheService.get(); debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms"); stopwatch.reset(); }