mirror of
				https://github.com/immich-app/immich.git
				synced 2025-11-03 19:17:11 -05:00 
			
		
		
		
	Add album list response caching
This commit is contained in:
		
							parent
							
								
									75d8ca1306
								
							
						
					
					
						commit
						d310c77fc8
					
				@ -29,3 +29,7 @@ const String backupRequireCharging = "immichBackupRequireCharging"; // Key 3
 | 
				
			|||||||
// Asset cache
 | 
					// Asset cache
 | 
				
			||||||
const String assetListCacheBox = "assetListCacheBox";
 | 
					const String assetListCacheBox = "assetListCacheBox";
 | 
				
			||||||
const String assetListCachedAssets = "assetListCachedAssets";
 | 
					const String assetListCachedAssets = "assetListCachedAssets";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Album cache
 | 
				
			||||||
 | 
					const String albumListCacheBox = "albumListCacheBox";
 | 
				
			||||||
 | 
					const String albumListCachedAssets = "albumListCachedAssets";
 | 
				
			||||||
 | 
				
			|||||||
@ -38,6 +38,7 @@ void main() async {
 | 
				
			|||||||
  await Hive.openBox(hiveGithubReleaseInfoBox);
 | 
					  await Hive.openBox(hiveGithubReleaseInfoBox);
 | 
				
			||||||
  await Hive.openBox(userSettingInfoBox);
 | 
					  await Hive.openBox(userSettingInfoBox);
 | 
				
			||||||
  await Hive.openBox(assetListCacheBox);
 | 
					  await Hive.openBox(assetListCacheBox);
 | 
				
			||||||
 | 
					  await Hive.openBox(albumListCacheBox);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  SystemChrome.setSystemUIOverlayStyle(
 | 
					  SystemChrome.setSystemUIOverlayStyle(
 | 
				
			||||||
    const SystemUiOverlayStyle(
 | 
					    const SystemUiOverlayStyle(
 | 
				
			||||||
 | 
				
			|||||||
@ -1,22 +1,35 @@
 | 
				
			|||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
 | 
					import 'package:hooks_riverpod/hooks_riverpod.dart';
 | 
				
			||||||
import 'package:immich_mobile/modules/album/services/album.service.dart';
 | 
					import 'package:immich_mobile/modules/album/services/album.service.dart';
 | 
				
			||||||
 | 
					import 'package:immich_mobile/modules/album/services/album_cache.service.dart';
 | 
				
			||||||
import 'package:openapi/api.dart';
 | 
					import 'package:openapi/api.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AlbumNotifier extends StateNotifier<List<AlbumResponseDto>> {
 | 
					class AlbumNotifier extends StateNotifier<List<AlbumResponseDto>> {
 | 
				
			||||||
  AlbumNotifier(this._albumService) : super([]);
 | 
					  AlbumNotifier(this._albumService, this._albumCacheService) : super([]);
 | 
				
			||||||
  final AlbumService _albumService;
 | 
					  final AlbumService _albumService;
 | 
				
			||||||
 | 
					  final AlbumCacheService _albumCacheService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  _cacheState() {
 | 
				
			||||||
 | 
					    _albumCacheService.put(state);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  getAllAlbums() async {
 | 
					  getAllAlbums() async {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (_albumCacheService.isValid() && state.isEmpty) {
 | 
				
			||||||
 | 
					      state = await _albumCacheService.getAsync();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    List<AlbumResponseDto>? albums =
 | 
					    List<AlbumResponseDto>? albums =
 | 
				
			||||||
        await _albumService.getAlbums(isShared: false);
 | 
					        await _albumService.getAlbums(isShared: false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (albums != null) {
 | 
					    if (albums != null) {
 | 
				
			||||||
      state = albums;
 | 
					      state = albums;
 | 
				
			||||||
 | 
					      _cacheState();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  deleteAlbum(String albumId) {
 | 
					  deleteAlbum(String albumId) {
 | 
				
			||||||
    state = state.where((album) => album.id != albumId).toList();
 | 
					    state = state.where((album) => album.id != albumId).toList();
 | 
				
			||||||
 | 
					    _cacheState();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Future<AlbumResponseDto?> createAlbum(
 | 
					  Future<AlbumResponseDto?> createAlbum(
 | 
				
			||||||
@ -28,6 +41,8 @@ class AlbumNotifier extends StateNotifier<List<AlbumResponseDto>> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if (album != null) {
 | 
					    if (album != null) {
 | 
				
			||||||
      state = [...state, album];
 | 
					      state = [...state, album];
 | 
				
			||||||
 | 
					      _cacheState();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return album;
 | 
					      return album;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return null;
 | 
					    return null;
 | 
				
			||||||
@ -36,5 +51,8 @@ class AlbumNotifier extends StateNotifier<List<AlbumResponseDto>> {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
final albumProvider =
 | 
					final albumProvider =
 | 
				
			||||||
    StateNotifierProvider<AlbumNotifier, List<AlbumResponseDto>>((ref) {
 | 
					    StateNotifierProvider<AlbumNotifier, List<AlbumResponseDto>>((ref) {
 | 
				
			||||||
  return AlbumNotifier(ref.watch(albumServiceProvider));
 | 
					  return AlbumNotifier(
 | 
				
			||||||
 | 
					    ref.watch(albumServiceProvider),
 | 
				
			||||||
 | 
					    ref.watch(albumCacheServiceProvider),
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										38
									
								
								mobile/lib/modules/album/services/album_cache.service.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								mobile/lib/modules/album/services/album_cache.service.dart
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,38 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					import 'package:collection/collection.dart';
 | 
				
			||||||
 | 
					import 'package:flutter/foundation.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:openapi/api.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class AlbumCacheService extends JsonCache<List<AlbumResponseDto>> {
 | 
				
			||||||
 | 
					  AlbumCacheService() : super(albumListCacheBox, albumListCachedAssets);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  void put(List<AlbumResponseDto> data) {
 | 
				
			||||||
 | 
					    putRawData(data.map((e) => e.toJson()).toList());
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  List<AlbumResponseDto> get() {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      final mapList =  readRawData() as List<dynamic>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      final responseData = mapList
 | 
				
			||||||
 | 
					          .map((e) => AlbumResponseDto.fromJson(e))
 | 
				
			||||||
 | 
					          .whereNotNull()
 | 
				
			||||||
 | 
					          .toList();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      return responseData;
 | 
				
			||||||
 | 
					    } catch (e) {
 | 
				
			||||||
 | 
					      debugPrint(e.toString());
 | 
				
			||||||
 | 
					      return [];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					final albumCacheServiceProvider = Provider(
 | 
				
			||||||
 | 
					      (ref) => AlbumCacheService(),
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
@ -4,36 +4,55 @@ import 'package:collection/collection.dart';
 | 
				
			|||||||
import 'package:flutter/foundation.dart';
 | 
					import 'package:flutter/foundation.dart';
 | 
				
			||||||
import 'package:hive_flutter/hive_flutter.dart';
 | 
					import 'package:hive_flutter/hive_flutter.dart';
 | 
				
			||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
 | 
					import 'package:hooks_riverpod/hooks_riverpod.dart';
 | 
				
			||||||
 | 
					import 'package:http/http.dart';
 | 
				
			||||||
import 'package:immich_mobile/constants/hive_box.dart';
 | 
					import 'package:immich_mobile/constants/hive_box.dart';
 | 
				
			||||||
import 'package:openapi/api.dart';
 | 
					import 'package:openapi/api.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
final assetCacheServiceProvider = Provider(
 | 
					abstract class JsonCache<T> {
 | 
				
			||||||
  (ref) => AssetCacheService(),
 | 
					  final String boxName;
 | 
				
			||||||
);
 | 
					  final String valueKey;
 | 
				
			||||||
 | 
					  final Box _cacheBox;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AssetCacheService {
 | 
					  JsonCache(this.boxName, this.valueKey) : _cacheBox = Hive.box(boxName);
 | 
				
			||||||
  final _cacheBox = Hive.box(assetListCacheBox);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool isValid() {
 | 
					  bool isValid() {
 | 
				
			||||||
    return _cacheBox.containsKey(assetListCachedAssets) &&
 | 
					    return _cacheBox.containsKey(valueKey) && _cacheBox.get(valueKey) is String;
 | 
				
			||||||
        _cacheBox.get(assetListCachedAssets) is String;
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void invalidate() {
 | 
					  void invalidate() {
 | 
				
			||||||
    _cacheBox.clear();
 | 
					    _cacheBox.clear();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void putAssets(List<AssetResponseDto> assets) {
 | 
					  void putRawData(dynamic data) {
 | 
				
			||||||
    final mapList = assets.map((e) => e.toJson()).toList();
 | 
					    final jsonString = json.encode(data);
 | 
				
			||||||
    final jsonString = json.encode(mapList);
 | 
					    _cacheBox.put(valueKey, jsonString);
 | 
				
			||||||
 | 
					 | 
				
			||||||
    _cacheBox.put(assetListCachedAssets, jsonString);
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  List<AssetResponseDto> getAssets() {
 | 
					  dynamic readRawData() {
 | 
				
			||||||
 | 
					    return json.decode(_cacheBox.get(valueKey));
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  void put(T data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  T get();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Future<T> getAsync() async {
 | 
				
			||||||
 | 
					    return Future.microtask(() => get());
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class AssetCacheService extends JsonCache<List<AssetResponseDto>> {
 | 
				
			||||||
 | 
					  AssetCacheService() : super(assetListCacheBox, assetListCachedAssets);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  void put(List<AssetResponseDto> data) {
 | 
				
			||||||
 | 
					    putRawData(data.map((e) => e.toJson()).toList());
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @override
 | 
				
			||||||
 | 
					  List<AssetResponseDto> get() {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      final jsonString = _cacheBox.get(assetListCachedAssets);
 | 
					      final mapList =  readRawData() as List<dynamic>;
 | 
				
			||||||
      final mapList = json.decode(jsonString) as List<dynamic>;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      final responseData = mapList
 | 
					      final responseData = mapList
 | 
				
			||||||
          .map((e) => AssetResponseDto.fromJson(e))
 | 
					          .map((e) => AssetResponseDto.fromJson(e))
 | 
				
			||||||
@ -48,7 +67,8 @@ class AssetCacheService {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Future<List<AssetResponseDto>> getAssetsAsync() async {
 | 
					 | 
				
			||||||
    return Future.microtask(() => getAssets());
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					final assetCacheServiceProvider = Provider(
 | 
				
			||||||
 | 
					      (ref) => AssetCacheService(),
 | 
				
			||||||
 | 
					);
 | 
				
			||||||
 | 
				
			|||||||
@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
 | 
				
			|||||||
import 'package:hive/hive.dart';
 | 
					import 'package:hive/hive.dart';
 | 
				
			||||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
 | 
					import 'package:hooks_riverpod/hooks_riverpod.dart';
 | 
				
			||||||
import 'package:immich_mobile/constants/hive_box.dart';
 | 
					import 'package:immich_mobile/constants/hive_box.dart';
 | 
				
			||||||
 | 
					import 'package:immich_mobile/modules/album/services/album_cache.service.dart';
 | 
				
			||||||
import 'package:immich_mobile/modules/home/services/asset_cache.service.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/authentication_state.model.dart';
 | 
				
			||||||
import 'package:immich_mobile/modules/login/models/hive_saved_login_info.model.dart';
 | 
					import 'package:immich_mobile/modules/login/models/hive_saved_login_info.model.dart';
 | 
				
			||||||
@ -17,6 +18,7 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
 | 
				
			|||||||
    this._backupService,
 | 
					    this._backupService,
 | 
				
			||||||
    this._apiService,
 | 
					    this._apiService,
 | 
				
			||||||
    this._assetCacheService,
 | 
					    this._assetCacheService,
 | 
				
			||||||
 | 
					    this._albumCacheService,
 | 
				
			||||||
  ) : super(
 | 
					  ) : super(
 | 
				
			||||||
          AuthenticationState(
 | 
					          AuthenticationState(
 | 
				
			||||||
            deviceId: "",
 | 
					            deviceId: "",
 | 
				
			||||||
@ -44,6 +46,7 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
 | 
				
			|||||||
  final BackupService _backupService;
 | 
					  final BackupService _backupService;
 | 
				
			||||||
  final ApiService _apiService;
 | 
					  final ApiService _apiService;
 | 
				
			||||||
  final AssetCacheService _assetCacheService;
 | 
					  final AssetCacheService _assetCacheService;
 | 
				
			||||||
 | 
					  final AlbumCacheService _albumCacheService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Future<bool> login(
 | 
					  Future<bool> login(
 | 
				
			||||||
    String email,
 | 
					    String email,
 | 
				
			||||||
@ -155,6 +158,7 @@ class AuthenticationNotifier extends StateNotifier<AuthenticationState> {
 | 
				
			|||||||
    Hive.box(userInfoBox).delete(accessTokenKey);
 | 
					    Hive.box(userInfoBox).delete(accessTokenKey);
 | 
				
			||||||
    state = state.copyWith(isAuthenticated: false);
 | 
					    state = state.copyWith(isAuthenticated: false);
 | 
				
			||||||
    _assetCacheService.invalidate();
 | 
					    _assetCacheService.invalidate();
 | 
				
			||||||
 | 
					    _albumCacheService.invalidate();
 | 
				
			||||||
    return true;
 | 
					    return true;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -201,5 +205,6 @@ final authenticationProvider =
 | 
				
			|||||||
    ref.watch(backupServiceProvider),
 | 
					    ref.watch(backupServiceProvider),
 | 
				
			||||||
    ref.watch(apiServiceProvider),
 | 
					    ref.watch(apiServiceProvider),
 | 
				
			||||||
    ref.watch(assetCacheServiceProvider),
 | 
					    ref.watch(assetCacheServiceProvider),
 | 
				
			||||||
 | 
					    ref.watch(albumCacheServiceProvider),
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -17,7 +17,7 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
 | 
				
			|||||||
  AssetNotifier(this._assetService, this._assetCacheService) : super([]);
 | 
					  AssetNotifier(this._assetService, this._assetCacheService) : super([]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  _cacheState() {
 | 
					  _cacheState() {
 | 
				
			||||||
    _assetCacheService.putAssets(state);
 | 
					    _assetCacheService.put(state);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  getAllAsset() async {
 | 
					  getAllAsset() async {
 | 
				
			||||||
@ -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.getAssetsAsync();
 | 
					      state = await _assetCacheService.getAsync();
 | 
				
			||||||
      debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms");
 | 
					      debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms");
 | 
				
			||||||
      stopwatch.reset();
 | 
					      stopwatch.reset();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user