-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Conversation
What scenario are you planning on to invalidate the cache to fetch new data? |
IMO we don't really need invalidation here. I'd just replace the contents of the cache any time the app receives new asset data. It's only used for the initial loading and is not used later. if (_assetCacheService.isValid() && state.isEmpty) {
state = await _assetCacheService.getAssetsAsync();
} |
if (await _assetCacheService.isValid() && state.isEmpty) { | ||
stopwatch.start(); | ||
state = await _assetCacheService.get(); | ||
debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms"); | ||
stopwatch.reset(); | ||
} |
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.
Regarding your first question: All caches are invalidated when the user logs out. |
No description provided.