Skip to content

High performance self-hosted photo and video management solution.

JSON based caching

Matthias Rupp894eea739e96
diff --git a/mobile/lib/modules/home/services/asset_cache.service.dart b/mobile/lib/modules/home/services/asset_cache.service.dart
index 694bb50c9b..068acbafb9 100644
--- a/mobile/lib/modules/home/services/asset_cache.service.dart
+++ b/mobile/lib/modules/home/services/asset_cache.service.dart
@@ -1,4 +1,7 @@
+import 'dart:convert';
+
import 'package:collection/collection.dart';
+import 'package:flutter/foundation.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:immich_mobile/constants/hive_box.dart';
@@ -8,77 +11,40 @@ final assetCacheServiceProvider = Provider(
(ref) => AssetCacheService(),
);
-typedef CacheEntry = Map<dynamic, dynamic>;
-typedef CacheList = List<CacheEntry>;
-
class AssetCacheService {
final _cacheBox = Hive.box(assetListCacheBox);
bool isValid() {
- return _cacheBox.containsKey(assetListCachedAssets) && _rawGet().isNotEmpty;
+ return _cacheBox.containsKey(assetListCachedAssets) &&
+ _cacheBox.get(assetListCachedAssets) is String;
}
void putAssets(List<AssetResponseDto> assets) {
- _rawPut(assets.map((e) => _serialize(e)).toList());
- }
+ final mapList = assets.map((e) => e.toJson()).toList();
+ final jsonString = json.encode(mapList);
- List<AssetResponseDto> getAssets() {
- return _rawGet().map((e) => _deserialize(e)).whereNotNull().toList();
+ _cacheBox.put(assetListCachedAssets, jsonString);
}
- Future<List<AssetResponseDto>> getAssetsAsync() async {
- return Future.microtask(() => getAssets());
- }
+ List<AssetResponseDto> getAssets() {
+ try {
+ final jsonString = _cacheBox.get(assetListCachedAssets);
+ final mapList = json.decode(jsonString) as List<dynamic>;
- List<dynamic> _rawGet() {
- return _cacheBox.get(assetListCachedAssets) as List<dynamic>;
- }
+ final responseData = mapList
+ .map((e) => AssetResponseDto.fromJson(e))
+ .whereNotNull()
+ .toList();
- void _rawPut(CacheList data) {
- _cacheBox.put(assetListCachedAssets, data);
- }
+ return responseData;
+ } catch (e) {
+ debugPrint(e.toString());
- CacheEntry _serialize(AssetResponseDto a) {
- return {
- "id": a.id,
- "cat": a.createdAt,
- "did": a.deviceAssetId,
- "oid": a.ownerId,
- "dev": a.deviceId,
- "dur": a.duration,
- "mat": a.modifiedAt,
- "opa": a.originalPath,
- "typ": a.type.value,
- "exif": a.exifInfo?.toJson(),
- "fav": a.isFavorite,
- "evp": a.encodedVideoPath,
- "mim": a.mimeType,
- "rsp": a.resizePath,
- "wbp": a.webpPath,
- };
+ return [];
+ }
}
- AssetResponseDto? _deserialize(CacheEntry map) {
- try {
- return AssetResponseDto(
- type: AssetTypeEnum.values
- .firstWhere((element) => element.value == map["typ"]),
- id: map["id"],
- deviceAssetId: map["did"],
- ownerId: map["oid"],
- deviceId: map["dev"],
- originalPath: map["opa"],
- resizePath: map["rsp"],
- createdAt: map["cat"],
- modifiedAt: map["mat"],
- isFavorite: map["fav"],
- mimeType: map["mim"],
- duration: map["dur"],
- webpPath: map["wbp"],
- encodedVideoPath: map["evp"],
- );
- } catch (e) {
- return null;
- }
+ Future<List<AssetResponseDto>> getAssetsAsync() async {
+ return Future.microtask(() => getAssets());
}
}
diff --git a/mobile/lib/shared/providers/asset.provider.dart b/mobile/lib/shared/providers/asset.provider.dart
index 5b09935df6..4b354d4c24 100644
--- a/mobile/lib/shared/providers/asset.provider.dart
+++ b/mobile/lib/shared/providers/asset.provider.dart
@@ -16,6 +16,10 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
AssetNotifier(this._assetService, this._assetCacheService) : super([]);
+ _cacheState() {
+ _assetCacheService.putAssets(state);
+ }
+
getAllAsset() async {
if (_assetCacheService.isValid() && state.isEmpty) {
state = await _assetCacheService.getAssetsAsync();
@@ -25,16 +29,18 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
if (allAssets != null) {
state = allAssets;
- _assetCacheService.putAssets(allAssets);
+ _cacheState();
}
}
clearAllAsset() {
state = [];
+ _cacheState();
}
onNewAssetUploaded(AssetResponseDto newAsset) {
state = [...state, newAsset];
+ _cacheState();
}
deleteAssets(Set<AssetResponseDto> deleteAssets) async {
@@ -73,6 +79,8 @@ class AssetNotifier extends StateNotifier<List<AssetResponseDto>> {
state.where((immichAsset) => immichAsset.id != asset.id).toList();
}
}
+
+ _cacheState();
}
}