From 75d8ca13063946bed4fbb6823ae96cd2d1b9162f Mon Sep 17 00:00:00 2001 From: Matthias Rupp Date: Sun, 16 Oct 2022 09:50:31 +0200 Subject: [PATCH] Invalidation on logout and timing measurements --- .../modules/home/services/asset_cache.service.dart | 4 ++++ .../login/providers/authentication.provider.dart | 6 +++++- mobile/lib/shared/providers/asset.provider.dart | 13 +++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/mobile/lib/modules/home/services/asset_cache.service.dart b/mobile/lib/modules/home/services/asset_cache.service.dart index 068acbafb9..f5250f38e7 100644 --- a/mobile/lib/modules/home/services/asset_cache.service.dart +++ b/mobile/lib/modules/home/services/asset_cache.service.dart @@ -19,6 +19,10 @@ class AssetCacheService { _cacheBox.get(assetListCachedAssets) is String; } + void invalidate() { + _cacheBox.clear(); + } + void putAssets(List assets) { final mapList = assets.map((e) => e.toJson()).toList(); final jsonString = json.encode(mapList); diff --git a/mobile/lib/modules/login/providers/authentication.provider.dart b/mobile/lib/modules/login/providers/authentication.provider.dart index 2ccf616a7b..f86a9735fc 100644 --- a/mobile/lib/modules/login/providers/authentication.provider.dart +++ b/mobile/lib/modules/login/providers/authentication.provider.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:hive/hive.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:immich_mobile/constants/hive_box.dart'; +import 'package:immich_mobile/modules/home/services/asset_cache.service.dart'; import 'package:immich_mobile/modules/login/models/authentication_state.model.dart'; import 'package:immich_mobile/modules/login/models/hive_saved_login_info.model.dart'; import 'package:immich_mobile/modules/backup/services/backup.service.dart'; @@ -15,6 +16,7 @@ class AuthenticationNotifier extends StateNotifier { this._deviceInfoService, this._backupService, this._apiService, + this._assetCacheService, ) : super( AuthenticationState( deviceId: "", @@ -41,6 +43,7 @@ class AuthenticationNotifier extends StateNotifier { final DeviceInfoService _deviceInfoService; final BackupService _backupService; final ApiService _apiService; + final AssetCacheService _assetCacheService; Future login( String email, @@ -151,7 +154,7 @@ class AuthenticationNotifier extends StateNotifier { Future logout() async { Hive.box(userInfoBox).delete(accessTokenKey); state = state.copyWith(isAuthenticated: false); - + _assetCacheService.invalidate(); return true; } @@ -197,5 +200,6 @@ final authenticationProvider = ref.watch(deviceInfoServiceProvider), ref.watch(backupServiceProvider), ref.watch(apiServiceProvider), + ref.watch(assetCacheServiceProvider), ); }); diff --git a/mobile/lib/shared/providers/asset.provider.dart b/mobile/lib/shared/providers/asset.provider.dart index 4b354d4c24..f0c90c74f1 100644 --- a/mobile/lib/shared/providers/asset.provider.dart +++ b/mobile/lib/shared/providers/asset.provider.dart @@ -21,15 +21,28 @@ class AssetNotifier extends StateNotifier> { } getAllAsset() async { + final stopwatch = Stopwatch(); + + if (_assetCacheService.isValid() && state.isEmpty) { + stopwatch.start(); state = await _assetCacheService.getAssetsAsync(); + debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms"); + stopwatch.reset(); } + stopwatch.start(); var allAssets = await _assetService.getAllAsset(); + debugPrint("Query assets from API: ${stopwatch.elapsedMilliseconds}ms"); + stopwatch.reset(); if (allAssets != null) { state = allAssets; + + stopwatch.start(); _cacheState(); + debugPrint("Store assets in cache: ${stopwatch.elapsedMilliseconds}ms"); + stopwatch.reset(); } }