-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(mobile): Cache assets and albums for faster loading speed #826
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1156290
Add asset response cache
matthinc 894eea7
JSON based caching
matthinc 75d8ca1
Invalidation on logout and timing measurements
matthinc d310c77
Add album list response caching
matthinc d08475d
Switch to lazyBox
matthinc 6796462
Switch to plain fs based caching mechanism
matthinc d77e254
Add cache for shared albums
matthinc 3617433
Refactor abstract class to separate file
matthinc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
mobile/lib/modules/album/services/album_cache.service.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
import 'package:collection/collection.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:immich_mobile/shared/services/json_cache.dart'; | ||
import 'package:openapi/api.dart'; | ||
|
||
class BaseAlbumCacheService extends JsonCache<List<AlbumResponseDto>> { | ||
BaseAlbumCacheService(super.cacheFileName); | ||
|
||
@override | ||
void put(List<AlbumResponseDto> data) { | ||
putRawData(data.map((e) => e.toJson()).toList()); | ||
} | ||
|
||
@override | ||
Future<List<AlbumResponseDto>> get() async { | ||
try { | ||
final mapList = await readRawData() as List<dynamic>; | ||
|
||
final responseData = mapList | ||
.map((e) => AlbumResponseDto.fromJson(e)) | ||
.whereNotNull() | ||
.toList(); | ||
|
||
return responseData; | ||
} catch (e) { | ||
debugPrint(e.toString()); | ||
return []; | ||
} | ||
} | ||
} | ||
|
||
class AlbumCacheService extends BaseAlbumCacheService { | ||
AlbumCacheService() : super("album_cache"); | ||
} | ||
|
||
class SharedAlbumCacheService extends BaseAlbumCacheService { | ||
SharedAlbumCacheService() : super("shared_album_cache"); | ||
} | ||
|
||
final albumCacheServiceProvider = Provider( | ||
(ref) => AlbumCacheService(), | ||
); | ||
|
||
final sharedAlbumCacheServiceProvider = Provider( | ||
(ref) => SharedAlbumCacheService(), | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import 'package:collection/collection.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:immich_mobile/shared/services/json_cache.dart'; | ||
import 'package:openapi/api.dart'; | ||
|
||
|
||
class AssetCacheService extends JsonCache<List<AssetResponseDto>> { | ||
AssetCacheService() : super("asset_cache"); | ||
|
||
@override | ||
void put(List<AssetResponseDto> data) { | ||
putRawData(data.map((e) => e.toJson()).toList()); | ||
} | ||
|
||
@override | ||
Future<List<AssetResponseDto>> get() async { | ||
try { | ||
final mapList = await readRawData() as List<dynamic>; | ||
|
||
final responseData = mapList | ||
.map((e) => AssetResponseDto.fromJson(e)) | ||
.whereNotNull() | ||
.toList(); | ||
|
||
return responseData; | ||
} catch (e) { | ||
debugPrint(e.toString()); | ||
|
||
return []; | ||
} | ||
} | ||
} | ||
|
||
final assetCacheServiceProvider = Provider( | ||
(ref) => AssetCacheService(), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:path_provider/path_provider.dart'; | ||
|
||
abstract class JsonCache<T> { | ||
final String cacheFileName; | ||
|
||
JsonCache(this.cacheFileName); | ||
|
||
Future<File> _getCacheFile() async { | ||
final basePath = await getTemporaryDirectory(); | ||
final basePathName = basePath.path; | ||
|
||
final file = File("$basePathName/$cacheFileName.bin"); | ||
|
||
return file; | ||
} | ||
|
||
Future<bool> isValid() async { | ||
final file = await _getCacheFile(); | ||
return await file.exists(); | ||
} | ||
|
||
Future<void> invalidate() async { | ||
final file = await _getCacheFile(); | ||
await file.delete(); | ||
} | ||
|
||
Future<void> putRawData(dynamic data) async { | ||
final jsonString = json.encode(data); | ||
final file = await _getCacheFile(); | ||
|
||
if (!await file.exists()) { | ||
await file.create(); | ||
} | ||
|
||
await file.writeAsString(jsonString); | ||
} | ||
|
||
dynamic readRawData() async { | ||
final file = await _getCacheFile(); | ||
final data = await file.readAsString(); | ||
return json.decode(data); | ||
} | ||
|
||
void put(T data); | ||
Future<T> get(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understanding of this code block, the data will be displayed from the cache first, then will fetch the new data in the background and update the state, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the state is updated twice. First time from the cache, second time from API.
I think one possible optimization would be to start both background jobs first and then await the results. But that would cause a race condition so I avoided it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, would it worth to create an api endpoint to get the asset count for each user and check that count before perform the fetch from the server?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO not really. Checking the cache costs as few ms, loading data from the cache 200-300ms.