问题
I want to cache data in an angular application, and avoid fetching data from the server too often.
The data is displayed in a material design table (datasource).
I have read some blogs that suggest using RxJS's publishReplay
along with RefCount
, or shareReplay
, to implement caching.
However, as far as I understand, this means that the cached data is stored in RAM. Is this a good strategy when there is a lot of data to be saved, or should I use something else like the browser's local storage?
Is there any idiom on how to manage cache in Angular? What is the recommended approach for saving records of a database in a cache (~ hundreds of lines) ?
Thanks
回答1:
^^ You have multiple options.
You can do it with a custom cache in the service (https://stackoverflow.com/a/60190745/1974681). With this approach when you reload your app, the caché is refreshed too.
You could use localStorage or sessionStorage. In that case the cache will not be refreshed on app reload (https://alligator.io/js/introduction-localstorage-sessionstorage/). In the end, browser storage uses RAM too.
localStorage and sessionStorage accomplish the exact same thing and have the same API, but with sessionStorage the data is persisted only until the window or tab is closed, while with localStorage the data is persisted until the user manually clears the browser cache or until your web app clears the data.
You can go for more complex solutions like Orbitjs, which is like having a database in the browser and you can work offline (very cool)
I hope I've helped!
来源:https://stackoverflow.com/questions/60186091/caching-mat-table-datasource-data-in-angular