Skip to content
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 8 commits into from
Oct 19, 2022

Conversation

matthinc
Copy link
Contributor

No description provided.

@alextran1502
Copy link
Contributor

What scenario are you planning on to invalidate the cache to fetch new data?

@matthinc
Copy link
Contributor Author

matthinc commented Oct 15, 2022

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();
    }

@matthinc matthinc changed the title Add asset response cache for quicker initial loading time feat(mobile): Cache assets and albums for faster loading speed Oct 19, 2022
@matthinc matthinc marked this pull request as ready for review October 19, 2022 15:02
Comment on lines +27 to +32
if (await _assetCacheService.isValid() && state.isEmpty) {
stopwatch.start();
state = await _assetCacheService.get();
debugPrint("Reading assets from cache: ${stopwatch.elapsedMilliseconds}ms");
stopwatch.reset();
}
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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.

@matthinc
Copy link
Contributor Author

Regarding your first question: All caches are invalidated when the user logs out.

@alextran1502 alextran1502 merged commit 061b229 into main Oct 19, 2022
@alextran1502 alextran1502 deleted the feature/asset-response-caching branch October 19, 2022 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants