Defining the requirement
As you have learned throughout the previous chapters, the HTTPClient
module is Observable-based, which means that methods such as get
, post
, put
and delete
return an Observable.
So, subscribing multiple times to this Observable will cause the source Observable to be re-created over and over again, hence performing a request on each subscription. It is a cold Observable, as we learned in Chapter 8, Multicasting Essentials. This behavior will result in an overhead of HTTP requests, which may decrease the performance of your web applications, especially if the server takes some time to respond.
Reducing HTTP requests by caching the result on the client side is one of the most commonly used techniques to optimize web applications. But when should we cache data? When data doesn't change frequently and it is used by more than one component, it makes a lot of sense to cache it and share it in multiple places. The user's profile data is a good example...