From 6796462b13f3c97d2c98d635677909fe3f3c2485 Mon Sep 17 00:00:00 2001 From: Matthias Rupp Date: Mon, 17 Oct 2022 18:02:43 +0200 Subject: [PATCH] Switch to plain fs based caching mechanism --- mobile/lib/constants/hive_box.dart | 8 ---- mobile/lib/main.dart | 8 ---- .../album/providers/album.provider.dart | 2 +- .../album/services/album_cache.service.dart | 3 +- .../home/services/asset_cache.service.dart | 44 +++++++++++++------ .../lib/shared/providers/asset.provider.dart | 2 +- 6 files changed, 33 insertions(+), 34 deletions(-) diff --git a/mobile/lib/constants/hive_box.dart b/mobile/lib/constants/hive_box.dart index d36cfba48b..7faf6555f6 100644 --- a/mobile/lib/constants/hive_box.dart +++ b/mobile/lib/constants/hive_box.dart @@ -25,11 +25,3 @@ const String backgroundBackupInfoBox = "immichBackgroundBackupInfoBox"; // Box const String backupFailedSince = "immichBackupFailedSince"; // Key 1 const String backupRequireWifi = "immichBackupRequireWifi"; // Key 2 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"; diff --git a/mobile/lib/main.dart b/mobile/lib/main.dart index 24b3cd2a79..ee5209b5c2 100644 --- a/mobile/lib/main.dart +++ b/mobile/lib/main.dart @@ -38,14 +38,6 @@ void main() async { await Hive.openBox(hiveGithubReleaseInfoBox); 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( const SystemUiOverlayStyle( statusBarIconBrightness: Brightness.light, diff --git a/mobile/lib/modules/album/providers/album.provider.dart b/mobile/lib/modules/album/providers/album.provider.dart index 4973779286..9a098aa49b 100644 --- a/mobile/lib/modules/album/providers/album.provider.dart +++ b/mobile/lib/modules/album/providers/album.provider.dart @@ -14,7 +14,7 @@ class AlbumNotifier extends StateNotifier> { getAllAlbums() async { - if (_albumCacheService.isValid() && state.isEmpty) { + if (await _albumCacheService.isValid() && state.isEmpty) { state = await _albumCacheService.get(); } diff --git a/mobile/lib/modules/album/services/album_cache.service.dart b/mobile/lib/modules/album/services/album_cache.service.dart index 856423bd0d..d7ed5b7334 100644 --- a/mobile/lib/modules/album/services/album_cache.service.dart +++ b/mobile/lib/modules/album/services/album_cache.service.dart @@ -2,12 +2,11 @@ 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> { - AlbumCacheService() : super(albumListCacheBox, albumListCachedAssets); + AlbumCacheService() : super("album_cache"); @override void put(List data) { diff --git a/mobile/lib/modules/home/services/asset_cache.service.dart b/mobile/lib/modules/home/services/asset_cache.service.dart index f3f0845c21..0ba669d4c1 100644 --- a/mobile/lib/modules/home/services/asset_cache.service.dart +++ b/mobile/lib/modules/home/services/asset_cache.service.dart @@ -1,35 +1,51 @@ import 'dart:convert'; +import 'dart:io'; 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:http/http.dart'; -import 'package:immich_mobile/constants/hive_box.dart'; import 'package:openapi/api.dart'; +import 'package:path_provider/path_provider.dart'; abstract class JsonCache { - final String boxName; - final String valueKey; - final LazyBox _cacheBox; + final String cacheFileName; - JsonCache(this.boxName, this.valueKey) : _cacheBox = Hive.lazyBox(boxName); + JsonCache(this.cacheFileName); - bool isValid() { - return _cacheBox.containsKey(valueKey) && _cacheBox.containsKey(valueKey); + Future _getCacheFile() async { + final basePath = await getTemporaryDirectory(); + final basePathName = basePath.path; + + final file = File("$basePathName/$cacheFileName.bin"); + + return file; } - void invalidate() { - _cacheBox.clear(); + Future isValid() async { + final file = await _getCacheFile(); + return await file.exists(); } - void putRawData(dynamic data) { + Future invalidate() async { + final file = await _getCacheFile(); + await file.delete(); + } + + Future putRawData(dynamic data) async { 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 { - final data = await _cacheBox.get(valueKey); + final file = await _getCacheFile(); + final data = await file.readAsString(); return json.decode(data); } @@ -38,7 +54,7 @@ abstract class JsonCache { } class AssetCacheService extends JsonCache> { - AssetCacheService() : super(assetListCacheBox, assetListCachedAssets); + AssetCacheService() : super("asset_cache"); @override void put(List data) { diff --git a/mobile/lib/shared/providers/asset.provider.dart b/mobile/lib/shared/providers/asset.provider.dart index 589a94a84d..386d71cae0 100644 --- a/mobile/lib/shared/providers/asset.provider.dart +++ b/mobile/lib/shared/providers/asset.provider.dart @@ -24,7 +24,7 @@ class AssetNotifier extends StateNotifier> { final stopwatch = Stopwatch(); - if (_assetCacheService.isValid() && state.isEmpty) { + if (await _assetCacheService.isValid() && state.isEmpty) { stopwatch.start(); state = await _assetCacheService.get(); debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms");