1
0
forked from Cutlery/immich

Switch to lazyBox

This commit is contained in:
Matthias Rupp 2022-10-17 16:40:51 +02:00
parent d310c77fc8
commit d08475d5af
6 changed files with 23 additions and 22 deletions

View File

@ -27,9 +27,9 @@ const String backupRequireWifi = "immichBackupRequireWifi"; // Key 2
const String backupRequireCharging = "immichBackupRequireCharging"; // Key 3 const String backupRequireCharging = "immichBackupRequireCharging"; // Key 3
// Asset cache // Asset cache
const String assetListCacheBox = "assetListCacheBox"; const String assetListCacheBox = "assetListCacheBoxl";
const String assetListCachedAssets = "assetListCachedAssets"; const String assetListCachedAssets = "assetListCachedAssets";
// Album cache // Album cache
const String albumListCacheBox = "albumListCacheBox"; const String albumListCacheBox = "albumListCacheBoxl";
const String albumListCachedAssets = "albumListCachedAssets"; const String albumListCachedAssets = "albumListCachedAssets";

View File

@ -37,8 +37,14 @@ void main() async {
await Hive.openBox<HiveBackupAlbums>(hiveBackupInfoBox); await Hive.openBox<HiveBackupAlbums>(hiveBackupInfoBox);
await Hive.openBox(hiveGithubReleaseInfoBox); await Hive.openBox(hiveGithubReleaseInfoBox);
await Hive.openBox(userSettingInfoBox); 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( SystemChrome.setSystemUIOverlayStyle(
const SystemUiOverlayStyle( const SystemUiOverlayStyle(

View File

@ -15,7 +15,7 @@ class AlbumNotifier extends StateNotifier<List<AlbumResponseDto>> {
getAllAlbums() async { getAllAlbums() async {
if (_albumCacheService.isValid() && state.isEmpty) { if (_albumCacheService.isValid() && state.isEmpty) {
state = await _albumCacheService.getAsync(); state = await _albumCacheService.get();
} }
List<AlbumResponseDto>? albums = List<AlbumResponseDto>? albums =

View File

@ -15,9 +15,9 @@ class AlbumCacheService extends JsonCache<List<AlbumResponseDto>> {
} }
@override @override
List<AlbumResponseDto> get() { Future<List<AlbumResponseDto>> get() async {
try { try {
final mapList = readRawData() as List<dynamic>; final mapList = await readRawData() as List<dynamic>;
final responseData = mapList final responseData = mapList
.map((e) => AlbumResponseDto.fromJson(e)) .map((e) => AlbumResponseDto.fromJson(e))

View File

@ -11,12 +11,12 @@ import 'package:openapi/api.dart';
abstract class JsonCache<T> { abstract class JsonCache<T> {
final String boxName; final String boxName;
final String valueKey; 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() { bool isValid() {
return _cacheBox.containsKey(valueKey) && _cacheBox.get(valueKey) is String; return _cacheBox.containsKey(valueKey) && _cacheBox.containsKey(valueKey);
} }
void invalidate() { void invalidate() {
@ -28,17 +28,13 @@ abstract class JsonCache<T> {
_cacheBox.put(valueKey, jsonString); _cacheBox.put(valueKey, jsonString);
} }
dynamic readRawData() { dynamic readRawData() async {
return json.decode(_cacheBox.get(valueKey)); final data = await _cacheBox.get(valueKey);
return json.decode(data);
} }
void put(T data); void put(T data);
Future<T> get();
T get();
Future<T> getAsync() async {
return Future.microtask(() => get());
}
} }
class AssetCacheService extends JsonCache<List<AssetResponseDto>> { class AssetCacheService extends JsonCache<List<AssetResponseDto>> {
@ -50,9 +46,9 @@ class AssetCacheService extends JsonCache<List<AssetResponseDto>> {
} }
@override @override
List<AssetResponseDto> get() { Future<List<AssetResponseDto>> get() async {
try { try {
final mapList = readRawData() as List<dynamic>; final mapList = await readRawData() as List<dynamic>;
final responseData = mapList final responseData = mapList
.map((e) => AssetResponseDto.fromJson(e)) .map((e) => AssetResponseDto.fromJson(e))
@ -66,7 +62,6 @@ class AssetCacheService extends JsonCache<List<AssetResponseDto>> {
return []; return [];
} }
} }
} }
final assetCacheServiceProvider = Provider( final assetCacheServiceProvider = Provider(

View File

@ -26,7 +26,7 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
if (_assetCacheService.isValid() && state.isEmpty) { if (_assetCacheService.isValid() && state.isEmpty) {
stopwatch.start(); stopwatch.start();
state = await _assetCacheService.getAsync(); state = await _assetCacheService.get();
debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms"); debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms");
stopwatch.reset(); stopwatch.reset();
} }