Reputation: 1446
I'm developing an Angular app that handles a massive amount of data from the server. So every API call can take up to 5 seconds and there are pages we have few API calls.
What I want to achieve is to save the data in cache for every page I'm visiting and if I navigate to the page again I will see the cached data immediately, call API to get new data and refresh only the component that their model has changed.
Now API calls are being done from ngOnInit
method and the app show spinner until the results are shown.
Is there any best practice to achieve that behavior in Angular, other than saving the json in local-storage or something similar?
Upvotes: 4
Views: 14881
Reputation: 8042
Question is unfocused but I'll bite.
Ways of saving data in increasing complexity are:
ngrx
(I recommend @ngrx/data
if you have control over the api design and @ngrx/entity
otherwise)These are all "online" so that app refresh loses the data and refresh api calls would be needed (which isn't necessarily a bad thing).
Good principles and rules of thumb:
ngrx
a good pattern is API → normalizr → entities → storeUpvotes: 2
Reputation: 577
TL;DR; google for "caching technics with RxJS"
The bigger answer:
There are several options:
localStorage
/sessionStorage
if you need it to be persistentBehaviorSubject
and retrieve it latest value(article for example)...and many more depending on what you want, and how the code is constructed.
If I was you - I would pick the variant with BehaviorSubject
as initial if it is a simple application and don't have a huge state and it's management.
Upvotes: 11