You've already forked immich
							
							
				mirror of
				https://github.com/immich-app/immich.git
				synced 2025-10-31 00:18:28 +02:00 
			
		
		
		
	Switch to plain fs based caching mechanism
This commit is contained in:
		| @@ -25,11 +25,3 @@ const String backgroundBackupInfoBox = "immichBackgroundBackupInfoBox"; // Box | |||||||
| const String backupFailedSince = "immichBackupFailedSince"; // Key 1 | const String backupFailedSince = "immichBackupFailedSince"; // Key 1 | ||||||
| const String backupRequireWifi = "immichBackupRequireWifi"; // Key 2 | const String backupRequireWifi = "immichBackupRequireWifi"; // Key 2 | ||||||
| const String backupRequireCharging = "immichBackupRequireCharging"; // Key 3 | const String backupRequireCharging = "immichBackupRequireCharging"; // Key 3 | ||||||
|  |  | ||||||
| // Asset cache |  | ||||||
| const String assetListCacheBox = "assetListCacheBoxl"; |  | ||||||
| const String assetListCachedAssets = "assetListCachedAssets"; |  | ||||||
|  |  | ||||||
| // Album cache |  | ||||||
| const String albumListCacheBox = "albumListCacheBoxl"; |  | ||||||
| const String albumListCachedAssets = "albumListCachedAssets"; |  | ||||||
|   | |||||||
| @@ -38,14 +38,6 @@ void main() async { | |||||||
|   await Hive.openBox(hiveGithubReleaseInfoBox); |   await Hive.openBox(hiveGithubReleaseInfoBox); | ||||||
|   await Hive.openBox(userSettingInfoBox); |   await Hive.openBox(userSettingInfoBox); | ||||||
|  |  | ||||||
|   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( | ||||||
|       statusBarIconBrightness: Brightness.light, |       statusBarIconBrightness: Brightness.light, | ||||||
|   | |||||||
| @@ -14,7 +14,7 @@ class AlbumNotifier extends StateNotifier<List<AlbumResponseDto>> { | |||||||
|  |  | ||||||
|   getAllAlbums() async { |   getAllAlbums() async { | ||||||
|  |  | ||||||
|     if (_albumCacheService.isValid() && state.isEmpty) { |     if (await _albumCacheService.isValid() && state.isEmpty) { | ||||||
|       state = await _albumCacheService.get(); |       state = await _albumCacheService.get(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -2,12 +2,11 @@ | |||||||
| import 'package:collection/collection.dart'; | import 'package:collection/collection.dart'; | ||||||
| import 'package:flutter/foundation.dart'; | import 'package:flutter/foundation.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/modules/home/services/asset_cache.service.dart'; | import 'package:immich_mobile/modules/home/services/asset_cache.service.dart'; | ||||||
| import 'package:openapi/api.dart'; | import 'package:openapi/api.dart'; | ||||||
|  |  | ||||||
| class AlbumCacheService extends JsonCache<List<AlbumResponseDto>> { | class AlbumCacheService extends JsonCache<List<AlbumResponseDto>> { | ||||||
|   AlbumCacheService() : super(albumListCacheBox, albumListCachedAssets); |   AlbumCacheService() : super("album_cache"); | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   void put(List<AlbumResponseDto> data) { |   void put(List<AlbumResponseDto> data) { | ||||||
|   | |||||||
| @@ -1,35 +1,51 @@ | |||||||
| import 'dart:convert'; | import 'dart:convert'; | ||||||
|  | import 'dart:io'; | ||||||
|  |  | ||||||
| import 'package:collection/collection.dart'; | import 'package:collection/collection.dart'; | ||||||
| import 'package:flutter/foundation.dart'; | import 'package:flutter/foundation.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:http/http.dart'; | ||||||
| import 'package:immich_mobile/constants/hive_box.dart'; |  | ||||||
| import 'package:openapi/api.dart'; | import 'package:openapi/api.dart'; | ||||||
|  | import 'package:path_provider/path_provider.dart'; | ||||||
|  |  | ||||||
| abstract class JsonCache<T> { | abstract class JsonCache<T> { | ||||||
|   final String boxName; |   final String cacheFileName; | ||||||
|   final String valueKey; |  | ||||||
|   final LazyBox _cacheBox; |  | ||||||
|  |  | ||||||
|   JsonCache(this.boxName, this.valueKey) : _cacheBox = Hive.lazyBox(boxName); |   JsonCache(this.cacheFileName); | ||||||
|  |  | ||||||
|   bool isValid() { |   Future<File> _getCacheFile() async { | ||||||
|     return _cacheBox.containsKey(valueKey) && _cacheBox.containsKey(valueKey); |     final basePath = await getTemporaryDirectory(); | ||||||
|  |     final basePathName = basePath.path; | ||||||
|  |  | ||||||
|  |     final file = File("$basePathName/$cacheFileName.bin"); | ||||||
|  |  | ||||||
|  |     return file; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void invalidate() { |   Future<bool> isValid() async { | ||||||
|     _cacheBox.clear(); |     final file = await _getCacheFile(); | ||||||
|  |     return await file.exists(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void putRawData(dynamic data) { |   Future<void> invalidate() async { | ||||||
|  |     final file = await _getCacheFile(); | ||||||
|  |     await file.delete(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   Future<void> putRawData(dynamic data) async { | ||||||
|     final jsonString = json.encode(data); |     final jsonString = json.encode(data); | ||||||
|     _cacheBox.put(valueKey, jsonString); |     final file = await _getCacheFile(); | ||||||
|  |  | ||||||
|  |     if (!await file.exists()) { | ||||||
|  |       await file.create(); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     await file.writeAsString(jsonString); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   dynamic readRawData() async { |   dynamic readRawData() async { | ||||||
|     final data = await _cacheBox.get(valueKey); |     final file = await _getCacheFile(); | ||||||
|  |     final data = await file.readAsString(); | ||||||
|     return json.decode(data); |     return json.decode(data); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -38,7 +54,7 @@ abstract class JsonCache<T> { | |||||||
| } | } | ||||||
|  |  | ||||||
| class AssetCacheService extends JsonCache<List<AssetResponseDto>> { | class AssetCacheService extends JsonCache<List<AssetResponseDto>> { | ||||||
|   AssetCacheService() : super(assetListCacheBox, assetListCachedAssets); |   AssetCacheService() : super("asset_cache"); | ||||||
|  |  | ||||||
|   @override |   @override | ||||||
|   void put(List<AssetResponseDto> data) { |   void put(List<AssetResponseDto> data) { | ||||||
|   | |||||||
| @@ -24,7 +24,7 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> { | |||||||
|     final stopwatch = Stopwatch(); |     final stopwatch = Stopwatch(); | ||||||
|  |  | ||||||
|  |  | ||||||
|     if (_assetCacheService.isValid() && state.isEmpty) { |     if (await _assetCacheService.isValid() && state.isEmpty) { | ||||||
|       stopwatch.start(); |       stopwatch.start(); | ||||||
|       state = await _assetCacheService.get(); |       state = await _assetCacheService.get(); | ||||||
|       debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms"); |       debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms"); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user